Socket programming 1 get By Name import java
Socket programming 1
get. By. Name import java. net. *; public class Get. Host. Name { public static void main (String args[]) { String host = "www. concordia. ca"; try { Inet. Address address = Inet. Address. get. By. Name(host); System. out. println(address); } catch (Unknown. Host. Exception e) { System. out. println("Could not find "+ host); } } }
get. By. Name import java. net. *; public class Get. Name. By. Address { public static void main (String args[]) { try { Inet. Address address = Inet. Address. get. By. Name("132. 205. 7. 63"); System. out. println(address); } catch (Unknown. Host. Exception e) { System. out. println("Could not find 132. 205. 7. 63"); } } }
Local. Machine. Address import java. net. *; public class Local. Machine. Address { public static void main (String args[]) { try { Inet. Address address = Inet. Address. get. Local. Host(); System. out. println(address); } catch (Unknown. Host. Exception e) { System. out. println("Could not find this computer's address. "); } } }
Local machine name import java. net. *; public class Local. Machine. Name { public static void main (String args[]) { try { Inet. Address address = Inet. Address. get. Local. Host(); System. out. println("Local Machine Name is " + address. get. Host. Name()); } catch (Unknown. Host. Exception e) { System. out. println("No Name for this machine. "); } } }
Elementary TCP socket TCP Server TCP Client Server. Socket() Well-known port Bind() Listen() Accept() Socket() Connect() Write() Connection establishment (TCP 3 -way Handshake) Data (request) Blocks until connections from client Read() Process request Read() Close() Data (reply) Write() Close() Socket functions for elementary TCP client-server
Server. Socket • Server. Socket(int port) Creates a server socket, bound to the specifie port. Back
Listen • accept() method of Server. Socket instance object returns Socket instance object. Accept method listens for a connection to be made to this socket and accepts it. Back
Read from a socket • Data. Input. Stream – A data input stream lets an application read primitive Java data types from an underlying input stream in a machineindependent way. – An application uses a data output stream to write data that can later be read by a data input stream. – read. UTF() 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 read. UTF method of Data. Input.
get. Input. Stream() method of Socket class • get. Input. Stream() method of class socket Returns an input stream type (Input. Stream) for this socket. • If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel. Back
Write to a socket • Data. Output. Stream 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. – write. UTF() method writes a string to the underlying output stream using Java modified UTF -8 encoding in a machine-independent manner.
get. Output. Stream() method of Socket class • get. Output. Stream() 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
Socket • Socket(Inet. Address address, int port) Creates a stream socket and connects it to the specified port number at the specified IP address. Back
TCPServer import java. net. *; import java. io. *; public class TCPServer { public static void main (String args[]) { try{ int server. Port = 7896; // the server port Server. Socket listen. Socket = new Server. Socket(server. Port); while(true) { Socket client. Socket = listen. Socket. accept(); Connection c = new Connection(client. Socket); } } catch(IOException e) {System. out. println("Listen socket: "+e. get. Message()); } } } class Connection extends Thread { Data. Input. Stream in; Data. Output. Stream out; Socket client. Socket; public Connection (Socket a. Client. Socket) { try { client. Socket = a. Client. Socket; in = new Data. Input. Stream( client. Socket. get. Input. Stream()); out =new Data. Output. Stream( client. Socket. get. Output. Stream()); this. start(); } catch(IOException e) {System. out. println("Connection: "+e. get. Message()); } } public void run(){ try { // an echo server String data = in. read. UTF(); // read a line of data from the stream out. write. UTF("Thats what i received from you : "+ data); }catch (EOFException e){System. out. println("EOF: "+e. get. Message()); } catch(IOException e) {System. out. println("readline: "+e. get. Message()); } finally{ try {client. Socket. close(); }catch (IOException e){/*close failed*/}} } }
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{ int server. Port = 7896; s = new Socket("127. 0. 0. 1", server. Port); Data. Input. Stream in = new Data. Input. Stream( s. get. Input. Stream()); Data. Output. Stream out =new Data. Output. Stream( s. get. Output. Stream()); out. write. UTF("127. 0. 0. 1"); // UTF is a string encoding see Sn. 4. 4 String data = in. read. UTF(); // read a line of data from the stream System. out. println("Received: "+ data) ; } } }catch (Unknown. Host. Exception e){System. out. println("Socket: "+e. get. Message()); }catch (EOFException e){System. out. println("EOF: "+e. get. Message()); }catch (IOException e){System. out. println("readline: "+e. get. Message()); }finally {if(s!=null) try {s. close(); }catch (IOException e){System. out. println("close: "+e. get. Message()); }
References • http: //www. ibiblio. org/java/books/jnp/javane texamples/index. html • Java Docummentation. • http: //users. encs. concordia. ca/~s 423_2/Code Sample/Sockets/Text%20 Book/
- Slides: 16