This lab is not really about sockets but about how you can build a server that can handle several clients
at once.
Servers can be classified attending on how they handle simultaneous clients. The first type is called iterative server. This is when a server can only handle one client at a time. If there are more than one client, they will be served on a first-come first-served basis. Every call to the accept() method will give the server a Socket object to talk to the next client. Once a client has been served, the server goes on to serve the next client that is waiting (if there is any). When multiple clients arrive at the same time, all but the lucky one will have to wait.
The second type of servers, called concurrent servers, can handle several clients at the same time. Clients do not have to wait to be served, and average response time is usually much lower, which is a good thing for interactive services (as humans tend to become anxious if they have to wait without nothing apparently happening).
The way we can program a concurrent server is by using techniques for concurrent programming. Two basic mechanisms for doing that: multiple process and multiple execution threads. We can use threads easily in Java by extending Thread class and re-writing its run() method with the code we want our thread to run.
Here you have a simple example of it. The main program will start a thread that after some time will print a message. Main program itself does not much more than printing another message, but it could be doing any other more fancy task meanwhile.
No comments:
Post a Comment