[ Contact ] [ Links ] [ Previous : 58 / 60 : JAVA TCP/IP Socket Implementation ] [ Up ] [ Next : 60 / 60 : JAVA TCP/IP Socket Implementation (7) ]

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());
		break;                    
 	}
	/* ... Process client connection ... */
}
// Only break out of while loop if there was an error

eScience
< Internetworked VR : Networking >
[ Contact ] [ Links ] [ Previous : 58 / 60 : JAVA TCP/IP Socket Implementation ] [ Up ] [ Next : 60 / 60 : JAVA TCP/IP Socket Implementation (7) ]

See the "Links" link above to find out the sources of the proposed informations
Pascal Vuylsteker / eScience / Computer Science / ANU
Last modified: 22/3/2005
TOC - Print
Send your comments at :
<pvk@vuylsteker.net>