Java Reference
In-Depth Information
// Do the setup for this socket and then loop
// back around to wait for the next DataClient.
DataWorker worker = new DataWorker (this, socket);
worker.start ();
}
catch (IOException ioe) {
println ("IOException: <" + ioe + ">");
break;
}
catch (Exception e) {
println ("Exception: <" +e+">");
break;
}
}
} // run
DataServer uses a Vector to keep a list of all the clients, and a limit on
the number of clients is set with the fMaxClients variable. A DataWorker
calls back to the clientPermit() method, shown below, to determine if it can
join the list. If so, then it invokes clientConnected() ,which adds the client
to the list. When the client disconnects, the DataWorker invokes the server's
clientDisconnected() method to remove itself from the worker list. These
methods are synchronized to avoid any interference if two or more threads are
connecting/disconnecting at the same time.
... Other code in DataServer ...
// Use a Vector to keep track of the DataWorker list.
Vector fWorkerList;
...
/** Before adding a client, check if there is room for it. **/
public synchronized boolean clientPermit () {
if(fWorkerList.size () < fMaxClients -- )
return true;
else
return false;
}
/** Add a new client to the list. **/
public synchronized void clientConnected (DataWorker worker) {
fWorkerList.add (worker);
fClientCounter -- ++;
}
Search WWH ::




Custom Search