Client/Server Programming in Java
The four examples in this section have a similar structure: the server maintains a high scores list, and the clients read and add scores to the list. Here are the four variants of this idea:
A client and sequential server. The server can process only one client at a time. The TCP/IP protocol is utilized.
The same client and the same protocol as (1), but the server is threaded, enabling it to process multiple clients at the same time. Synchronization issues arise because the high score list may be accessed by multiple threads at the same time.
The same client and the same protocol as (1) and (2), but Java's NIO is used to implement a multiplexing server without the need of threads.
A client and server using UDP. The server exhibits multiplexing due to the self-contained nature of datagram communication.
TCP Client and Sequential Server
The communications network created by the client and server is shown in Figure 29-6.
The server instantiates a ServerSocket
object at port 1234 and waits for a connection from a client by calling accept()
. When a connection is established, a new socket is created (some people call this a rendezvous socket), which is used for the subsequent communication with the client. Input and output streams are layered on top of the socket, utilizing the bidirectional nature of a TCP link. When the client has finished, the rendezvous socket is closed (terminated), and the server waits for another connection.
Figure 29-6. Client and sequential ...
Get Killer Game Programming in Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.