Friday, September 5, 2025
HomeLanguagesJavaCreating a Socket to Display Message to a Single Client in Java

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

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

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(socket.getInputStream()));
            System.out.println("Response from Server.....");
 
            // Print response from server
            System.out.println("Client Side : "
                               + bs.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);
        }
    }
}


RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS