Java Reference
In-Depth Information
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
}
/**
* A helper method which closes the socket connection.
* Needs to be called from within a try-catch
*
* @throws IOException Thrown if the close operation fails.
*/
private void closeConnections() throws IOException {
oos.close();
ois.close();
socket.close();
}
}
Socket Servers
In this section, we discuss the various types of socket servers and the techniques often used to
build them. We also introduce the concept of an application protocol and how we imple-
mented the protocol in the Denny's DVDs socket server.
Multicast and Unicast Servers
A unicast server involves one-to-one communication between a client and a server. The sock-
ets developed in Denny's DVDs are unicast. However, in addition to unicast sockets, there are
multicast sockets .
Multicast sockets are connections between groups of machines. Multicasting, or broad-
casting, is used when data is sent from one host to multiple clients. The class
java.net.MulticastSocket is used when writing applications that broadcast messages to
many clients. We will not discuss multicast sockets any further, but you should at least be
familiar with the concept. Figure 7-2 highlights the differences between multicast and unicast
sockets.
Multitasking
An iterative, or single-threaded, server is one that handles client requests sequentially. As one
request comes in, the server responds and begins listening again once the request is com-
pleted. Client requests that come in while the server is processing a prior request are placed
on a queue and serviced as prior requests are completed. In addition, as long as one client
maintains the port connection to a single-threaded server, no other client can connect—if
the connection is held for too long, some of the waiting clients may time out. (Refer to the
SO_TIMEOUT setting earlier.) Figure 7-2 shows the lifecycle of an iterative, or sequential, socket
server.
Search WWH ::




Custom Search