Advanced Java Programming - Old Questions

12. What is socket? How can you communicate two programs in a network using TCP Socket?

5 marks | Asked in 2070

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.


Sockets provide the communication mechanism between two programs using TCP. We create client server programs using TCP as follows:


Creating a Socket Client:

The Socket class is used to implement a client program. We use this class to make connection to a server, send data to and read data from that server. The following steps are applied for a communication with the server:

1. The client initiates connection to a server specified by hostname/IP address and port number. (creation of socket object)

2. Send data to the server using an OutputStream.

3. Read data from the server using an InputStream.

4. Close the connection


Creating a Socket Server

The ServerSocket class is used to implement a server program. Here are the typical steps involve in developing a server program:

1. Create a server socket and bind it to a specific port number

 2. Listen for a connection from the client and accept it. This results in a client socket is created for the connection.

 3. Read data from the client via an InputStream obtained from the client socket.

4. Send data to the client via the client socket’s OutputStream.

5. Close the connection with the client.


Example:

Java program using TCP that enables chatting between client and server


//Server.java 

import java.io.*; 

import java.net.*; 

public class Server

  public static void main(String args[]) throws IOException 

 { 

  try 

 { 

  System.out.println("SERVER:......\\n"); 

  ServerSocket s = new ServerSocket(95); 

  System.out.println("Server Waiting For The Client"); 

  Socket cs=s.accept(); 

  System.out.println("Client connected”); 

  BufferedReader in=new BufferedReader(new InputStreamReader(cs.getInputStream())); 

  PrintWriter out=new PrintWriter(cs.getOutputStream(),true); 

  while(true)

  { 

    BufferedReader din=new BufferedReader(new InputStreamReader(System.in)); 

    System.out.print("To Client:"); 

    String tocl=din.readLine(); 

    out.println(tocl); 

    String st=in.readLine(); 

    if(st.equalsIgnoreCase("Bye")||st==null)break; 

    System.out.println("From Client:"+st); 

  }

 in.close(); 

 out.close(); 

 cs.close(); 

catch(IOException e) { } 

 } 

}


//Client.java 

import java.io.*; 

import java.net.*; 

public class Client 

  public static void main(String args[]) throws IOException 

  { 

   try

  { 

    System.out.println("CLIENT:......\\n"); 

    Socket con=new Socket("localhost", 95); 

    BufferedReader in=new BufferedReader(new InputStreamReader(con.getInputStream())); 

    PrintWriter out=new PrintWriter(con.getOutputStream(),true); 

    while(true) 

   { 

     String s1=in.readLine(); 

     System.out.println("From Server:"+s1); 

     System.out.print("Enter the messages to the server:"); 

     BufferedReader din=new BufferedReader(new InputStreamReader(System.in)); 

     String st=din.readLine(); 

     out.println(st); 

     if(st.equalsIgnoreCase("Bye")||st==null)break; 

   } 

  in.close(); 

  out.close(); 

  con.close(); 

 catch(UnknownHostException e){ } 

}