previous |
start |
next
Multi-Threaded Servers
The server in the previous slide was single
threaded, meaning that only one client could be connected
to it at any instant. This is a serious restriction in a "Real
World(tm)" environment.
Whilst the Java code for a multi-threaded server is too complex to
include into the lecture, the basic concept is that the
accept()
method returns a new,
connected, socket. A program can start up a new
thread of execution which handles communications
using the connected socket, whilst allowing the "main program" to
again go back to "accepting connections".
Conceptual Conundrum -- how can the newly-created
program thread perform communications on (eg) port 7277 at the same
time as the server is "waiting for connections" at the same port
number? The answer to this is very subtle. In fact, an active TCP
connection is characterised by a 4-tuple,
consisting of the IP addresses of each of the computers involved,
plus both of the port numbers (both client and
server) in use. Because this 4-tuple is unique for every TCP
connection (Exercise: why?), it's possible to have many server
threads serving client requests simultaneously (or, more correctly,
concurrently)
previous |
start |
next