«Back to Home

Core Java

Topics

Socket Programming In Java

Socket Programming
 
In Java, socket programming is used for communication between the Applications, running on the different JRE and it can be connection-oriented or connection-less.
 
Socket class and ServerSocket class are used for the connection-oriented socket programming and DatagramSocket class and DatagramPacket class are used for the connection-less socket programming.
 
In socket programming, the client have to know: IP Address of Server and Port number.
 
Socket class

A socket is simply an endpoint for connections between the machines. The Socket class can be used to create a socket.
 
Methods of Socket class

public InputStream getInputStream()

This method is used to return the InputStream attached with the socket.
 
public OutputStream getOutputStream()

This method is used to return the OutputStream attached with the socket.
 
public synchronized void close()

This method is used to close the socket
 
ServerSocket class

The ServerSocket class can be used to make a Server socket and this object is used to establish the communication with the clients.
 
Methods of ServerSocket class

public Socket accept()

This method is used to return the socket and establish a connection between the Server and client.
 
public synchronized void close()

This method is used to close the Server socket.
 
Let's see an example of socket programming,, given below.
 
Code
  1. import java.io.*;  
  2. import java.net.*;  
  3. public class ServerClass {  
  4.     public static void main(String[] args) {  
  5.         try {  
  6.             ServerSocket soc = new ServerSocket(8888);  
  7.             Socket s = soc.accept();  
  8.             DataInputStream dis = new DataInputStream(s.getInputStream());  
  9.             String st = (String) dis.readUTF();  
  10.             System.out.println("Message= " + st);  
  11.             soc.close();  
  12.         } catch (Exception e) {  
  13.             System.out.println(e);  
  14.         }  
  15.     }  
  16. }  
2
  1. import java.io.*;  
  2. import java.net.*;  
  3. public class ClientClass {  
  4.     public static void main(String[] args) {  
  5.         try {  
  6.             Socket soc = new Socket("Localhost"8888);  
  7.             DataOutputStream dos = new DataOutputStream(soc.getOutputStream());  
  8.             dos.writeUTF("HI, SERVER");  
  9.             dos.flush();  
  10.             dos.close();  
  11.             soc.close();  
  12.         } catch (Exception e) {  
  13.             System.out.println(e);  
  14.         }  
  15.     }  
  16. }  
3

In the example of socket programming, given below, where the client sends a text and the Server receives it. After running the client Application message will be displayed on the Server console.
 
Let’s see another example of socket programming, given below.
 
Code
  1. import java.io.*;  
  2. import java.net.*;  
  3. public class ServerClass {  
  4.     public static void main(String args[]) throws Exception {  
  5.         ServerSocket ssoc = new ServerSocket(3333);  
  6.         Socket soc = ssoc.accept();  
  7.         DataInputStream dis = new DataInputStream(soc.getInputStream());  
  8.         DataOutputStream dos = new DataOutputStream(soc.getOutputStream());  
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  10.         String str = "", str2 = "";  
  11.         while (!str.equals("Stop")) {  
  12.             str = dis.readUTF();  
  13.             System.out.println("Client: " + str);  
  14.             str2 = br.readLine();  
  15.             dos.writeUTF(str2);  
  16.             dos.flush();  
  17.         }  
  18.         dis.close();  
  19.         soc.close();  
  20.         ssoc.close();  
  21.     }  
  22. }  
4
  1. import java.io.*;  
  2. import java.net.*;  
  3.   
  4. public class ClientClass {  
  5.     public static void main(String args[]) throws Exception {  
  6.         Socket soc = new Socket("Localhost"3333);  
  7.         DataInputStream dis = new DataInputStream(soc.getInputStream());  
  8.         DataOutputStream dos = new DataOutputStream(soc.getOutputStream());  
  9.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  10.         String str = "", str2 = "";  
  11.         while (!str.equals("Stop")) {  
  12.             str = br.readLine();  
  13.             dos.writeUTF(str);  
  14.             dos.flush();  
  15.             str2 = dis.readUTF();  
  16.             System.out.println("Server: " + str2);  
  17.         }  
  18.         dos.close();  
  19.         soc.close();  
  20.     }  
  21. }  
5

In the example, mentioned above, the client will write first to the Server then the Server will receive and print the text. The Server will also write to the client, client will receive, print the text and step goes on.
 
Summary

Thus, we learnt Java socket programming is used for the communication between the Applications, running on different JRE and it can be connection-oriented or connection-less and also learnt how we can use it in Java.