1 / 16

Socket programming 1

Socket programming 1. getByName. import java.net.*; public class GetHostName { public static void main (String args []) { String host = "www.concordia.ca"; try { InetAddress address = InetAddress. getByName (host); System. out.println (address); }

kioko
Télécharger la présentation

Socket programming 1

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. Socket programming 1

  2. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "www.concordia.ca"; try { InetAddress address = InetAddress.getByName(host); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find "+ host); } } }

  3. getByName import java.net.*; public class GetNameByAddress { public static void main (String args[]) { try { InetAddress address = InetAddress.getByName("132.205.7.63"); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find 132.205.7.63"); } } }

  4. LocalMachineAddress import java.net.*; public class LocalMachineAddress { public static void main (String args[]) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println(address); } catch (UnknownHostException e) { System.out.println("Could not find this computer's address."); } } }

  5. Local machine name import java.net.*; public class LocalMachineName { public static void main (String args[]) { try { InetAddress address = InetAddress.getLocalHost(); System.out.println("Local Machine Name is " + address.getHostName()); } catch (UnknownHostException e) { System.out.println("No Name for this machine."); } } }

  6. Elementary TCP socket TCP Server TCP Client ServerSocket() Well-known port Bind() Listen() Accept() Socket() Blocks until connections from client Connect() Connection establishment (TCP 3-way Handshake) Write() Read() Data (request) Process request Write() Data (reply) Read() Close() Close() Socket functions for elementary TCP client-server

  7. ServerSocket • ServerSocket(int port)           Creates a server socket, bound to the specifie port. Back

  8. Listen • accept() method of ServerSocket instance object returns Socket instance object.           Accept method listens for a connection to be made to this socket and accepts it. Back

  9. Read from a socket • DataInputStream • A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. • An application uses a data output stream to write data that can later be read by a data input stream. • readUTF() method return String data type • It reads from the stream in a representation of a Unicode character string encoded in Java modified UTF-8 format; this string of characters is then returned as a String. The details of the modified UTF-8 representation are exactly the same as for the readUTF method of DataInput.

  10. getInputStream() method of Socket class • getInputStream() method of class socket          Returns an input stream type (InputStream) for this socket. • If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel. Back

  11. Write to a socket • DataOutputStream class • A data output stream lets an application write primitive Java data types to an output stream in a portable way. • An application can then use a data input stream to read the data back in. • writeUTF() method writes a string to the underlying output stream using Java modified UTF-8 encoding in a machine-independent manner.

  12. getOutputStream() method of Socket class • getOutputStream() method of Socket class returns an output stream for this socket. • If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. Back

  13. Socket • Socket(InetAddress address, int port)           Creates a stream socket and connects it to the specified port number at the specified IP address. Back

  14. TCPServer import java.net.*; import java.io.*; public class TCPServer { public static void main (String args[]) { try{ int serverPort = 7896; // the server port ServerSocket listenSocket = new ServerSocket(serverPort); while(true) { Socket clientSocket = listenSocket.accept(); Connection c = new Connection(clientSocket); } } catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());} } } class Connection extends Thread { DataInputStream in; DataOutputStream out; Socket clientSocket; public Connection (Socket aClientSocket) { try { clientSocket = aClientSocket; in = new DataInputStream( clientSocket.getInputStream()); out =new DataOutputStream( clientSocket.getOutputStream()); this.start(); } catch(IOException e) {System.out.println("Connection:"+e.getMessage());} } public void run(){ try { // an echo server String data = in.readUTF(); // read a line of data from the stream out.writeUTF("Thats what i received from you :"+ data); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); } catch(IOException e) {System.out.println("readline:"+e.getMessage()); } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}} } }

  15. TCPClient import java.net.*; import java.io.*; public class TCPClient { public static void main (String args[]) { // arguments supply message and hostname Socket s = null; try{ intserverPort = 7896; s = new Socket("127.0.0.1", serverPort); DataInputStream in = new DataInputStream( s.getInputStream()); DataOutputStream out =new DataOutputStream( s.getOutputStream()); out.writeUTF("127.0.0.1"); // UTF is a string encoding see Sn. 4.4 String data = in.readUTF(); // read a line of data from the stream System.out.println("Received: "+ data) ; }catch (UnknownHostException e){System.out.println("Socket:"+e.getMessage()); }catch (EOFException e){System.out.println("EOF:"+e.getMessage()); }catch (IOException e){System.out.println("readline:"+e.getMessage()); }finally {if(s!=null) try {s.close();}catch (IOException e){System.out.println("close:"+e.getMessage());} } }

  16. References • http://www.ibiblio.org/java/books/jnp/javanetexamples/index.html • Java Docummentation. • http://users.encs.concordia.ca/~s423_2/CodeSample/Sockets/Text%20Book/

More Related