java.net.ServerSocket Class in Java

27 Apr, 2022
ServerSocket Class is used for providing system-independent implementation of the server-side of a client/server Socket Connection. The constructor for ServerSocket throws an exception if it can’t listen on the specified port (for example, the port is already being used).
It is widely used so the applications of java.net.ServerSocket class which is as follows:
In java.nio channel, ServerSocket class is used for retrieving a serverSocket associated with this channel.In java.rmi.Server, ServerSocket class is used to create a server socket on the specified port (port 0 indicates an anonymous port).In javax.net, Server socket is used widely so as to:return an unbound server socket.return a server socket bound to the specified port.return a server socket bound to the specified port, and uses the specified connection backlog.return a server socket bound to the specified port, with a specified listen backlog and local IP.
In java.nio channel, ServerSocket class is used for retrieving a serverSocket associated with this channel.
In java.rmi.Server, ServerSocket class is used to create a server socket on the specified port (port 0 indicates an anonymous port).
In javax.net, Server socket is used widely so as to:return an unbound server socket.return a server socket bound to the specified port.return a server socket bound to the specified port, and uses the specified connection backlog.return a server socket bound to the specified port, with a specified listen backlog and local IP.
return an unbound server socket.
return a server socket bound to the specified port.
return a server socket bound to the specified port, and uses the specified connection backlog.
return a server socket bound to the specified port, with a specified listen backlog and local IP.
Let us do go through the methods of this class which is as follows:
implementation:
Example 1 Server-Side
Java
// Java Program to implement ServerSocket class// Server Side // Importing required librariesimport java.io.*;import java.net.*; // Main classpublic class MyServer {     // Main driver method    public static void main(String[] args)    {         // Try block to check for exceptions        try {             // Creating an object of ServerSocket class            // in the main() method  for socket connection            ServerSocket ss = new ServerSocket(6666);             // Establishing a connection            Socket soc = ss.accept();             // Invoking input stream via getInputStream()            // method by creating DataInputStream class            // object            DataInputStream dis                = new DataInputStream(soc.getInputStream());             String str = (String)dis.readUTF();             // Display the string on the console            System.out.println("message= " + str);             // Lastly close the socket using standard close            // method to release memory resources            ss.close();        }         // Catch block to handle the exceptions        catch (Exception e) {             // Display the exception on the console            System.out.println(e);        }    }}
Output:
Example 2 Client-Side 
Java
// Java Program to implement ServerSocket class// Client - side // Importing required librariesimport java.io.*;import java.net.*; // Main classpublic class MyClient {     // Main driver method    public static void main(String[] args)    {         // Try block to check if exception occurs        try {             // Creating Socket class object and            // initializing Socket            Socket soc = new Socket("localhost", 6666);             DataOutputStream d = new DataOutputStream(                soc.getOutputStream());             // Message to be displayed            d.writeUTF("Hello GFG Readers!");             // Flushing out internal buffers,            // optimizing for better performance            d.flush();             // Closing the connections             // Closing DataOutputStream            d.close();            // Closing socket            soc.close();        }         // Catch block to handle exceptions        catch (Exception e) {             // Print the exception on the console            System.out.println(e);        }    }}
Output:
 
saurabh1990aror
sooda367
gabaa406
anikakapoor
eklw78ks3sewuo2ouubbbd70mv9tis3ve13vmi3c
Java-net-package
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.