Comment on page
This class implements server sockets. A server socket waits for requests to come in over the network. It may then accept the incoming TCP/IP connection and perform some operation based on that request, possibly returning a result to the requester.
You may retrieve the address and port values of this ServerSocket with
After creating a server socket, you may use the method
accept()to wait for incoming connections. This method blocks the thread for the amount of time specified by the timeout value passed to the constructor, returning a null value when the timeout is over, or until a connection request is received and accepted, returning a socket instance representing the new connection.
The returned object is always a valid Socket instance, that may be used to transfer data between this server and the client that requested the connection, and that should be closed when no longer needed.
You should never use blocking operations on threads handling events and/or the graphical interface, otherwise the user won’t be able to interact with the application. Take a look at the source code of the sample ServerSocketTest.
Finally, you may use the method
close()to close this server socket, releasing any associated resources.
Remember to close any sockets associated to this server socket before closing it. Otherwise all open sockets will throw an IOException.