TCP Socket Server-Client

"java.net" package contain all networking classes. So this package need to be imported in the program. ServerSocket class is used to create a server. Port number has to be provided during creation of server. This port number will be associated with server application.

Important! Combination of IP address and port number known as socket.

Creating a TCP server

Port number from 0 to 1024 are reserved that's why port number after 1024 will be used. We have taken 5000 as port number.

ServerSocket  server=new ServerSocket(5000);

Start Listening for connection

ServerSocket has a method accept(), on the invocation of this method server start listening for connection. On the call of accept method socket class object is returned. This socket object represent connection between server and client.

Socket  socket= server.accept();

Above code will make server to listen for only one connection. To create a server which can listen for any number of connection.

Socket  socket;
  while(true)
   {
     socket=server.accept();
	 // process socket
   }

Closing the connection

When work of connection is completed then connection has to be teared down. To close the socket connection close method is used.

socket.close();

Shut down the server

To stop server from accepting connection close() method can be used on ServerSocket object.

server.close();

Tcp socket server program


import java.net.*;
class server
 {
   public static void main(String arg[])throws Exception
     {
	   ServerSocket server=new ServerSocket(5000);
	   while(true)
	    {
	   System.out.println("Waiting ....");
	    try 
		  {
		   Socket s=server.accept();
		   }
		catch(Exception e) {System.out.println("Exception Occurred");}
	   System.out.println("Connection Established!!");
	  }
	 }
 }
   



Creating a Client

In order to connect client to the server, IP address of server machine and port on which server is running is required. if server is running on same machine then ip address will be 127.0.0.1, which is address of localhost.

Socket socket=new Socket("ip-address",port);

Tcp client socket program


import java.net.*;
class client
 {
   public static void main(String arg[]) 
     {
	   try
	    {
	   Socket s=new Socket("127.0.0.1",5000);
	   System.out.println("Connection established!!");
	   }
	   catch(Exception e)
	    {
		  System.out.println("Server not running on 5000 Port");
		}
	 }
 }
   

Execution of Programs

compile both program by using following commands on command interpreter


c:>javac server.java
c:>javac client.java

Run server program first then open another command prompt and run client program in that command prompt

c:>java server
Waiting...

On execution of client program

c:>java client
Connection established!!

When you run client program server receive request and connection is established. Server program also print following message

c:>java server
Waiting...
Connection established!!


TCP Server Client Message Exchange Program