Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();

Similar presentations


Presentation on theme: "Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();"— Presentation transcript:

1

2 Network Programming with Java

3 java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost(); System.out.println(localAddress.getHostAddress()); } Know your own IP Address Know IP address of a Host public static void main(String[] args) throws UnknownHostException { InetAddress googleAddress = InetAddress.getByName("www.google.com"); System.out.println(googleAddress.getHostAddress()); }

4 Java TCP/IP Server & Clients TCP/IP Server: That(a program or a process) listens to a TCP port. Client: What (a program or a process) initiates the connection Server is represented by java.net.ServerSocket Client is represented by java.net.Socket int portNumber = 7896; // TCP port Number ServerSocket server = new ServerSocket(portNumber);// listen on port Socket socket = server.accept(); // Blocks until connection made String destIP = "172.17.20.123"; // Target Host IPv4 Address int portNumber = 7896; // TCP port Number Socket socket = new Socket(destIP, portNumber);// connect

5 Stay & while connected… ServerClient Wait for connection serverSocket.accept(); Socket socket = new Socket(destIP,portNumber); Server’s Output/ Client’s Input Client’s Output/ Server’s Input

6 Java I/O Some important OutputStream child: DataOutputStream FileOutputStream ByteArrayOutputStream

7 Java I/O Some important InputStream child: DataInputStream FileInputStream ByteArrayInputStream Read Data From Console (command) DataInputStream dis=new DataInputStream(System.in); String x=dis.readLine(); Read an input integer Example DataInputStream dis=new DataInputStream(System.in); String x=dis.readLine(); int z=Integer.parseInt(x);

8 File Handling To write some text to a File, follow the steps below… 1. File dir = new File("H:\\soham"); 2. Check if the directory exist and if H:\soham is a Directory and not a file, 3. create a file under the parent directory 4. Extract the raw bytes from the String To write some text to a File, follow the steps below… 1. File dir = new File("H:\\soham"); 2. Check if the directory exist and if H:\soham is a Directory and not a file, 3. create a file under the parent directory 4. Extract the raw bytes from the String if(false==dir.exists() || false==dir.isDirectory()){ dir.mkdirs(); } File outFile = new File(dir, "we.txt"); String outString = "Welcome to File handling"; byte[] data = outString.getBytes();

9 File Handling 5.Open an OutputStream to the file: 6.Write the raw bytes to the OutputStream, flush and close it FileOutputStream fos = new FileOutputStream(outFile); fos.write(data); fos.flush(); fos.close();

10 Write to a File : Second approach FileOutputStream fos = new FileOutputStream(outFile); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(90); dos.flush(); dos.writeUTF(outString); dos.flush(); dos.close(); DataOutputStream provides a wrapper over an OutputStream to write primitive data types, not just byte[]

11 Read from a File Read byte-by-byte Read Bytes chunk-wise : Faster approach


Download ppt "Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();"

Similar presentations


Ads by Google