View RSS Feed

My Java Tips

TCP IP Using Socket Programming

Rate this Entry
by , 11-08-2011 at 07:46 PM (3579 Views)
TCP is a connection-oriented protocol that addresses numerous reliability issues to provide a reliable byte stream. It ensures that the data arrives in-order, duplicate data is discarded and lost/discarded packets are re-sent. It includes traffic congestion control meaning that if there is too much traffic on the network, appropriate measures are taken which include telling the transmitting hosts to slow down the rate at which the data is being sent out and telling the receiving hosts to increase the amount of time before which they would send a retransmit request to the transmitting hosts. In Java, there are two kinds of TCP sockets. One is for servers which the other is for clients. The ServerSocket class is designed as a listener meaning that it does not do anything till the client connects to it. The Socket class on the other hand is designed for the clients and it connects to the server socket and initiates protocol exchanges.

Here is a simple TCP/IP server:



Here is a simple TCP/IP server:

Java Code:
import java.net.*;import java.io.*;

public class tcp_server

{

public static void main (String args[])

{

try

{

int serverPort = 7896;

ServerSocket listenSocket = new ServerSocket(serverPort, 25);

while(true)

{

Socket clientSocket = listenSocket.accept();

connection c = new connection(clientSocket);

}

}

catch(IOException e)

{

System.out.println("Listen :"+e.getMessage());

}

}

}import java.net.*;

import java.io.*;

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("close failed");

}

}

}

}
Code Explanation
In the tcp_server class, First of all create a server socket and specify the port address and the maximum number of clients the server can handle at any given time. In the connection class, we are creating a client socket along with an object for handling the data being sent in by the client and another one for the data being sent out by the server. We make this class thread-based so that whenever a new client is connected to the server, a new thread is created for servicing it.
A simple TCP/IP client can be implemented in the following manner.

Java Code:
import java.net.*;

import java.io.*;

public class tcp_client

{

 public static void main (String args[])

 {

 	Socket s = null;

 	try

 	{

 		int serverPort = 7896;

 		s = new Socket(args[1], serverPort);

 		DataInputStream in = new DataInputStream( s.getInputStream());

 		DataOutputStream out =	new DataOutputStream(s.getOutputStream());

 		out.writeUTF(args[0]);

 		String data = in.readUTF();

 		System.out.println("Received: "+ data) ;

       	}

 	catch (UnknownHostException e)

 	{

 		System.out.println("Sock:"+e.getMessage());

     }

 	catch (EOFException e)

 	{

 		System.out.println("EOF:"+e.getMessage());

 	}

 	catch (IOException e)

 	{

 		System.out.println("IO:"+e.getMessage());

 	}

 	finally

 	{

 		if(s!=null)

 			try

 			{

 				s.close();

 			}

 			catch (IOException e)

 			{

 				System.out.println("close:"+e.getMessage());

 			}

 	}

 }

}
Code Explanation
In this class, we have first of all created a client socket connecting it to the server whose name is passed as an argument from the command line and which has a port address of 7896 and then we create the objects for handling the communications with the server. Alternately, we can also link a client to a host by the IP address using the InetAddress class.

Submit "TCP IP Using Socket Programming" to Facebook Submit "TCP IP Using Socket Programming" to Digg Submit "TCP IP Using Socket Programming" to del.icio.us Submit "TCP IP Using Socket Programming" to StumbleUpon Submit "TCP IP Using Socket Programming" to Google

Comments