Tuesday, 26 February 2013

                                    JAVA Socket Programming 

* What is a socket?

•Socket
–The combination of an IP address and a port number. (RFC 793 ,original TCP specification)
–The name of the Berkeley-derived application program
 ming interfaces (APIs) for applications using TCP/IP protocols.
 
–Two types
•Stream socket : reliable two-way connected communication streams
•Datagram socket
–
•Socket pair
 
–Specified the two end points that uniquely identifies each TCP connection in an internet.
 
–4-tuple: (client IP address, client port number, server IP address, server port number)


* Client-server applications



•Implementation of a protocol standard defined in an RFC. (FTP, HTTP, SMTP…)
–Conform to the rules dictated by the RFC.
–Should use the port number associated with the protocol.

•Proprietary client-server application.
–A single developer( or team) creates both client and server program.
–The developer has complete control.
–Must be careful not to use one of the well-known port number defined in the RFCs.
* well-known port number : managed by the Internet Assigned Numbers Authority(IANA)



 * ProSocket gramming with TCP:-


 


 
The application developer has the ability to fix a few TCP parameters, such as maximum buffer and maximum segment sizes.


Sockets for server and client:-
•Server
–Welcoming socket
•Welcomes some initial contact from a client.
–Connection socket
•Is created at initial contact of client.
•New socket that is dedicated to the particular client.
•Client
–Client socket
 
•Initiate a TCP connection to the server by creating a socket object. (Three-way handshake) 
•Specify the address of the server process, namely, the IP address of the server and the port number of the process.


JAVA TCP Sockets :-


•In Package java.net
 
–java.net.Socket
 
•Implements client sockets (also called just “sockets”).
•An endpoint for communication between two machines.
•Constructor and Methods
 
–Socket(String host, int port): Creates a stream socket and connects it to the specified port number on the named host.
 
–InputStream getInputStream()
 
–OutputStream getOutputStream()
 
–close()

–java.net.ServerSocket
 
•Implements server sockets.
•Waits for requests to come in over the network.
•Performs some operation based on the request.
•Constructor and Methods
 
–ServerSocket(int port)
 
–Socket Accept(): Listens for a connection to be made to this socket and 
accepts it. This method blocks until a connection is made.
 

 TCPClient.java:-

BufferedReader inFromServer =
          new
BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
       
  sentence = inFromUser.readLine();
       
  outToServer.writeBytes(sentence + '\n');
       
  modifiedSentence = inFromServer.readLine();
       
  System.out.println("FROM SERVER: " + modifiedSentence);
      
  clientSocket.close();
                  
}
} 



TCPServer.java :-

DataOutputStream  outToClient = 

             new
DataOutputStream(connectionSocket.getOutputStream());
          
  clientSentence = inFromClient.readLine();
          
  capitalizedSentence = clientSentence.toUpperCase() + '\n';

  outToClient.writeBytes(capitalizedSentence);
       
     }
  }
}