KEMBAR78
Socket Programming it-slideshares.blogspot.com
SOCKET PROGRAMMING
Networking Basics   Computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP) TCP When two applications want to communicate to each other reliably, they establish a connection and send data back and forth over that connection TCP provides a point-to-point channel for applications that require reliable communications. The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Telnet are all examples of applications that require a reliable communication channel  UDP The UDP protocol provides for communication that is not guaranteed between two applications on the network. UDP is not connection-based like TCP. Rather, it sends independent packets of data, called  datagrams , from one application to another
Understanding Ports
Communication between Applications Using Ports Local ports identify the application establishing a connection from other programs, allowing multiple TCP applications to run on the same machine.
Some Well-Known Port Services
Transmission Control Protocol TCP establishes a virtual connection to transmit data
TCP Programmning in Java ServerSocket server = new ServerSocket(port); Socket serverSoc = server.accept(); Socket clientSoc = new Socket(serverAdd,port);
TCP Programmning in Java InputStream inClient =    clientSoc.getInputStream(); OutputStream outClient =    clientSoc.getOutputStream(); InputStream inServer =    serverSoc.getInputStream();   OutputStream outServer =  serverSoc.getOutputStream(); Server Client stream
Socket Basics A socket is a connection between two hosts. It can perform seven basic operations: Connect to a remote machine Send data Receive data Close a connection Bind to a port Listen for incoming data Accept connections from remote machines on the bound port Java's Socket class, which is used by both  clients  and servers, has methods that correspond to the  first four of these operations . The  last three operations  are needed only by  servers , which wait for clients to connect to them. They are implemented by the  ServerSocket  class
Program using client socket The program creates a new socket with a  Socket( )  constructor. The socket  attempts to connect to the remote host . Once the connection is established, the local and remote hosts  get input  and  output streams  from the socket and use those streams to send data to each other. This connection is  full-duplex ; both hosts can send and receive data simultaneously. When the transmission of data is complete, one or both sides close the connection . Some protocols, such as HTTP 1.0, require the connection to be  closed after each request is serviced. Others, such as FTP, allow multiple requests to be processed in a single connection.
Socket Basics - Constructors public Socket(String host, int port) throws UnknownHostException, IOException This constructor creates a TCP socket to the specified port on the specified host and attempts to connect to the remote host. For example: try { Socket toOReilly = new  Socket("www.oreilly.com", 80); // send and receive data... } catch ( UnknownHostException e ) { System.err.println(e); } catch ( IOException e ) { System.err.println(e); }
LowPortScanner Program import  java.net.*; import  java.io.*; public   class  LowPortScanner { public   static   void  main(String[] args) { String host =  &quot;localhost&quot; ; if  (args. length  > 0)  host = args[0]; for  ( int  i = 1; i < 1024; i++) { try  {   System. out .print( &quot;Scanning on port : &quot; +i + &quot; ; &quot; );   Socket s =  new  Socket(host, i);   System. out .println( &quot;There is a server on port &quot;  + i +  &quot; of &quot; +host); }   catch  (UnknownHostException e) { System. out .println( &quot;The Server adress is unknown&quot; ); break ;   }   catch  (IOException e) { System. out .println( &quot;The Server is not found&quot; );   } }}}
Socket Basics - Constructor public Socket(InetAddress host, int port) throws IOException Like the previous constructor, this constructor creates a TCP socket to the specified port on the specified host and tries to connect. It differs by using an InetAddress object to specify the host rather than a hostname. It throws an IOException if it can't connect, but does not throw an UnknownHostException; if the host is unknown, you will find out when you create the InetAddress object. For example: try {   InetAddress  OReilly= InetAddress.getByName(&quot; www.oreilly.com &quot;); Socket OReillySocket = new  Socket(OReilly , 80); // send and receive data... } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }
Socket Basics - Constructor public Socket(String serverAdd, int serverPort, InetAddress interface, int localPort) throws IOException This constructor creates a socket to the specified port on the specified host and tries to connect. It connects  to  the host and port specified in the first two arguments. It connects  from  the local network interface and port specified by the last two arguments. The network interface may be either physical (e.g., a different Ethernet card) or virtual (a multihomed host). If is passed for the localPort argument, Java chooses a random available port between 1024 and 65,535. For example, if I were running a program on  metalab.unc.edu  and wanted to make sure that my connection went over its 100 megabit-per-second (Mbps) fiber-optic interface( fddisunsite.oit.unc.edu ) instead of the 10Mbps Ethernet interface ( helios.oit.unc.edu ), I would open a socket like this: InetAddress  fddi  =  InetAddress.getByName(&quot; fddisunsite.oit.unc.edu &quot;); Socket OReillySocket = new Socket (&quot;www.oreilly.com&quot;, 80 ,  fddi, 0 ); // work with the sockets...
Socket Basics - Constructor public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException This constructor is identical to the previous one except that the host to connect to is passed as an InetAddress, not a String. It creates a TCP socket to the specified port on the specified host from the specified interface and local port, and tries to connect. If it fails, it throws an IOException. For example: try{ InetAddress  metalab  =  InetAddress.getByName(&quot;metalab.unc.edu&quot;); InetAddress  oreilly  =  InetAddress.getByName(&quot;www.oreilly.com&quot;); Socket oreillySocket = new Socket( oreilly, 80 ,  metalab, 0 ); }
Getting Information About a  Socket public InetAddress getInetAddress( ) Given a Socket object, the getInetAddress( ) method tells you which remote host the Socket is connected to or, if the connection is now closed, which host the Socket was connected to when it was connected. For example: try {   Socket  theSocket  = new Socket(&quot;java.sun.com&quot;, 80); InetAddress  host  =  theSocket.getInetAddress( ); System.out.println(&quot;Connected to remote host &quot; +  host ); } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }
Getting Information About a  Socket public int getPort( ) The getPort( ) method tells you which port the Socket is (or was or will be) connected to on the remote host. For example: try { Socket theSocket = new Socket(&quot;java.sun.com&quot;, 80); int port = theSocket.getPort( ); System.out.println(&quot;Connected on  remote port  &quot; + port); } public int getLocalPort( ) There are two ends to a connection: the remote host and the local host. To find the port number for the local end of a connection, call getLocalPort( ). For example: try { Socket theSocket = new Socket(&quot;java.sun.com&quot;, 80, true); int localPort = theSocket.getLocalPort( ); System.out.println(&quot;Connecting from  local port  &quot; + localPort); }
Socket Basics – SocketInfo Program public   class  SocketInfo { public   static   void  main(String[] args) { String[] hostNames = { &quot;www.hcmuaf.edu.vn&quot; ,  &quot;mail.hcmuaf.edu&quot; , &quot;testweb.hcmuaf.edu.vn&quot; }; for  ( int  i = 0; i< hostNames. length ; i++){ try  { Socket theSocket =  new  Socket(hostNames[i], 80); System. out .println( &quot;Connected to &quot; + theSocket.getInetAddress( ) +  &quot; on port &quot;  + theSocket.getPort( )  +  &quot; from port &quot; + theSocket.getLocalPort( ) +  &quot; of &quot;   +  theSocket.getLocalAddress( )); } catch  (UnknownHostException e) { System. err .println( &quot;I can't find &quot;  + hostNames[i]); } catch  (SocketException e) { System. err .println( &quot;Could not connect to &quot;  + hostNames[i]); } catch  (IOException e) { System. err .println(e); } }}}
Getting Information About a  Socket public InputStream getInputStream( ) throws IOException The  getInputStream ( ) method returns an input stream that can read data from the socket into a program. You usually chain this  InputStream  to a  filter   stream  or  reader  that offers more functionality — DataInputStream  or  InputStreamReader , for example—before reading input. It's also extremely helpful to buffer the input by chaining it to a  BufferedInputStream  or a  BufferedReader  for performance reasons When reading data from the network, it's important to keep in mind that  not all protocols use ASCII  or even text.
Getting Information About a  Socket public OutputStream getOutputStream( ) throws IOException The  getOutputStream ( ) method returns a raw OutputStream for writing data from your application to the other end of the socket. You usually chain this stream to a more convenient class like  DataOutputStream  or  OutputStreamWriter  before using it. For performance reasons, it's a good idea to buffer it as well. The following example uses  getOutputStream ( ) and  getInputStream ( ) to implement a simple echo client. The user types input on the command-line, which is then sent to the server. The server echoes it back
Socket Basics - An Echo Client import java.net.*; import java.io.*; public class EchoClient { public static final int  ECHO_PORT = 7 ; public static void main(String[] args) { String hostname = &quot;localhost&quot;; PrintWriter out = null; BufferedReader networkIn = null; try { Socket theSocket = new Socket(hostname, ECHO_PORT); networkIn  = new BufferedReader( new InputStreamReader( theSocket.getInputStream() )); out  = new PrintWriter( theSocket.getOutputStream() ); BufferedReader  userIn  = new BufferedReader( new InputStreamReader( System.in )); System.out.println(&quot;Connected to echo server&quot;); System.out.println( networkIn.readLine() );
Socket Basics - An Echo Client while (true) { String theLine = userIn.readLine(); out.println(theLine);  out.flush(); System.out.println(networkIn.readLine()); if (theLine.equals(&quot;BYE&quot;)) break; } } // end try catch (IOException e) { System.err.println(e); } finally { try { if (networkIn != null)  networkIn.close(); if (out != null)  out.close(); } catch (IOException e) {} } } // end main }
Socket Basics - Closing the Socket public synchronized void close( ) throws IOException When you're through with a socket, you should call its close( ) method to disconnect. Ideally, you put this in a finally block so that the socket is closed whether or not an exception is thrown. The syntax is straightforward: Socket connection = null; try { Socket connection = new Socket(&quot;www.oreilly.com&quot;, 13); // interact with the socket }  // end try catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } finally { if (connection != null) connection.close( ); }
Socket Basics - Half-closed sockets When a client program sends a request to the server, the server needs to be able to determine when the end of the request occurs . For that reason, many Internet protocols (such as SMTP) are line-oriented. Other protocols contain a header that specifies the size of the request data. Otherwise, indicating the end of the request data is harder than writing data to a file. With a file, you'd just close the file at the end of the data. But  if you close a socket, then you immediately disconnect from the server . The  half-close  overcomes this problem .  You can close the output stream of a socket , thereby indicating to the server the end of the request data,  but keep the input stream open so that you can read the response . public void shutdownInput( ) throws IOException  public void shutdownOutput( ) throws IOException
Socket Basics - Half-closed sockets Socket connection = null; try { connection = new Socket(&quot;www.oreilly.com&quot;, 80); BufferedReader  reader  = new BufferedReader( new  InputStreamReader(socket.getInputStream())); Writer  out  = new OutputStreamWriter(connection.getOutputStream( ),  “UTF-8&quot;); out.write(&quot;GET / HTTP 1.0\r\n\r\n&quot;); out.flush( ); connection.shutdownOutput( ); // now socket is half closed;  read response data String line; while ((line =  reader.readLine() ) != null) . . . } catch (IOException e) {} finally { try { if (connection != null) connection.close( ); } catch (IOException e) {} }
Socket Basics - Socket timeouts public synchronized void setSoTimeout(int milliseconds) throws SocketException public synchronized int getSoTimeout( ) throws SocketException Normally  when you try to read data from a socket ,  the   read ( )  call blocks as long as necessary to get enough bytes . By setting  SO_TIMEOUT , you ensure that the call will not block for more than a fixed number of milliseconds. When the timeout expires, an  InterruptedException  is thrown, and you should be prepared to catch it. However, the socket is still connected. Although this  read ( ) call failed, you can try to read from the socket again. The next call may succeed. Socket s = new Socket (); s.setSoTimeout(10000) ; // time out after 10 seconds s.connect(….); Timeouts are given in milliseconds. Zero is interpreted as an infinite timeout, and isthe default value .
Socket Basics - Sockets for Servers The basic life cycle of a server is: A new  ServerSocket  is created on a particular port using a  ServerSocket()  constructor. The  ServerSocket listens for incoming connection  attempts on that port using its  accept ( ) method.  accept ( )  blocks until a client attempts to make a connection , at which point  accept ( ) returns a Socket object connecting the client and the server. Depending on the type of server, either the Socket's  getInputStream ( ) method,  getOutputStream ( ) method, or both are called to get input and output streams that communicate with the client. The server and the client interact according to an agreed-upon protocol  until it is time to close the connection. The server, the client, or both  close the connection . The server returns to step 2 and waits for the next connection .
Socket Basics - Sockets for Servers public ServerSocket(int port) throws IOException, BindException This constructor creates a server socket on the port specified by the argument. For example, to create a server socket that would be used by an HTTP server on port 80, you would write: try { ServerSocket httpd = new ServerSocket(80); }  catch (IOException e) { System.err.println(e); } The constructor throws an  IOException  (specifically, a  BindException ) if the socket cannot be created and bound to the requested port. An  IOException  when creating a  ServerSocket  almost always means one of two things. Either  another server socket is already using the requested port , or you're trying to connect to a  port from 1 to 1023 on Unix without root (superuser) privileges .
Socket Basics - LocalServerPortScanner import java.net.*; import java.io.*; public class LocalServerPortScanner { public static void main(String[] args) { for (int port = 1; port <= 1024; port++) { try { // the next line will fail and drop into the catch block if // there is already a server running on the port ServerSocket server = new ServerSocket(port); } catch (IOException e) { System.out.println(&quot;There is a server on port &quot; +  port + &quot;.&quot;); } // end try } // end for }}
Socket Basics - Sockets for Servers public Socket accept( ) throws IOException When server setup is done and you're ready to accept a connection, call the  ServerSocket's accept( )  method. This method &quot;blocks&quot;: it stops the flow of execution and waits until a client connects. When a client does connect, the  accept( )  method returns a Socket object. You use the streams returned by this Socket's  getInputStream( )  and  getOutputStream( )  methods to communicate with the client. For example: ServerSocket server = new ServerSocket(5776); while (true) { Socket connection = server.accept( ); PrintWriter out = new PrintWriter(connection.getOutputStream( ), TRUE ); out. println(&quot;You've connected to this server. Bye-bye now.&quot;); connection.close( ); }
ServerSocket - Socket Options The only socket option supported for server sockets is SO_TIMEOUT.  SO_TIMEOUT  is the amount of time, in milliseconds, that  accept ( )  waits for an incoming connection before throwing a  java.io.InterruptedIOException . If SO_TIMEOUT is 0, then  accept ( )  will never time out. The default is to never time out. public void setSoTimeout(int timeout) throws SocketException The  setSoTimeout ()  method sets the  SO_TIMEOUT  field for this server socket object. The countdown starts when  accept ()  is invoked. When the timeout expires,  accept ()  throws an  InterruptedIOException . You should set this option before calling  accept () ; you cannot change the timeout value while  accept ()  is waiting for a connection. The  timeout   argument must be greater than or equal to zero; if it isn't, the method throws an  IllegalArgumentException . For example:
ServerSocket - Socket Options try {   ServerSocket server = new ServerSocket(2048);   // block for no more than 30 seconds   server.setSoTimeout(30000);   try { Socket s = server.accept(); // handle the connection // ... } catch (InterruptedIOException e) {   System.err.println(&quot;No connection within 30 seconds&quot;); } finally {   server.close( ); } catch (IOException e) { System.err.println(&quot;Unexpected IOException:&quot; + e); } }
Implement EchoServer public   class  EchoServer { public   static   final   int   ECHO_PORT  = 7; public   static   void  main(String[] args) { try  { // establish server socket ServerSocket s =  new  ServerSocket( ECHO_PORT ); // wait for client connection Socket incoming = s.accept(); BufferedReader in =  new  BufferedReader ( new     InputStreamReader(incoming.getInputStream())); PrintWriter out =  new  PrintWriter (incoming.getOutputStream(),  true   /* autoFlush */ ); out.println( &quot;Welcome to ECHO SERVER! Enter BYE to    exit.&quot; );
Implement EchoServer //  echo client input boolean  done =  false ; while  (!done) { String line = in.readLine(); if  (line ==  null )  done =  true ; else  { out.println( &quot;Echo: &quot;  + line); if  (line.trim().equals( &quot;BYE&quot; )) done =  true ; } } incoming.close(); } catch  (Exception e) { e.printStackTrace(); } } }
Implement ThreadedEchoServer public   class  ThreadedEchoServer { public   static   final   int   ECHO_PORT  = 7; public   static   void  main(String[] args) { try  { int  i = 1; ServerSocket s =  new  ServerSocket( ECHO_PORT ); for  (; ; ) { Socket incoming = s.accept(); System. out .println( &quot;Connection number:&quot;  + i); System. out .println( &quot;Local Port: &quot; + incoming.getLocalPort()+ &quot;Foreign Port :&quot; + incoming.getPort()); Thread t =  new  ThreadedEchoHandler(incoming, i); t.start();  i++; } } catch  (Exception e) { e.printStackTrace(); }}}
Implement ThreadedEchoServer class  ThreadedEchoHandler  extends  Thread { private  Socket  incoming ; private   int   counter ; public  ThreadedEchoHandler(Socket i,  int  c) { incoming  = i; counter  = c; } public   void  run() { try  { BufferedReader in =  new  BufferedReader ( new   InputStreamReader( incoming .getInputStream())); PrintWriter out =  new  PrintWriter ( incoming .getOutputStream(),  true ); out.println( &quot;Welcom to Threaded ECHO SERVER! Enter BYE to exit.&quot; ); boolean  done =  false ; while  (!done) { String str = in.readLine(); if  (str ==  null )  done =  true ; else  { out.println( &quot;Echo (&quot;  +  counter  +  &quot;): &quot;  + str); if  (str.trim().equals( &quot;BYE&quot; ))  done =  true ; } } incoming .close(); } catch  (Exception e) { e.printStackTrace(); }}}
InetAddress class Usually, you don't have to worry too much about Internet addresses—the numerical host addresses that consist of four bytes such as 132.163.4.102. However, you can use the  InetAddress   class if you need to convert between host names and Internet addresses. The static  getByName   method returns an  InetAddress   object of a host. For example, InetAddress   address  =  InetAddress.getByName (&quot;time-A.timefreq.bldrdoc.gov&quot;) ; returns an  InetAddress   object that encapsulates the sequence of four bytes 132.163.4.102. You can access the bytes with the  getAddress   method. byte[]   addressBytes   =   address.getAddress() ;
InetAddress class Some host names with a lot of traffic correspond to multiple Internet addresses, to facilitate load balancing. You can get all hosts with the  getAllByName   method. InetAddress[] addresses = InetAddress. getAllByName(host); Finally, you sometimes need the address of the local host. If you simply ask for the address of  localhost , you always get the address 127.0.0.1, which isn't very useful. Instead, use the static  getLocalHost   method to get the address of your local host. InetAddress   address   =   InetAddress .getLocalHost();
InetAddress class public String getHostName()   Gets the host name for this IP address. If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service.  public String getCanonicalHostName()  Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration.  public byte[] getAddress()   Returns the raw IP address of this InetAddress object. The result is in network byte order: the highest order byte of the address is in getAddress()[0].
InetAddress class public String getHostAddress()   Returns the IP address string in textual presentation for example &quot;132.163.4.102&quot;. public String toString()  Converts this IP address to a String. The string returned is of the form: hostname / literal IP address. If the host name is unresolved, no reverse name service loopup is performed. The hostname part will be represented by an empty string.  public static InetAddress getByName(String host)  throws UnknownHostException  Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as &quot;java.sun.com&quot;, or a textual representation of its IP address.
InetAddress class public static InetAddress[] getAllByName(String host) throws UnknownHostException   Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system. The host name can either be a machine name, such as &quot;java.sun.com&quot;, or a textual representation of its IP address. public static InetAddress getByAddress(byte[] addr) throws UnknownHostException  Returns an InetAddress object given the raw IP address . The argument is in network byte order: the highest order byte of the address is in getAddress()[0]. public static InetAddress getLocalHost() throws UnknownHostException   Returns the local host.
NSLookup public   class  NSLookup { public   static   void  main(String[] args) { String hostName =  &quot;localhost&quot; ; String hostNameIP =  &quot;127.0.0.1&quot; ; InetAddress add; try { add = InetAddress. getByName (hostName); System. out .println( &quot;DNS host name: &quot; +add.getCanonicalHostName()); System. out .println( &quot;IP Address: &quot; +add.getHostAddress()); add = InetAddress. getByName (hostNameIP); System. out .println( &quot;DNS host name: &quot; +add.getCanonicalHostName()); System. out .println( &quot;IP Address: &quot; +add.getHostAddress()); System. out .println( &quot;InetAddress toString: &quot; +add); InetAddress[] addresses = InetAddress. getAllByName (hostName); for  ( int  i = 0; i < addresses. length ; i++)  System. out .println(addresses[i]); }  catch (UnknownHostException e){  System. out .println( &quot;The Address not exist&quot; ); } }}
SMTP & Sending E-Mail
Simple Mail Transfer Protocol - RFC 821 To send e-mail, you make a socket connection to port 25 , the SMTP port. SMTP is the Simple Mail Transport Protocol that describes the format for e-mail messages. Open a socket to your host. Socket s = new Socket(&quot;mail.yourserver.com&quot;, 25);   PrintWriter out = new PrintWriter(s.getOutputStream()); Send the following information to the print stream: HELO   sending host # Domain name MAIL   FROM: < sender email address > RCPT   TO: < recipient email address > DATA mail message (any number of lines) . QUIT
Sending E-Mail private BufferedReader in; private PrintWriter out; private JTextField from; private JTextField to; private JTextArea message; ………………………… . public void sendMail() { try { Socket s = new Socket(smtpServer.getText(), 25); out = new PrintWriter(s.getOutputStream()); in = new BufferedReader(new InputStreamReader(s.getInputStream())); String hostName  =  InetAddress.getLocalHost().getHostName(); receive(); send(&quot;HELO &quot; + hostName); receive()
Sending E-Mail send(&quot;MAIL FROM: <&quot; + from.getText() +&quot;>&quot;); receive(); send(&quot;RCPT TO: <&quot; + to.getText() +&quot;>&quot;); receive(); send(&quot;DATA&quot;); receive(); StringTokenizer tokenizer = new StringTokenizer( message.getText(), &quot;\n&quot;); while (tokenizer.hasMoreTokens()) send(tokenizer.nextToken()); send(&quot;.&quot;); receive();   s.close(); } catch (IOException exception) { ………………………… . } }
Sending E-Mail public void send(String s)  throws IOException { …………………… . out.print(s); out.print(&quot;\r\n&quot;); out.flush(); } public void receive()  throws IOException { String line = in.readLine(); if (line != null) { ………………… ..  } }
Receive E-Mail – POP3 (RFC 1939) // Login by sending USER and PASS commands USER  username PASS  password // Get mail count from server .... STAT   message_Number , meassage_Size RETR  meassage_Number  … message body . QUIT

Socket Programming it-slideshares.blogspot.com

  • 1.
  • 2.
    Networking Basics Computers running on the Internet communicate to each other using either the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP) TCP When two applications want to communicate to each other reliably, they establish a connection and send data back and forth over that connection TCP provides a point-to-point channel for applications that require reliable communications. The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Telnet are all examples of applications that require a reliable communication channel UDP The UDP protocol provides for communication that is not guaranteed between two applications on the network. UDP is not connection-based like TCP. Rather, it sends independent packets of data, called datagrams , from one application to another
  • 3.
  • 4.
    Communication between ApplicationsUsing Ports Local ports identify the application establishing a connection from other programs, allowing multiple TCP applications to run on the same machine.
  • 5.
  • 6.
    Transmission Control ProtocolTCP establishes a virtual connection to transmit data
  • 7.
    TCP Programmning inJava ServerSocket server = new ServerSocket(port); Socket serverSoc = server.accept(); Socket clientSoc = new Socket(serverAdd,port);
  • 8.
    TCP Programmning inJava InputStream inClient = clientSoc.getInputStream(); OutputStream outClient = clientSoc.getOutputStream(); InputStream inServer = serverSoc.getInputStream(); OutputStream outServer = serverSoc.getOutputStream(); Server Client stream
  • 9.
    Socket Basics Asocket is a connection between two hosts. It can perform seven basic operations: Connect to a remote machine Send data Receive data Close a connection Bind to a port Listen for incoming data Accept connections from remote machines on the bound port Java's Socket class, which is used by both clients and servers, has methods that correspond to the first four of these operations . The last three operations are needed only by servers , which wait for clients to connect to them. They are implemented by the ServerSocket class
  • 10.
    Program using clientsocket The program creates a new socket with a Socket( ) constructor. The socket attempts to connect to the remote host . Once the connection is established, the local and remote hosts get input and output streams from the socket and use those streams to send data to each other. This connection is full-duplex ; both hosts can send and receive data simultaneously. When the transmission of data is complete, one or both sides close the connection . Some protocols, such as HTTP 1.0, require the connection to be closed after each request is serviced. Others, such as FTP, allow multiple requests to be processed in a single connection.
  • 11.
    Socket Basics -Constructors public Socket(String host, int port) throws UnknownHostException, IOException This constructor creates a TCP socket to the specified port on the specified host and attempts to connect to the remote host. For example: try { Socket toOReilly = new Socket(&quot;www.oreilly.com&quot;, 80); // send and receive data... } catch ( UnknownHostException e ) { System.err.println(e); } catch ( IOException e ) { System.err.println(e); }
  • 12.
    LowPortScanner Program import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = &quot;localhost&quot; ; if (args. length > 0) host = args[0]; for ( int i = 1; i < 1024; i++) { try { System. out .print( &quot;Scanning on port : &quot; +i + &quot; ; &quot; ); Socket s = new Socket(host, i); System. out .println( &quot;There is a server on port &quot; + i + &quot; of &quot; +host); } catch (UnknownHostException e) { System. out .println( &quot;The Server adress is unknown&quot; ); break ; } catch (IOException e) { System. out .println( &quot;The Server is not found&quot; ); } }}}
  • 13.
    Socket Basics -Constructor public Socket(InetAddress host, int port) throws IOException Like the previous constructor, this constructor creates a TCP socket to the specified port on the specified host and tries to connect. It differs by using an InetAddress object to specify the host rather than a hostname. It throws an IOException if it can't connect, but does not throw an UnknownHostException; if the host is unknown, you will find out when you create the InetAddress object. For example: try { InetAddress OReilly= InetAddress.getByName(&quot; www.oreilly.com &quot;); Socket OReillySocket = new Socket(OReilly , 80); // send and receive data... } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }
  • 14.
    Socket Basics -Constructor public Socket(String serverAdd, int serverPort, InetAddress interface, int localPort) throws IOException This constructor creates a socket to the specified port on the specified host and tries to connect. It connects to the host and port specified in the first two arguments. It connects from the local network interface and port specified by the last two arguments. The network interface may be either physical (e.g., a different Ethernet card) or virtual (a multihomed host). If is passed for the localPort argument, Java chooses a random available port between 1024 and 65,535. For example, if I were running a program on metalab.unc.edu and wanted to make sure that my connection went over its 100 megabit-per-second (Mbps) fiber-optic interface( fddisunsite.oit.unc.edu ) instead of the 10Mbps Ethernet interface ( helios.oit.unc.edu ), I would open a socket like this: InetAddress fddi = InetAddress.getByName(&quot; fddisunsite.oit.unc.edu &quot;); Socket OReillySocket = new Socket (&quot;www.oreilly.com&quot;, 80 , fddi, 0 ); // work with the sockets...
  • 15.
    Socket Basics -Constructor public Socket(InetAddress host, int port, InetAddress interface, int localPort) throws IOException This constructor is identical to the previous one except that the host to connect to is passed as an InetAddress, not a String. It creates a TCP socket to the specified port on the specified host from the specified interface and local port, and tries to connect. If it fails, it throws an IOException. For example: try{ InetAddress metalab = InetAddress.getByName(&quot;metalab.unc.edu&quot;); InetAddress oreilly = InetAddress.getByName(&quot;www.oreilly.com&quot;); Socket oreillySocket = new Socket( oreilly, 80 , metalab, 0 ); }
  • 16.
    Getting Information Abouta Socket public InetAddress getInetAddress( ) Given a Socket object, the getInetAddress( ) method tells you which remote host the Socket is connected to or, if the connection is now closed, which host the Socket was connected to when it was connected. For example: try { Socket theSocket = new Socket(&quot;java.sun.com&quot;, 80); InetAddress host = theSocket.getInetAddress( ); System.out.println(&quot;Connected to remote host &quot; + host ); } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); }
  • 17.
    Getting Information Abouta Socket public int getPort( ) The getPort( ) method tells you which port the Socket is (or was or will be) connected to on the remote host. For example: try { Socket theSocket = new Socket(&quot;java.sun.com&quot;, 80); int port = theSocket.getPort( ); System.out.println(&quot;Connected on remote port &quot; + port); } public int getLocalPort( ) There are two ends to a connection: the remote host and the local host. To find the port number for the local end of a connection, call getLocalPort( ). For example: try { Socket theSocket = new Socket(&quot;java.sun.com&quot;, 80, true); int localPort = theSocket.getLocalPort( ); System.out.println(&quot;Connecting from local port &quot; + localPort); }
  • 18.
    Socket Basics –SocketInfo Program public class SocketInfo { public static void main(String[] args) { String[] hostNames = { &quot;www.hcmuaf.edu.vn&quot; , &quot;mail.hcmuaf.edu&quot; , &quot;testweb.hcmuaf.edu.vn&quot; }; for ( int i = 0; i< hostNames. length ; i++){ try { Socket theSocket = new Socket(hostNames[i], 80); System. out .println( &quot;Connected to &quot; + theSocket.getInetAddress( ) + &quot; on port &quot; + theSocket.getPort( ) + &quot; from port &quot; + theSocket.getLocalPort( ) + &quot; of &quot; + theSocket.getLocalAddress( )); } catch (UnknownHostException e) { System. err .println( &quot;I can't find &quot; + hostNames[i]); } catch (SocketException e) { System. err .println( &quot;Could not connect to &quot; + hostNames[i]); } catch (IOException e) { System. err .println(e); } }}}
  • 19.
    Getting Information Abouta Socket public InputStream getInputStream( ) throws IOException The getInputStream ( ) method returns an input stream that can read data from the socket into a program. You usually chain this InputStream to a filter stream or reader that offers more functionality — DataInputStream or InputStreamReader , for example—before reading input. It's also extremely helpful to buffer the input by chaining it to a BufferedInputStream or a BufferedReader for performance reasons When reading data from the network, it's important to keep in mind that not all protocols use ASCII or even text.
  • 20.
    Getting Information Abouta Socket public OutputStream getOutputStream( ) throws IOException The getOutputStream ( ) method returns a raw OutputStream for writing data from your application to the other end of the socket. You usually chain this stream to a more convenient class like DataOutputStream or OutputStreamWriter before using it. For performance reasons, it's a good idea to buffer it as well. The following example uses getOutputStream ( ) and getInputStream ( ) to implement a simple echo client. The user types input on the command-line, which is then sent to the server. The server echoes it back
  • 21.
    Socket Basics -An Echo Client import java.net.*; import java.io.*; public class EchoClient { public static final int ECHO_PORT = 7 ; public static void main(String[] args) { String hostname = &quot;localhost&quot;; PrintWriter out = null; BufferedReader networkIn = null; try { Socket theSocket = new Socket(hostname, ECHO_PORT); networkIn = new BufferedReader( new InputStreamReader( theSocket.getInputStream() )); out = new PrintWriter( theSocket.getOutputStream() ); BufferedReader userIn = new BufferedReader( new InputStreamReader( System.in )); System.out.println(&quot;Connected to echo server&quot;); System.out.println( networkIn.readLine() );
  • 22.
    Socket Basics -An Echo Client while (true) { String theLine = userIn.readLine(); out.println(theLine); out.flush(); System.out.println(networkIn.readLine()); if (theLine.equals(&quot;BYE&quot;)) break; } } // end try catch (IOException e) { System.err.println(e); } finally { try { if (networkIn != null) networkIn.close(); if (out != null) out.close(); } catch (IOException e) {} } } // end main }
  • 23.
    Socket Basics -Closing the Socket public synchronized void close( ) throws IOException When you're through with a socket, you should call its close( ) method to disconnect. Ideally, you put this in a finally block so that the socket is closed whether or not an exception is thrown. The syntax is straightforward: Socket connection = null; try { Socket connection = new Socket(&quot;www.oreilly.com&quot;, 13); // interact with the socket } // end try catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } finally { if (connection != null) connection.close( ); }
  • 24.
    Socket Basics -Half-closed sockets When a client program sends a request to the server, the server needs to be able to determine when the end of the request occurs . For that reason, many Internet protocols (such as SMTP) are line-oriented. Other protocols contain a header that specifies the size of the request data. Otherwise, indicating the end of the request data is harder than writing data to a file. With a file, you'd just close the file at the end of the data. But if you close a socket, then you immediately disconnect from the server . The half-close overcomes this problem . You can close the output stream of a socket , thereby indicating to the server the end of the request data, but keep the input stream open so that you can read the response . public void shutdownInput( ) throws IOException public void shutdownOutput( ) throws IOException
  • 25.
    Socket Basics -Half-closed sockets Socket connection = null; try { connection = new Socket(&quot;www.oreilly.com&quot;, 80); BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); Writer out = new OutputStreamWriter(connection.getOutputStream( ), “UTF-8&quot;); out.write(&quot;GET / HTTP 1.0\r\n\r\n&quot;); out.flush( ); connection.shutdownOutput( ); // now socket is half closed; read response data String line; while ((line = reader.readLine() ) != null) . . . } catch (IOException e) {} finally { try { if (connection != null) connection.close( ); } catch (IOException e) {} }
  • 26.
    Socket Basics -Socket timeouts public synchronized void setSoTimeout(int milliseconds) throws SocketException public synchronized int getSoTimeout( ) throws SocketException Normally when you try to read data from a socket , the read ( ) call blocks as long as necessary to get enough bytes . By setting SO_TIMEOUT , you ensure that the call will not block for more than a fixed number of milliseconds. When the timeout expires, an InterruptedException is thrown, and you should be prepared to catch it. However, the socket is still connected. Although this read ( ) call failed, you can try to read from the socket again. The next call may succeed. Socket s = new Socket (); s.setSoTimeout(10000) ; // time out after 10 seconds s.connect(….); Timeouts are given in milliseconds. Zero is interpreted as an infinite timeout, and isthe default value .
  • 27.
    Socket Basics -Sockets for Servers The basic life cycle of a server is: A new ServerSocket is created on a particular port using a ServerSocket() constructor. The ServerSocket listens for incoming connection attempts on that port using its accept ( ) method. accept ( ) blocks until a client attempts to make a connection , at which point accept ( ) returns a Socket object connecting the client and the server. Depending on the type of server, either the Socket's getInputStream ( ) method, getOutputStream ( ) method, or both are called to get input and output streams that communicate with the client. The server and the client interact according to an agreed-upon protocol until it is time to close the connection. The server, the client, or both close the connection . The server returns to step 2 and waits for the next connection .
  • 28.
    Socket Basics -Sockets for Servers public ServerSocket(int port) throws IOException, BindException This constructor creates a server socket on the port specified by the argument. For example, to create a server socket that would be used by an HTTP server on port 80, you would write: try { ServerSocket httpd = new ServerSocket(80); } catch (IOException e) { System.err.println(e); } The constructor throws an IOException (specifically, a BindException ) if the socket cannot be created and bound to the requested port. An IOException when creating a ServerSocket almost always means one of two things. Either another server socket is already using the requested port , or you're trying to connect to a port from 1 to 1023 on Unix without root (superuser) privileges .
  • 29.
    Socket Basics -LocalServerPortScanner import java.net.*; import java.io.*; public class LocalServerPortScanner { public static void main(String[] args) { for (int port = 1; port <= 1024; port++) { try { // the next line will fail and drop into the catch block if // there is already a server running on the port ServerSocket server = new ServerSocket(port); } catch (IOException e) { System.out.println(&quot;There is a server on port &quot; + port + &quot;.&quot;); } // end try } // end for }}
  • 30.
    Socket Basics -Sockets for Servers public Socket accept( ) throws IOException When server setup is done and you're ready to accept a connection, call the ServerSocket's accept( ) method. This method &quot;blocks&quot;: it stops the flow of execution and waits until a client connects. When a client does connect, the accept( ) method returns a Socket object. You use the streams returned by this Socket's getInputStream( ) and getOutputStream( ) methods to communicate with the client. For example: ServerSocket server = new ServerSocket(5776); while (true) { Socket connection = server.accept( ); PrintWriter out = new PrintWriter(connection.getOutputStream( ), TRUE ); out. println(&quot;You've connected to this server. Bye-bye now.&quot;); connection.close( ); }
  • 31.
    ServerSocket - SocketOptions The only socket option supported for server sockets is SO_TIMEOUT. SO_TIMEOUT is the amount of time, in milliseconds, that accept ( ) waits for an incoming connection before throwing a java.io.InterruptedIOException . If SO_TIMEOUT is 0, then accept ( ) will never time out. The default is to never time out. public void setSoTimeout(int timeout) throws SocketException The setSoTimeout () method sets the SO_TIMEOUT field for this server socket object. The countdown starts when accept () is invoked. When the timeout expires, accept () throws an InterruptedIOException . You should set this option before calling accept () ; you cannot change the timeout value while accept () is waiting for a connection. The timeout argument must be greater than or equal to zero; if it isn't, the method throws an IllegalArgumentException . For example:
  • 32.
    ServerSocket - SocketOptions try { ServerSocket server = new ServerSocket(2048); // block for no more than 30 seconds server.setSoTimeout(30000); try { Socket s = server.accept(); // handle the connection // ... } catch (InterruptedIOException e) { System.err.println(&quot;No connection within 30 seconds&quot;); } finally { server.close( ); } catch (IOException e) { System.err.println(&quot;Unexpected IOException:&quot; + e); } }
  • 33.
    Implement EchoServer public class EchoServer { public static final int ECHO_PORT = 7; public static void main(String[] args) { try { // establish server socket ServerSocket s = new ServerSocket( ECHO_PORT ); // wait for client connection Socket incoming = s.accept(); BufferedReader in = new BufferedReader ( new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */ ); out.println( &quot;Welcome to ECHO SERVER! Enter BYE to exit.&quot; );
  • 34.
    Implement EchoServer // echo client input boolean done = false ; while (!done) { String line = in.readLine(); if (line == null ) done = true ; else { out.println( &quot;Echo: &quot; + line); if (line.trim().equals( &quot;BYE&quot; )) done = true ; } } incoming.close(); } catch (Exception e) { e.printStackTrace(); } } }
  • 35.
    Implement ThreadedEchoServer public class ThreadedEchoServer { public static final int ECHO_PORT = 7; public static void main(String[] args) { try { int i = 1; ServerSocket s = new ServerSocket( ECHO_PORT ); for (; ; ) { Socket incoming = s.accept(); System. out .println( &quot;Connection number:&quot; + i); System. out .println( &quot;Local Port: &quot; + incoming.getLocalPort()+ &quot;Foreign Port :&quot; + incoming.getPort()); Thread t = new ThreadedEchoHandler(incoming, i); t.start(); i++; } } catch (Exception e) { e.printStackTrace(); }}}
  • 36.
    Implement ThreadedEchoServer class ThreadedEchoHandler extends Thread { private Socket incoming ; private int counter ; public ThreadedEchoHandler(Socket i, int c) { incoming = i; counter = c; } public void run() { try { BufferedReader in = new BufferedReader ( new InputStreamReader( incoming .getInputStream())); PrintWriter out = new PrintWriter ( incoming .getOutputStream(), true ); out.println( &quot;Welcom to Threaded ECHO SERVER! Enter BYE to exit.&quot; ); boolean done = false ; while (!done) { String str = in.readLine(); if (str == null ) done = true ; else { out.println( &quot;Echo (&quot; + counter + &quot;): &quot; + str); if (str.trim().equals( &quot;BYE&quot; )) done = true ; } } incoming .close(); } catch (Exception e) { e.printStackTrace(); }}}
  • 37.
    InetAddress class Usually,you don't have to worry too much about Internet addresses—the numerical host addresses that consist of four bytes such as 132.163.4.102. However, you can use the InetAddress class if you need to convert between host names and Internet addresses. The static getByName method returns an InetAddress object of a host. For example, InetAddress address = InetAddress.getByName (&quot;time-A.timefreq.bldrdoc.gov&quot;) ; returns an InetAddress object that encapsulates the sequence of four bytes 132.163.4.102. You can access the bytes with the getAddress method. byte[] addressBytes = address.getAddress() ;
  • 38.
    InetAddress class Somehost names with a lot of traffic correspond to multiple Internet addresses, to facilitate load balancing. You can get all hosts with the getAllByName method. InetAddress[] addresses = InetAddress. getAllByName(host); Finally, you sometimes need the address of the local host. If you simply ask for the address of localhost , you always get the address 127.0.0.1, which isn't very useful. Instead, use the static getLocalHost method to get the address of your local host. InetAddress address = InetAddress .getLocalHost();
  • 39.
    InetAddress class publicString getHostName() Gets the host name for this IP address. If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. public String getCanonicalHostName() Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration. public byte[] getAddress() Returns the raw IP address of this InetAddress object. The result is in network byte order: the highest order byte of the address is in getAddress()[0].
  • 40.
    InetAddress class publicString getHostAddress() Returns the IP address string in textual presentation for example &quot;132.163.4.102&quot;. public String toString() Converts this IP address to a String. The string returned is of the form: hostname / literal IP address. If the host name is unresolved, no reverse name service loopup is performed. The hostname part will be represented by an empty string. public static InetAddress getByName(String host) throws UnknownHostException Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as &quot;java.sun.com&quot;, or a textual representation of its IP address.
  • 41.
    InetAddress class publicstatic InetAddress[] getAllByName(String host) throws UnknownHostException Given the name of a host, returns an array of its IP addresses, based on the configured name service on the system. The host name can either be a machine name, such as &quot;java.sun.com&quot;, or a textual representation of its IP address. public static InetAddress getByAddress(byte[] addr) throws UnknownHostException Returns an InetAddress object given the raw IP address . The argument is in network byte order: the highest order byte of the address is in getAddress()[0]. public static InetAddress getLocalHost() throws UnknownHostException Returns the local host.
  • 42.
    NSLookup public class NSLookup { public static void main(String[] args) { String hostName = &quot;localhost&quot; ; String hostNameIP = &quot;127.0.0.1&quot; ; InetAddress add; try { add = InetAddress. getByName (hostName); System. out .println( &quot;DNS host name: &quot; +add.getCanonicalHostName()); System. out .println( &quot;IP Address: &quot; +add.getHostAddress()); add = InetAddress. getByName (hostNameIP); System. out .println( &quot;DNS host name: &quot; +add.getCanonicalHostName()); System. out .println( &quot;IP Address: &quot; +add.getHostAddress()); System. out .println( &quot;InetAddress toString: &quot; +add); InetAddress[] addresses = InetAddress. getAllByName (hostName); for ( int i = 0; i < addresses. length ; i++) System. out .println(addresses[i]); } catch (UnknownHostException e){ System. out .println( &quot;The Address not exist&quot; ); } }}
  • 43.
  • 44.
    Simple Mail TransferProtocol - RFC 821 To send e-mail, you make a socket connection to port 25 , the SMTP port. SMTP is the Simple Mail Transport Protocol that describes the format for e-mail messages. Open a socket to your host. Socket s = new Socket(&quot;mail.yourserver.com&quot;, 25); PrintWriter out = new PrintWriter(s.getOutputStream()); Send the following information to the print stream: HELO sending host # Domain name MAIL FROM: < sender email address > RCPT TO: < recipient email address > DATA mail message (any number of lines) . QUIT
  • 45.
    Sending E-Mail privateBufferedReader in; private PrintWriter out; private JTextField from; private JTextField to; private JTextArea message; ………………………… . public void sendMail() { try { Socket s = new Socket(smtpServer.getText(), 25); out = new PrintWriter(s.getOutputStream()); in = new BufferedReader(new InputStreamReader(s.getInputStream())); String hostName = InetAddress.getLocalHost().getHostName(); receive(); send(&quot;HELO &quot; + hostName); receive()
  • 46.
    Sending E-Mail send(&quot;MAILFROM: <&quot; + from.getText() +&quot;>&quot;); receive(); send(&quot;RCPT TO: <&quot; + to.getText() +&quot;>&quot;); receive(); send(&quot;DATA&quot;); receive(); StringTokenizer tokenizer = new StringTokenizer( message.getText(), &quot;\n&quot;); while (tokenizer.hasMoreTokens()) send(tokenizer.nextToken()); send(&quot;.&quot;); receive(); s.close(); } catch (IOException exception) { ………………………… . } }
  • 47.
    Sending E-Mail publicvoid send(String s) throws IOException { …………………… . out.print(s); out.print(&quot;\r\n&quot;); out.flush(); } public void receive() throws IOException { String line = in.readLine(); if (line != null) { ………………… .. } }
  • 48.
    Receive E-Mail –POP3 (RFC 1939) // Login by sending USER and PASS commands USER username PASS password // Get mail count from server .... STAT message_Number , meassage_Size RETR meassage_Number … message body . QUIT