Creating a Socket to Display Message to a Single Client in Java Last Updated : 25 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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); } } } Comment More infoAdvertise with us Next Article Transfer the File "Client Socket to Server Socket" in Java P prateekc231 Follow Improve Article Tags : Java Java-Networking Practice Tags : Java Similar Reads Transfer the File "Client Socket to Server Socket" in Java Prerequisites: Socket Programming in JavaFile Handling in JavaThis article describes a one-way client and Server Setup where a client connects, and sends the file to the server and the server writes the file in another location with a different name. It means we send the file using the server socket 4 min read How to Make a Server to Allow the Connection to the Socket 6123 in Java? A socket connection means the two machines have information about each otherâs network location (IP Address) and TCP port. The java.net.Socket class represents a Socket. Here, we are going to look at the approach of connecting to socket 6123. Approach: Create an object of Socket class and pass 6123 2 min read How to Create a Socket at a Specific Port in Java? A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. Socket Programming, us basically client-server programming where a socket is used as a link betwee 3 min read java.net.SocketOption Class in Java The java.net.SocketOption is a socket option that is connected with a socket, as the set of channels packages is java.nio.channels.NetworkChannel that this interface has defined the setOption as well as the getOption methods to set and query's the channels within its Socket Options. --> java.net 5 min read How to Create a Chat App Using socket.io in NodeJS? Socket.io is a JavaScript library that enables real-time, bidirectional, event-based communication between the client and server. It works on top of WebSocket but provides additional features like automatic reconnection, broadcasting, and fallback options.What We Are Going to Create?In this article, 5 min read How to Create a Non-Blocking Server in Java? A Non-Blocking server means that it is able to have multiple requests in progress at the same time by the same process or thread because it uses Non-Blocking I/O. In the Non-Blocking approach - one thread can handle multiple queries at a time. A server using a Non-Blocking socket works in an asynchr 7 min read Like