Java Reference
In-Depth Information
addressisoftenreferredtoasanInternetProtocolVersion6(IPv6)address(see
ht-
Port numbers
are 16-bit unsigned integers that uniquely identify processes, which
are the sources orrecipients ofmessages. Portnumbers less than 1024are reserved
for standard processes. For example, port number 25 has traditionally identified
the Simple Mail Transfer Protocol (SMTP) process for sending email, although
port number 587 is commonly being used these days (see
ht-
TCP is used to create an ongoing conversation between two hosts by sending mes-
sages back and forth. Before this conversation can occur, a connection must be estab-
lishedbetweenthesehosts.Afterthisconnectionhasbeenestablished,TCPentersapat-
ternofsendingamessagepacketandwaitingforareplythatthepacketarrivedcorrectly
(orforatimeouttoexpirewhenthereplydoesn'tarrivebecauseofanetworkproblem).
This send/reply cycle guarantees a reliable connection.
Because it can take time to establish a connection, and because it also takes time to
sendpacketsbecauseoftheneedtoreceivereplyacknowledgments(ortimeouts),TCP
isfairlyslow.UDP,whichdoesn'trequireconnectionsandpacketacknowledgement,is
muchfasterthanTCP.However,UDPisn'tasreliable(there'snoguaranteethatapacket
will arrive correctly or even arrive) as TCP because there's no acknowledgment. Fur-
thermore, UDP is limited to single-packet one-way conversations.
The
java.net
package provides
Socket
and
ServerSocket
classes for per-
forming TCP-based communications. It also provides
DatagramSocket
,
Data-
gramPacket
, and
MulticastSocket
classes for performing UDP communica-
tions.
MulticastSocket
is a subclass of
DatagramSocket
.
Socket Addresses
Instancesofthe
Socket
-suffixedclassesareassociatedwith
socket addresses
thatare
comprised of IP addresses and port numbers.
The
Socket
classreliesonthe
java.net.InetAddress
classtorepresentthe
IPv4orIPv6addressportionofthesocketaddress.Itrepresentstheportnumbersepar-
ately. (The other
Socket
-suffixed classes also take advantage of
InetAddress
.)
Note
InetAddress
reliesonits
java.net.Inet4Address
subclasstorep-
resentanIPv4addressandonits
java.net.Inet6Address
subclasstorepresent
an IPv6 address.