Javatpoint Logo
Javatpoint Logo

Server Socket in Java

In 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 Java

An 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 Java

There are various methods of Server Socket in Java and let us understand about their features, purpose and functionality in detail.

Method Description
ServerSocket (int i) This method is used for constructing a new ServerSocket on the required and specific port.
ServerSocket( int I , int B) This method is used for creating a new ServerSocket on the required port with the required backlog which is used for representing the maximum number of connections queued.
accept() This method is used for listening for a connection that needs to be made for this socket and accepts that. Until a new connection is made, the method blocks.
bind( SocketAddress S) This method is used for binding the ServerSocket to a required network address.
close() This method is used for closing the ServerSocket. Any accept operations that are blocked will be thrown a SocketException.
getInetAddress() This method gets the local address where the socket is bound currently.
getLocalPort() This method fetches the local port where the socket is bound.
setSoTimeout( int i) This method sets the maximum time as Socket.accept() method call which will block. If the time period expires, then no connection will be made.
getReceiveBufferSize() This method fetches the buffer size and returns it.
serReceiveBufferSize( int i) This method changes the size of the buffer received.

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA