1 / 29

Network programming

Network programming. With JAVA. InetAddress class. The java.net.InetAddress class is Java's high-level representation of an IP address. It is used by most of the other networking classes including both a hostname and an IP address.

leda
Download Presentation

Network programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network programming With JAVA

  2. InetAddress class • The java.net.InetAddress class is Java's high-level representation of an IP address. It is used by most of the other networking classes including both a hostname and an IP address. • Three static methods which are often used: getLocalHost(), getByName(String), getAllByName(String) Example InetAddress add1= InetAddress.getLocalHost(); InetAddress add2= InetAddress.getByName(“localhost”);

  3. getLocalhost() try { InetAddressaddress = InetAddress.getLocalHost(); System.out.println("My name is:”+address.getHostName()); System.out.println(“My Address is:”+address.getHostAddress()); } catch (UnknownHostException e) { System.out.println(“I don't know."); }

  4. getByName() ia = InetAddress.getByName("www.yahoo.com"); System.out.println(ia.getHostName()); System.out.println(ia.getHostAddress());

  5. getAllByName() InetAddress[] a = ia.getAllByName("www.google.com"); for (InetAddressiac : a) { System.out.println(iac.getHostAddress()); }

  6. Socket Programming

  7. Host 1 203.162.39.51 SOCKET Process A The transmitter & receiver establishes a TCP connection called socket. Socket enables data being transmitted & received on networks,. Port:4523 Process B Port:80 Host 2 10.6.8.111

  8. UDP Programming

  9. Java UDP Classes • Consist of two classes: DatagramPacket and DatagramSocket. • The DatagramPacket class stuffs bytes of data into UDP packets called datagrams and lets you un-stuff datagrams that you receive. • DatagramSocket class is used for sending and receiving UDP datagrams. • To send data, you put the data in a DatagramPacket and send using a DatagramSocket. • To receive data, you receive a DatagramPacket object from a DatagramSocket and then read the contents of the packet.

  10. DatagramPacket Constructor for sending: public DatagramPacket(byte[] data, int length, InetAddress destination, int port) public DatagramPacket(byte[] data,intoffset, int length, InetAddress destination, int port) String s = "This is a test."; byte[] data = s.getBytes(); try { InetAddressia = InetAddress.getByName("www.yahoo.com"); int port = 7; DatagramPacket dp = new DatagramPacket(data, data.length, ia, port); } …

  11. DatagramPacket Constructor for receiving: public DatagramPacket(byte[] buffer, int length) publicDatagramPacket(byte[] buffer, int offset, int length) Example: byte[] buffer = new byte[8192]; DatagramPacketdp = new DatagramPacket(buffer, buffer.length);

  12. The get methods • public InetAddressgetAddress() returns an InetAddress object containing the address of the remote host. • public intgetPort() returns an integer specifying the remote port. public byte[] getData() returns a byte array containing the data from the datagram. • public intgetLength() returns the number of bytes of data in the datagram.

  13. Example String s = "This is a test."; byte[] data = s.getBytes( ); try { InetAddressia = InetAddress.getByName("www.yahoo.com"); int port = 7; DatagramPacket dp = new DatagramPacket(data, data.length, ia, port); System.out.println("This packet is addressed to " + dp.getAddress() + " on port " + dp.getPort()); System.out.println("There are " + dp.getLength() + " bytes of data in the packet"); System.out.println( new String(dp.getData(), dp.getOffset(), dp.getLength())); } catch (UnknownHostException e) { System.err.println(e);}

  14. DatagramSocket class • To send or receive a DatagramPacket, one must open a datagram socket. • Adatagram socket is created and accessed through the DatagramSocket class.

  15. Constructors publicDatagramSocket() throwsSocketException • This constructor is used in a client that initiates a conversation with a server. • It also receive the datagrams that a server sends back to it. • A SocketException is thrown if the socket can't be created.

  16. Constructors Public DatagramSocket(int port) throwsSocketException • Creates a socket that listens on the particular port. • This constructor is used in a server that listens on a well-known port. • A SocketException is thrown if the socket can't be created. • There are two common reasons for the constructor to fail: • The specified port is already occupied, oryou are trying to connect to a port below 1,024.

  17. DatagramSocketMethods 1. public void send(DatagramPacket p) throwsIOException 2. public void receive(DatagramPacket p) throwsIOException 3. public void close() 4. publicintgetLocalPort()

  18. UDP Server Procedure • Create a UDP socket • Create a DatagramPacket to receive data • Receive data from Client. • Close UDP Socket DatagramSocket dsk= new DatagramSocket(1234); byte[] buffer= new byte[128]; DatagramPacket pk= new DatagramPacket(buffer, 128); dsk.receive(pk); System.out.println(“Client: ” + pk.getAddress() + ”:” + pk.getPort()); System.out.println(new String(buffer, 0, pk.getLength())); dsk.close();

  19. UDP Client Procedure • Create a UDP Socket • Create a DatagramPacketto send data • Send data • Close UDP Socket DatagramSocket dsk= new DatagramSocket(); String msg= “abc”; InetAddressaddr= InetAddress.getByName(“localhost”); DatagramPacket pk= new DatagramPacket(msg.getBytes(), msg.length(), addr, 1234); dsk.send(pk); dsk.close();

  20. TCP Programming

  21. TCP Sockets

  22. ServerSocket constructor publicServerSocket (intport) throwsIOException Example: To create a server socket to listen on port 1234: try { ServerSocket server = new ServerSocket(1234); … } catch (IOExceptionie) { // error catching code … }

  23. ServerSocketmethods • publicSocket accept() throwsIOException • publicvoid close() throwsIOException Example: To create a server socket to listen on port 1234 and accept connection request: try { ServerSocket server = new ServerSocket(1234); while (true) { Socket connection = server.accept(); provideService(connection); } } catch (IOExceptionie) { // error catching code … }

  24. Client Socket constructor Creates a TCP socket to the specified port on the specified host and attempts to connect to the remote host. 1.publicSocket(String host, int port) throwsUnknownHostException, IOException 2.publicSocket(InetAddressaddress, intport) throwsIOException

  25. Example try { Socket theSocket = new Socket(host, 80); System.out.println("Connected to " + theSocket.getInetAddress() + " on port " + theSocket.getPort() + " from port " + theSocket.getLocalPort() +" of “+ theSocket.getLocalAddress()); } catch (UnknownHostException ex) { System.err.println("I can't find " + host); } catch (SocketException ex) { System.err.println("Could not connect to " + host); } catch (IOException ex) { System.err.println(ex); }

  26. Data transmission Once connected, data will be transmitted/received : 1. publicInputStreamgetInputStream() throwsIOException 2. publicOutputStreamgetOutputStream() throwsIOException

  27. Example void doClientConnection(String computerName, intserverPort) { Socket connection; InputStreamin; OutputStreamout; try { connection = new Socket(computerName,serverPort); in = connection.getInputStream(); out = connection.getOutputStream(); } catch (IOException e) { // Error message return; } . . // Use the streams, in and out, to communicate with server. . try { connection.close(); } catch (IOException e) { } }

  28. TCP server procedures ServerSocketwelcomeSocket = new ServerSocket(6192); Socket connectionSocket= welcomeSocket.accept(); InputStream is= connectionSocket.getInputStream(); OutputStreamos= connectionSocket.getOutputStream(); byte[] buffer= new byte[128]; intlen= is.read(buffer); System.out.println(new String(buffer,0,len)); connectionSocket.close(); welcomeSocket.close();

  29. TCP client procedures Socket clientSocket = new Socket(host,6192); InputStream is= clientSocket.getInputStream(); OutputStreamos= connectionSocket.getOutputStream(); os.write(buffer); clientSocket.close();

More Related