Implement client-server communication using TCP

Implement client-server communication using TCP

Introduction:
                       TCP provides the service of exchanging data directly between two network hosts, whereas IP handles addressing and routing message across one or more networks. In particular, TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer. TCP is the protocol that major Internet applications rely on, applications such as the World Wide Web, e-mail, and file transfer.
                       TCP connection is managed by an operating system through a programming interface that represents the local end-point for communications, the Internet socket. During the lifetime of a TCP connection it undergoes a series of state changes:


  1. LISTEN : In case of a server, waiting for a connection request from any remote client.
  2. SYN-SENT : waiting for the remote peer to send back a TCP segment with the SYN and ACK flags set. (usually set by TCP clients)
  3. SYN-RECEIVED : waiting for the remote peer to send back an acknowledgment after having sent back a connection acknowledgment to the remote peer. (usually set by TCP servers)
  4. ESTABLISHED : the port is ready to receive/send data from/to the remote peer.
  5. FIN-WAIT-1
  6. FIN-WAIT-2
  7. CLOSE-WAIT
  8. CLOSING
  9. LAST-ACK
  10. TIME-WAIT : represents waiting for enough time to pass to be sure the remote peer received the acknowledgment of its connection termination request. According to RFC 793 a connection can stay in TIME-WAIT for a maximum of four minutes.
  11. CLOSED

TCP uses the notion of port numbers to identify sending and receiving application end-points on a host, or Internet sockets. Each side of a TCP connection has an associated 16-bit unsigned port number (0-65535) reserved by the sending or receiving application. Arriving TCP data packets are identified as belonging to a specific TCP connection by its sockets, that is, the combination of source host address, source port, destination host address, and destination port. This means that a server computer can provide several clients with several services simultaneously, as long as a client takes care of initiating any simultaneous connections to one destination port from different source ports.


TCP Server:
import java.net.*;
import java.io.*;

public class TCPServer  {
   public static void main(String a[])
   {
      Socket s=null;
      try
      {
int serverPort=8117;
ServerSocket listenSocket=new ServerSocket(serverPort);
while(true)
{
   Socket clientSocket=listenSocket.accept();
   Connection c=new Connection(clientSocket);
}
      }
      catch(IOException e)
      {   System.out.println("Listen:"+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  {
String data=in.readUTF();
out.writeUTF(data);
      }
      catch(EOFException e)
      {   System.out.println("EOF:"+e.getMessage());   }
      catch(IOException e)
      {   System.out.println("IO:"+e.getMessage());   }
      finally
      {
try
{
   clientSocket.close();
}
catch(IOException e)
{   System.out.println("IO:"+e.getMessage());   }
      }
   }
}
TCP Client:

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 serverPort = 8117;
                                    s = new Socket(args[1], serverPort);   
                                    DataInputStream in = new DataInputStream( s.getInputStream());
                                    DataOutputStream out =new DataOutputStream( s.getOutputStream());
                                    out.writeUTF(args[0]);            // 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());}}
     }
}


TCP: OUTPUT
C:\Documents and Settings\cse4y>e:
E:\>cd Lab
E:\Lab>cd TCP
E:\Lab\TCP>javac *.java
E:\Lab\TCP>java TCPServer 127.0.0.1

C:\Documents and Settings\cse4y>e:
E:\>cd Lab
E:\Lab>cd TCP
E:\Lab\TCP>java TCPClient hello 127.0.0.1
received;hello