Java Reference
In-Depth Information
collection of classes and interfaces that provide the low-level communication
details, allowing you to write programs that focus on solving the problem at
hand. The java.net package provides support for the two common network
protocols:
TCP. TCP stands for Transmission Control Protocol, which allows for
reliable communication between two applications. TCP is typically used
over the Internet Protocol, which is referred to as TCP/IP.
UDP. UDP stands for User Datagram Protocol, a connection-less protocol
that allows for packets of data to be transmitted between applications.
In the following sections, we take a look at the way these two protocols
compare.
Transmission Control Protocol
Transmission Control Protocol (TCP) is often compared to making a telephone
call. If you want to telephone someone, the person needs to have a phone, needs
a phone number, and needs to be waiting for an incoming call. After the person
you are calling answers the telephone, you now have a reliable, two-way com-
munication stream, allowing either person to talk to the other (even at the same
time). If one person hangs up the phone, the communication is over.
With a TCP network connection, the client computer is similar to the person
placing the telephone call, and the server computer is similar to the person
waiting for a call. When the client attempts to connect to the server, the server
needs to be running, needs to have an address on the network, and needs to be
waiting for an incoming connection on a port. When a TCP connection is estab-
lished, the client and server have a reliable, two-way communication stream
that allows data to be transmitted in either direction. The two computers can
communicate until the connection is closed or lost.
The java.net.ServerSocket and java.net.Socket classes are the only two
classes you will probably ever need to create a TCP/IP connection between
two computers unless you require a secure connection, in which case you
would use the SSLServerSocket and SSLSocket classes in the javax.net.ssl
package. I will discuss both of these techniques later in this chapter.
User Datagram Protocol
User Datagram Protocol (UDP) provides a protocol for sending packets of data
called datagrams between applications. If TCP is similar to placing a telephone
call, UDP can be compared to mailing someone a letter. The datagram packet
is like a letter, where a client sends a datagram to a server without actually
connecting to the server. This makes UDP an unreliable communication proto-
col when compared to TCP, where the client and server are directly connected.
Search WWH ::




Custom Search