Java Reference
In-Depth Information
31.1 Introduction
Computer networking is used to send and receive messages among computers on the Internet.
Key
Point
To browse the Web or send an email, your computer must be connected to the Internet. The
Internet is the global network of millions of computers. Your computer can connect to the
Internet through an Internet Service Provider (ISP) using a dialup, DSL, or cable modem, or
through a local area network (LAN).
When a computer needs to communicate with another computer, it needs to know the other
computer's address. An Internet Protocol (IP) address uniquely identifies the computer on the
Internet. An IP address consists of four dotted decimal numbers between 0 and 255 , such as
130.254.204.31 . Since it is not easy to remember so many numbers, they are often mapped
to meaningful names called domain names , such as liang.armstrong.edu . Special servers called
Domain Name Servers (DNS) on the Internet translate host names into IP addresses. When a
computer contacts liang.armstrong.edu , it first asks the DNS to translate this domain name into
a numeric IP address and then sends the request using the IP address.
The Internet Protocol is a low-level protocol for delivering data from one computer to
another across the Internet in packets. Two higher-level protocols used in conjunction with
the IP are the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP).
TCP enables two hosts to establish a connection and exchange streams of data. TCP guaran-
tees delivery of data and also guarantees that packets will be delivered in the same order in
which they were sent. UDP is a standard, low-overhead, connectionless, host-to-host protocol
that is used over the IP. UDP allows an application program on one computer to send a data-
gram to an application program on another computer.
Java supports both stream-based and packet-based communications. Stream-based com-
munications use TCP for data transmission, whereas packet-based communications use UDP.
Since TCP can detect lost transmissions and resubmit them, transmissions are lossless and reli-
able. UDP, in contrast, cannot guarantee lossless transmission. Stream-based communications
are used in most areas of Java programming and are the focus of this chapter. Packet-based
communications are introduced in Supplement III.P, Networking Using Datagram Protocol.
IP address
domain name
domain name server
TCP
UDP
stream-based communication
packet-based communication
31.2 Client/Server Computing
Java provides the ServerSocket class for creating a server socket and the Socket
class for creating a client socket. Two programs on the Internet communicate through
a server socket and a client socket using I/O streams.
Key
Point
Networking is tightly integrated in Java. The Java API provides the classes for creating sock-
ets to facilitate program communications over the Internet. Sockets are the endpoints of logi-
cal connections between two hosts and can be used to send and receive data. Java treats socket
communications much as it treats I/O operations; thus, programs can read from or write to
sockets as easily as they can read from or write to files.
Network programming usually involves a server and one or more clients. The client sends
requests to the server, and the server responds. The client begins by attempting to establish a
connection to the server. The server can accept or deny the connection. Once a connection is
established, the client and the server communicate through sockets.
The server must be running when a client attempts to connect to the server. The server
waits for a connection request from the client. The statements needed to create sockets on a
server and on a client are shown in FigureĀ 31.1.
socket
31.2.1 Server Sockets
To establish a server, you need to create a server socket and attach it to a port , which is where
the server listens for connections. The port identifies the TCP service on the socket. Port num-
bers range from 0 to 65536, but port numbers 0 to 1024 are reserved for privileged services.
server socket
port
 
 
 
 
Search WWH ::




Custom Search