Java Reference
In-Depth Information
If I mail you two letters on the same day, they might be delivered on the
same day, but this is hardly guaranteed. In fact, there is no guarantee that both
letters will even get delivered at all. It's possible that one will be delivered the
next day, whereas the other doesn't arrive for two weeks. The same is true for
datagram packets. UDP does not guarantee that packets will be received in the
order they were sent or that they will even be delivered at all.
If this type of unreliability is unacceptable for the program you are develop-
ing, you should use TCP instead. However, if you're developing a network
application in which reliable communication is not essential to the application,
UDP is probably a better option because it does not carry the overhead of TCP.
The java.net.DatagramPacket and java.net.DatagramSocket classes are used
to send and receive datagram packets; I will show you how this is done in the
upcoming section Overview of Datagram Packets .
Classroom Q & A
Q: How does a computer find another computer on the network?
A: Every computer on the network has a unique numeric value,
which is referred to as its IP address . Each computer also probably
has a name, which makes it easier for other computers to locate
it, especially if its IP address changes on the network but its name
doesn't.
Q: Can two computers have multiple TCP connections between
them?
A: Certainly. In fact, it is often the case that two computers have mul-
tiple applications running and communicating between each
other. Servers will have HTTP applications, FTP applications, and
so on, all running at the same time.
Q: So if two computers have multiple connections, how do you dis-
tinguish which application you want to communicate with?
A: Well, when data is sent from one application to another, the data
has two values associated with it: the computer's IP address and a
port number . The IP address denotes which computer the data is
intended for, and the port number denotes which application the
data is intended for.
Q: Do you need to associate a port number with all your network
programs?
A: Yes. You will find that port numbers are used throughout the Java
networking APIs, especially in constructors when applications are
starting up.
Search WWH ::




Custom Search