JAVA TCP/IP Socket Implementation (6)
Server Actions
|
Receive connections from clients
|
- Instantiate a ServerSocket object
- Receive connections from clients
- Communicate with clients
- Recieve data/requests
- Send data/replys
- 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