Wednesday, July 3, 2024
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);
        }
    }
}


Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments