Chapter 11
Socket
Programming
in Java
11.1 . Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 11: Outline
11.1 INTRODUCTION
11.2 PROGRAMMING WITH UDP
11.3 PROGRAMMING WITH TCP
11.2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 11: Objective
We show how entities such as IP addresses, ports, and socket
addresses are represented by corresponding classes in Java.
We introduce classes in Java that are used in UDP
programming. We then show how we can write simple client-
server programs using the iterative approach. Next, we show
how we can change the server program using the concurrent
approach.
We introduce classes in Java that are used in TCP
programming. We then show how we can write simple client-
server programs using the iterative approach. Finally, we show
how we can change the server program using the concurrent
approach.
11.3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11-1 INTRODUCTION
In this section we discuss how general ideas in
C network programming, which we discussed
in Chapter 2, can be used in Java network
programming.
11.4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.1.1 Addresses and Ports
Network programming in any language definitely
needs to deal with IP addresses and port numbers.
We briefly introduce how addresses and ports are
represented in Java. We recommend that the reader
compare the representations of these two entities in
C and Java.
IP Addresses
Port Numbers
InetSocketAddress
11.5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.1: Summary of InetAddress class
11.6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.1
In this example, we show how we use the second and the
third static methods to get the InetAddress of a site and the
local host (Table 11.2). In line 7, we use the second static
method to get the IP address of the site “forouzan.biz”. In
line 9, we pass an IP address, as a string, to the getByName
method to change it to an InetAddress object. Lines 11 to 13
print the above addresses as stored in the InetAddress
objects. We can use the getHostAddress method to extract
the address part of an InetAddress object as a string in line
15. In line 16, we use the getHostName method to find the
name of a host given the address (using the DNS again).
11.7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.2: Example 11.1
11.8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.2: Example 11.1 (continued)
11.9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.2
In this example, we show how we use the second and the
third static methods to get the InetAddress of a site and the
local host (Table 11.2).
11.10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.3: Example 11.2
11.11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.4: Summary of InetSocketAddress class
11.12 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.3
The program in Table 11.5 shows how we can create a
socket address. Note that the port number is separated by a
colon from the InetAddress in this presentation.
11.13 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.5: Example 11.3
11.14 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.1.2 Client-Server Paradigm
A server in a client-server paradigm can be designed
either as an iterative server or a concurrent server.
An iterative server handles the clients one by one. A
concurrent server can simultaneously serve as many
clients as the computer resources permit.
Client and Server Programs
Socket Interface in Java
11.15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11-2 PROGRAMMING WITH UDP
To be consistent with the socket programming
section of Chapter 2, we first discuss network
programming using the service of UDP, a
connectionless service. We talk about the iterative
approach first and concurrent approach next.
11.16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.2.1 Iterative Approach
UDP provides a connectionless service and
communication is done using chunks of data
called user datagrams. In the iterative
approach, the server serves one datagram at a
time. The rest of the arrived datagrams need to
wait, no matter whether they are coming from
the same client or other clients.
11.17 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.2.1 (continued)
Sockets Used for UDP
Classes
DatagramSocket Class
DatagramPacket Class
UDP Client Design
Client Program
The main Method
The Methods in UDPClient Class
11.18 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.2.1 (continued)
UDP Server
Server Program
The main Method
The Methods in UDPServer Class
11.19 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 11.1: Sockets for UDP communication
1 Request
Response 2
3 Request
Response 4
11.20 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.6: Some methods in DatagramSocket class
11.21 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.7: Some methods in DatagramPacket class
11.22 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 11.2: Design of the UDP Client
11.23 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.8: A simple UDP client program
11.24 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.8: A simple UDP client program (continued)
11.25 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.8: A simple UDP client program (continued)
11.26 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.8: A simple UDP client program (continued)
11.27 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 11.3: Design of the UDP Server
11.28 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.9: A simple UDP server program
11.29 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.9: A simple UDP server program (continued)
11.30 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.9: A simple UDP server program (continued)
11.31 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.9: A simple UDP server program (continued)
11.32 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.4
The simplest example is to simulate the standard echo
client/server. A short message is sent by the client. The
message is exactly echoed back. Although the standard uses
the well-known port 7, to simulate it, we use the port
number 52007 for the server.
11.33 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.4 (Continued)
11.34 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.5
In this example, we change our server to a simple date/time
server. It returns the local date and time at the location
where the server is running.
11.35 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.5 (continued)
11.36 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.6
In this example, we need to use our simple client-server
program to measure the time (in milliseconds) that it takes
to send a message from the client to the server.
11.37 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.6 (continued)
11.38 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.2.2 Concurrent Approach
The iterative approach to the UDP server
program is good enough for most applications
because, after processing and sending one
datagram, the server is ready to serve other
clients. However, if the processing of a
datagram takes a long time, a client may
monopolize the server. The concurrent server
programs were designed to solve this problem
using threads.
Server Program
11.39 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.10: A concurrent UDP server program
11.40 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.10: A concurrent UDP server program (continued)
11.41 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.10: A concurrent UDP server program (continued)
11.42 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.10: A concurrent UDP server program (continued)
11.43 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.7
We repeat Example 11.5 using the concurrent approach. We
need several computers to simultaneously send the request
and get the response.
11.44 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Example 11.8
We repeat Example 11.6 using the concurrent approach. We
need several computers to simultaneously send the request
and get the response.
11.45 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11-3 PROGRAMMING WITH TCP
We are now ready to discuss network
programming using the service of TCP, a
connection-oriented service. We first discuss
how to write a client and a server program
using the iterative approach. We then show how
we can change the server program to make it
concurrent.
11.46 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.3.1 Iterative Approach
Although the iterative approach to TCP
programming is rare, it is the foundation of the
concurrent approach. In this approach, a server
handles clients one by one. When the server starts
serving a client, the other clients need to wait.
11.47 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.3.1 (continued)
Two Types of Sockets
Classes
ServerSocket Class
Socket Class
TCP Client Design
TCP Client Program
The main Method
The Methods in TCPClient Class
11.48 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 11.4: ServerSocket and Socket objects in TCP communication
1 Connection establishment
3
2
Data transfer and termination
Create
4 Connection establishment
6
5
Data transfer and termination
Create
11.49 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.11: Summary of ServerSocket class
11.50 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.12: Summary of Socket class
11.51 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 11.5: Design of the TCP Client
11.52 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.13: A simple TCP client program
11.53 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.13: A simple TCP client program (continued)
11.54 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.13: A simple TCP client program (continued)
11.55 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.13: A simple TCP client program (continued)
11.56 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 11.6: Design of the TCP Server for each client connection
11.57 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.14: A simple TCP server program
11.58 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.14: A simple TCP server program (continued)
11.59 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.14: A simple TCP server program (continued)
11.60 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.14: A simple TCP server program (continued)
11.61 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11.3.2 Concurrent Approach
The iterative approach to the TCP server program
can allow a client to monopolize a server and does
not allow the server to pay attention to the
demands of other clients. The concurrent server
programs were designed to solve this problem. In
Java, this task is done using multiple threads.
Server Program
11.62 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.15: A concurrent TCP server program
11.63 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.15: A concurrent TCP server program (continued)
11.64 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.15: A concurrent TCP server program (continued)
11.65 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Table 11.15: A concurrent TCP server program (continued)
11.66 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 11: Summary
Network programming definitely needs to deal with IP addresses
and port numbers. In Java, an IP address is an instance of the
InetAddress class. A port number is represented as an integer. In
Java, a socket address, a combination of an IP address and a
port number, is represented by the SocketAddress class.
In the client-server paradigm, communication occurs between
two application programs, a client and a server. A client is a
finite program that requests service from a server. A server in a
client-server paradigm can be designed either as an iterative
server or as a concurrent server. An iterative server handles the
clients one by one. A concurrent server can simultaneously serve
as many clients as the computer resources permit.
11.67 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 11: Summary (continued)
Although there are a few ways to write a client or server
application program, we discussed only the socket interface
approach. The whole idea is to create a new abstract layer, the
socket interface layer, between the operating system and the
application layer.
Java implementation of application programming with UDP
uses two classes: DatagramSocket and DatagramPacket. The
first is to create a socket object; the second is to create the
datagrams exchanged. Java implementation of application
programming with TCP uses two classes: ServerSocket and
Socket. The first is used only during the connection
establishment; the second is used for the rest of the
communication.
11.68 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.