chapter 11 socket programming in java · 2017-01-18 · chapter 11: objective we show how entities...
TRANSCRIPT
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.1 .
Chapter 11
Socket
Programming
in Java
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.2
Chapter 11: Outline
11.1 INTRODUCTION
11.2 PROGRAMMING WITH UDP
11.3 PROGRAMMING WITH TCP
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.3
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.4
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.5
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.6
Table 11.1: Summary of InetAddress class
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.7
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).
Example 11.1
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.8
Table 11.2: Example 11.1
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.9
Table 11.2: Example 11.1 (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.10
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).
Example 11.2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.11
Table 11.3: Example 11.2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.12
Table 11.4: Summary of InetSocketAddress class
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.13
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.
Example 11.3
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.14
Table 11.5: Example 11.3
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.15
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.16
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.17
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.18
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.19
11.2.1 (continued)
UDP Server
The main Method
The Methods in UDPServer Class
Server Program
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.20
Figure 11.1: Sockets for UDP communication
1 Request
2Response
4Response
3 Request
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.21
Table 11.6: Some methods in DatagramSocket class
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.22
Table 11.7: Some methods in DatagramPacket class
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.23
Figure 11.2: Design of the UDP Client
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.24
Table 11.8: A simple UDP client program
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.25
Table 11.8: A simple UDP client program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.26
Table 11.8: A simple UDP client program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.27
Table 11.8: A simple UDP client program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.28
Figure 11.3: Design of the UDP Server
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.29
Table 11.9: A simple UDP server program
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.30
Table 11.9: A simple UDP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.31
Table 11.9: A simple UDP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.32
Table 11.9: A simple UDP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.33
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.
Example 11.4
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.34
Example 11.4 (Continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.35
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.
Example 11.5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.36
Example 11.5 (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.37
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.
Example 11.6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.38
Example 11.6 (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.39
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.40
Table 11.10: A concurrent UDP server program
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.41
Table 11.10: A concurrent UDP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.42
Table 11.10: A concurrent UDP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.43
Table 11.10: A concurrent UDP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.44
We repeat Example 11.5 using the concurrent approach. We
need several computers to simultaneously send the request
and get the response.
Example 11.7
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.45
We repeat Example 11.6 using the concurrent approach. We
need several computers to simultaneously send the request
and get the response.
Example 11.8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.46
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.47
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.48
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.49
Figure 11.4: ServerSocket and Socket objects in TCP communication
Connection establishment1
Connection establishment4
Create
2Data transfer and termination
3
Data transfer and termination
6
Create
5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.50
Table 11.11: Summary of ServerSocket class
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.51
Table 11.12: Summary of Socket class
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.52
Figure 11.5: Design of the TCP Client
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.53
Table 11.13: A simple TCP client program
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.54
Table 11.13: A simple TCP client program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.55
Table 11.13: A simple TCP client program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.56
Table 11.13: A simple TCP client program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.57
Figure 11.6: Design of the TCP Server for each client connection
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.58
Table 11.14: A simple TCP server program
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.59
Table 11.14: A simple TCP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.60
Table 11.14: A simple TCP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.61
Table 11.14: A simple TCP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.62
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.63
Table 11.15: A concurrent TCP server program
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.64
Table 11.15: A concurrent TCP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.65
Table 11.15: A concurrent TCP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.66
Table 11.15: A concurrent TCP server program (continued)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.67
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.11.68
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.