Socket programming and networking examples in Java.
For implementation of the Java server which listens to the incoming connections of the clients through TCP/IP, java.net.ServerSocket shall be used.
Code example has been given below which will be creating ServerSocket so that for listening at port 9000:
ServerSocket serverSocket = new ServerSocket(9000);
For acceptance of the incoming connections, ServerSocket.accept() method shall be called. This method would return the socket that is just like some ordinary
One common question which arises in mind is that who listens at the socketís other end. Answer to this question is nobody unless server socket is set or pre existing service has been connected and an attempt to make connection would be failed. Principal constructor is : Java Code: ServerSocket(int port)
that makes a server socket which listen at specific port. To make a connection or set server socket, use this code: Java Code: import java.net.*;