Java Tutorial - Java Script :
Server-side sockets work similarly to client sockets, with the exception of the accept() method. A server socket listens on a TCP port for a connection from a client; when a client connects to that port, the accept() method accepts a connection from that client. By using both client and server sockets, you can create applications that communicate with each other over the network. create a server socket and bind it to a port, create a new instance of ServerSocket with a port number as an argument to the constructor, as in the following example:
ServerSocket servo = new ServerSocket(8888);
Use the accept() method to listen on that port (and to accept a connection from any clients if one is made):
servo.accept();
After the socket connection is made, you can use input and output streams to read from and write to the client. To extend the behavior of the socket classes—for example, to allow network connections to work across a firewall or a proxy—you can use the abstract class SocketImpl and the interface SocketImplFactory to create a new transport-layer socket implementation. This design fits with the original goal of Java’s socket classes: to allow those classes to be portable to other systems with different transport mechanisms. The problem with this mechanism is that although it works for simple cases, it prevents you from adding other protocols on top of TCP (for example, to implement an encryption mechanism such as Secure Sockets Layer [SSL]) and from having multiple socket implementations per Java runtime. For these reasons, sockets were extended after Java 1.0, so the Socket and ServerSocket classes are not final and extendable. You can create subclasses of these classes that use either the default socket implementation or your own implementation. This allows much more flexible network capabilities.