How to Make a Server to Allow the Connection to the Socket 6123 in Java? Last Updated : 11 Nov, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 as an argument.Accept connections with accept() method of ServerSocket class.Get the address of the connection with getInetAddress() method of Socket class and getHostAddress() of InetAddress class. Server Code: Java // Make a server to allow the connection to the socket 6123 import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { check(); } private static void check() { try { // Creating object of ServerSocket class ServerSocket connection = new ServerSocket(6123); while (true) { System.out.println("Listening"); // Creating object of Socket class Socket socket = connection.accept(); // Creating object of InetAddress class InetAddress address = socket.getInetAddress(); System.out.println( "Connection made to " + address.getHostAddress()); pause(1000); // close the socket socket.close(); } } catch (IOException e) { System.out.println("Exception detected: " + e); } } private static void pause(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { } } } Output Client Code: Java // A Java program for a Client import java.net.*; import java.io.*; public class Client { // initialize socket and input output streams private Socket socket = null; private DataInputStream input = null; private DataOutputStream out = null; // constructor to put ip address and port public Client(String address, int port) { // establish a connection try { socket = new Socket(address, port); System.out.println("Connected"); // takes input from terminal input = new DataInputStream(System.in); // sends output to the socket out = new DataOutputStream( socket.getOutputStream()); } catch (UnknownHostException u) { System.out.println(u); } catch (IOException i) { System.out.println(i); } // string to read message from input String line = ""; // keep reading until "Over" is input while (!line.equals("Over")) { try { line = input.readLine(); out.writeUTF(line); } catch (IOException i) { System.out.println(i); } } // close the connection try { input.close(); out.close(); socket.close(); } catch (IOException i) { System.out.println(i); } } public static void main(String args[]) { Client client = new Client("127.0.0.1", 6123); } } Output Comment More infoAdvertise with us Next Article How to Make a Server to Allow the Connection to the Socket 6123 in Java? R rbbansal Follow Improve Article Tags : Java Java-Networking Practice Tags : Java Similar Reads 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 con 2 min read 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 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 Multi-threaded chat Application in Java | Set 1 (Server Side Programming) Prerequisites : Introducing threads in socket programmingIn the above article, a simple date time server was created which handled multiple user requests at the same time using threading. It explains the basic concepts of threading in network programming. The same concepts can be used with very slig 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