java.net.ServerSocket Class - Java Networking API
java.net.ServerSocket
This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.
The actual work of the server socket is performed by an instance of the SocketImpl class. An application can change the socket factory that creates the socket implementation to configure itself to create sockets appropriate to the local firewall.
Creating Objects
ServerSocket serverSocket = new ServerSocket(port);:
Creates a ServerSocket that listens on a specified port (e.g., 12345).
ServerSocket Class Methods
public Socket accept()
Listens for a connection to be made to this socket and accepts it.
public void bind(SocketAddress endpoint)
Binds the ServerSocket to a specific address (IP address and port number).
public void bind(SocketAddress endpoint, int backlog)
Binds the ServerSocket to a specific address (IP address and port number).
public void close()
Closes this socket.
public ServerSocketChannel getChannel()
Returns the unique ServerSocketChannel object associated with this socket, if any.
public InetAddress getInetAddress()
Returns the local address of this server socket.
public int getLocalPort()
Returns the port number on which this socket is listening.
public SocketAddress getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to.
public boolean isBound()
Returns the binding state of the ServerSocket.
public boolean isClosed()
Returns the closed state of the ServerSocket.
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth)
Sets performance preferences for this ServerSocket.
public void setReceiveBufferSize(int size)
Sets a default proposed value for the SO_RCVBUF option for sockets accepted from this ServerSocket.
public void setReuseAddress(boolean on)
Enable/disable the SO_REUSEADDR socket option.
public static void setSocketFactory(SocketImplFactory fac)
Sets the server socket implementation factory for the application.
public void setSoTimeout(int timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
public String toString()
Returns the implementation address and implementation port of this socket as a String.
ServerSocket Class Example
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSocketExample {
public static void main(String[] args) {
int port = 12345; // Port number to listen on
try {
// Create a ServerSocket that listens on the specified port
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server is running and listening on port " + port);
// Listen for incoming connections indefinitely
while (true) {
// Accept a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Accepted connection from client: " + clientSocket.getInetAddress());
// Setup streams for sending and receiving data
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()), true);
// Read messages from the client and echo them back
String message;
while ((message = in.readLine()) != null) {
System.out.println("Received from client: " + message);
// Echo message back to client
out.println("Server echoed: " + message);
}
// Close resources for this client
in.close();
out.close();
clientSocket.close();
}
} catch (IOException e) {
System.err.println("IOException occurred while running the server");
e.printStackTrace();
}
}
}