View RSS Feed

Java Socket

Java Networking: Server Sockets

Rate this Entry
by , 04-25-2012 at 11:33 PM (726 Views)
For implementation of the Java server which listens to the incoming connections of the clients through TCP/IP, 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 Java Socket. It looks like:
Java Code:
ServerSocket serverSocket = new ServerSocket(9000);
boolean isStopped = false;
    Socket clientSocket = serverSocket.accept();
    //do something with clientSocket
Just one single incoming connection gets opened for sake of every call to the accept() method.
In addition to this, acceptance of the incoming connections is only done when thread which runs the server consists of called accept(). Thread is executed outside this method all the time to which clients cant connect. Hence, accepting thread usually would be passing the incoming connections on worker thread pool, where communication could then be done with client.

Submit "Java Networking: Server Sockets" to Facebook Submit "Java Networking: Server Sockets" to Digg Submit "Java Networking: Server Sockets" to Submit "Java Networking: Server Sockets" to StumbleUpon Submit "Java Networking: Server Sockets" to Google