JAVA TCP/IP Socket Implementation (6)

Server Actions

Receive connections from clients

  1. Instantiate a ServerSocket object
  2. Receive connections from clients
  3. Communicate with clients
    1. Recieve data/requests
    2. Send data/replys
  4. Close the socket
  • Accept() call returns a socket that is the connection to a specific client
  • JAVA blocks on the accept() call
  • Usual practice is to fork a thread for each client as well as one for the socket designated to recieve client connections
Socket sock;	// Declare a socket to represent the connection to a
				// specific client, i.e. the socket client and server will
				// communicate over

//  Call accept() on the ServerSocket to receive client connections,
//  when a connection is received a new socket is returned over which
//  the client and server will communicate

while(true) {
	try {
		sock = acceptSock.accept();
	catch(IOException ioe) {
 		System.out.println("accept error: " + ioe.getMessage());
	/* ... Process client connection ... */
// Only break out of while loop if there was an error