Java Reference
In-Depth Information
sent through the postal system will make it to the intended recipient. The main classes used
are DatagramSocket and DatagramPacket in the Java API. A packet is information in the form of
byte sequences that are sent across a network. Since messages are not guaranteed to be deliv-
ered and can be received out of order, UDP socket applications have to deal with reordering
and data loss.
Efficiency and flexibility are the main reasons you would choose a UDP socket approach.
We do not use UDP for a few reasons. First, efficiency is not something that is an overriding
concern. Second, and most important, the exam does not permit a UDP approach. So that
sort of makes our decision rather clear-cut. But if we were to use a UDP approach, then
both the client and the server would use a DataGramSocket class for sending and receiving
DataGramPacket objects. The remainder of this chapter and the sample project use TCP
sockets instead of UDP sockets, so our discussion of UDP will end here.
Caution The exam requirements tend to be vague regarding this point. Most likely the instructions read
“You must use either serialized objects over a simple socket connection, or RMI.” It is not precisely clear as
to what a simple socket is and whether this precludes a UDP solution. We take it to mean that you should
use the java.net.Socket class and not the java.net.DatagramSocket class. While it would be techni-
cally feasible to create a UDP solution for this assignment, doing so would require a lot of work in developing
packet-handling code, and would go against the standard expected usage of UDP (simple, short [often less
than 100 bytes] messages). We therefore recommend that, if you're using sockets as the networking proto-
col, you use the simpler TCP approach. However, that being said, you should always refer to your specific
exam for instructions.
UDP is designed for minimal messaging applications—the very small messages that you
might use if you were trying to monitor network applications, such as SNMP (Simple Network
Management Protocol)—or for very simple query/responses, such as DNS (Domain Name
System) queries. It is better to use TCP for larger messages, such as the large messages that are
possible in response to a database query in our sample application. The next section describes
how to develop a socket solution using TCP.
TCP Sockets
TCP stands for Transmission Control Protocol—a protocol that controls the transmission of
packets so that messages are guaranteed to be received and that packets are received in the
same order as they were sent. To meet these guarantees, the TCP protocol uses slightly more
network traffic than UDP (and since RMI provides more features and guarantees than a plain
TCP socket, it uses even more network traffic than TCP). However, since the protocol provides
these features for us, we do not have to verify the order in which packets are received in our
application. This makes it a much better choice for Denny's DVDs.
A TCP socket is a socket connection that utilizes a TCP/IP connection as its underlying
transfer protocol. Each end of the connection is identified with an IP address and a port num-
ber. TCP socket clients send requests, and TCP socket servers listen for requests.
Search WWH ::




Custom Search