Server Socket in JavaIn Java, ServerSocket can be defined as a type of class which is majorly utilized for providing implementation of the server-side socket connection of client or server. Also, the socket connection of the client or client is fully independent of system. Let us understand about ServerSocket class in Java in detail. ServerSocket Class in JavaAn exception is being throwed by the ServerSocket by the constructor when it is not able to listen on the required port (For instance, when the port is already is being used). In other words, server sockets can also be considered as major components that are utilized in network programming, for enabling communication among applications of client and server through a network. A server-side endpoint is represented by a server socket whose main purpose is to listen for connections regarding incoming connections of client. The java.net class contains the ServerSocket class and it is provided by the Java for the implementation of server sockets. For the successful creation of a server socket connection in Java, an instance or object of the ServerSocket class must be definitely created, and a port number is specified along with a backlog parameter which is optional. The main purpose of a port number is to specify and identify the server socket which is present on the machine. The backlog parameter is majorly utilized for determining the maximum number of connections which are pending that are present in the queue of the server's connection. Upon the successful creation of the server socket, it is eligible to enter the listening state, where it waits for the incoming requests of connection from the clients. The main purpose of accept() method of the ServerSocket class is to accept and establish a connection with the client. A new object of the Socket class is returned whenever a connection request is received. The Socket object is utilized for representing the channel of communication that occurs with the client. After successful acceptance of the connection with the client, both input and output streams can be obtained by the server from the Socket object for communicating with the client. The mutual exchange of data between the server and client connected is done utilizing the streams of input and output. Let's understand about ServerSocket in Java in detail with the help of a Java example program. Example 1:Filename: ServerSocketExample.java Output: Server is listening on port 5555 Example 2:Filename: AdvancedJavaServerSocket.java Output: Server is listening on port 5555 Example 3:Filename: ServerSocketExample.java Output: Server is listening on port 5555 Let's understand about the methods of Server socket in Java and their functionalities in detail. Methods of Server Socket in JavaThere are various methods of Server Socket in Java and let us understand about their features, purpose and functionality in detail.
Let's understand in detail about the methods of the ServerSocket class with the help of few Java example programs. Example 1:Filename: ProtocolServer.java Output: Server is listening on port 8080 |