Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Creating a Socket to Display Message to a Single Client in Java

  • Last Updated : 02 Dec, 2020

This article describes the basic client-server connection where a client connects, a server sends a message to the client and the client displays the message using a socket connection. A client program sockets establish a connection with the server socket of server application then server socket connects with internal sockets in the server application.

Client-Side Program

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Client program uses Socket class to establish a connection with a server. Socket object needs the address of the server and the port number of the server.

Java






// Client program
  
import java.io.*;
import java.net.*;
  
class GFG {
    
    // driver function
    public static void main(String[] args)
    {
        try {
            
            // Create socket object by passing id address
            // and port number establish connection
            Socket socket = new Socket("localhost", 1346);
            System.out.println(
                "Connected Successfully.....");
  
            // Buffer reader to get all the input stream
            BufferedReader bs = new BufferedReader(
                new InputStreamReader(s.getInputStream()));
            System.out.println("Response from Server.....");
  
            // Print response from server
            System.out.println("Client Side : "
                               + br.readLine());
            // Close the connection
            socket.close();
        }
        catch (UnknownHostException e) {
            
            // Catch block for IP errors
            System.out.println("IP not found for" + e);
        }
        catch (IOException e) {
            
            // Catch block for data stream errors
            System.out.println("Not found data for socket"
                               + e);
        }
    }
}

Server-Side Program

Server program uses a Server Socket class to establish a connection with the client. Server Socket object needs the port number.

Java




// Server program
  
import java.io.*;
import java.net.*;
  
class GFG {
    public static void main(String[] args)
    {
        try {
            
            // establish connection
            ServerSocket serversocket
                = new ServerSocket(1346);
  
            System.out.println("waiting for request....");
  
            // Socket object to accept all the connections
            Socket socket = serversocket.accept();
  
            System.out.println("Request Accepted...");
            
            // Printstream to print all the data
            PrintStream ps
                = new PrintStream(socket.getOutputStream());
  
            BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
            
            System.out.println(
                "Input the data at the server...");
            
            // Printing bufferedreader data
            ps.print(br.readLine());
            socket.close();
            serversocket.close();
        }
        catch (IOException e) {
            
            // Catch block for data stream errors
            System.out.println("Not found data for socket"
                               + e);
        }
    }
}




My Personal Notes arrow_drop_up
Recommended Articles
Page :