Java Reference
In-Depth Information
19.2
Networking with Stream Sockets
Since in order to speak, one must first listen, learn to speak by listening.
MEVLANA RUMI
When computers want to communicate with each other over a network, each computer
must speak the same “language.” In other words, the computers need to communicate
using the same protocol . One of the most common protocols today is TCP , or the
Transmission Control Protocol . For example, the HTTP protocol used to transmit
Web pages is based on TCP. TCP is a stream-based protocol in which a stream of data
is transmitted from the sender to the receiver. TCP is considered a reliable protocol
because it guarantees that data from the sender is received in the same order in which it
was sent. An analogy to TCP is the telephone system. A connection is made when the
phone is dialed and the participants communicate by speaking back and forth. In TCP,
the receiver must first be listening for a connection, the sender initiates the connection,
and then the sender and receiver can transmit data. The program that is waiting for a
connection is called the server and the program that initiates the connection is called
the client .
An alternate protocol is UDP , or the User Datagram Protocol . In UDP, packets of
data are transmitted but no guarantee is made regarding the order in which the packets
are received. An analogy to UDP is the postal system. Letters that are sent might be
received in an unpredictable order, or lost entirely with no notification. Although Java
provides support for UDP, we will only introduce TCP in this section.
Transmission
Control
Protocol
(TCP)
server
client
User
Datagram
Protocol
(UDP)
Sockets
Network programming is implemented in Java using sockets . A socket describes one
end of the connection between two programs over the network. A socket consists
of an address that identifies the remote computer and a port for both the local and
remote computer. The port is assigned an integer value between 0 and 65,535 that is
used to identify which program should handle data received from the network. Two
applications may not bind to the same port. Typically, ports 0 to 1,024 are reserved for
use by well-known services implemented by your operating system.
The process of client/server communication is shown in Display 19.6. First, the
server waits for a connection by listening on a specific port. When a client connects to
this port, a new socket is created that identifies the remote computer, the remote port,
and the local port. A similar socket is created on the client. Once the sockets are created
on both the client and the server, data can be transmitted using streams in a manner
very similar to the way we implemented file I/O in Chapter 10.
Display 19.7 shows how to create a simple server that listens on port 7654 for a
connection. Once it receives a connection, a new socket is returned by the accept( )
method. From this socket, we create a BufferedReader , just as if we were reading
from a text file described in Chapter 10. Data is transmitted to the socket using a
DataOutputStream , which is similar to a FileOutputStream . The ServerSocket
sockets
port
 
 
Search WWH ::




Custom Search