by, 04-25-2012 at 11:22 PM (706 Views)
Communication mechanism is provided by the sockets b/w 2 computers by the help of TCP. Client program makes socket to make it connected to the server.
After the connection has been created, a socket object is created by the server at its communication end. Server & client then can communication by reading from or writing to the socket.
Socket is represented by the java.net.Socket & mechanism is provided by the java.net.ServerSocket for server program so that connections might be created with them.
By using socket, TCP connections are made b/w computers. Following steps are involved.
1. ServerSocket object is instantiated by the server, which denotes that which port number communication needs to be occured on.
2. ServerSocket class accept() method is invoked by the server. Such method waits till a connection is made b/w client and server, at a port.
3. Socket object is instantiated by the client after this that specifies the port number & server name.
4. Socket class constructor makes connection b/w client and port number & server. When communication has been established then socket object is present with client that makes communication possible with server.
5. Reference is returned by the accept() method to new socket present at server, which is connected to socket of client.
I/O streams could be used to communicate when establishment of connections has been done. Both InputStream and OutputStream is present in every socket. OutputStream of client & InputStream of server are connected, & serverís OutputStream is connected to the Clientís OutputStream.
TCP is considered to be a 2 way protocol of communication which makes it possible to send the data across stream, at same time. Useful classes which give methods for implementation of the sockets are given below.