A simple UDP server program (Part I)

advertisement
Chapter 25
Introduction
To
Application
Layer
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 25: Outline
25.1 INTRODUCTION
25.2 CLIENT-SERVER PROGRAMMING
25.3 ITERATIVE PROGRAMMING IN C
25.4 ITERATIVE PROGRAMMING IN JAVA
Chapter 25: Objective
 The first section introduces the application layer. The section
first describes the services provided by the application layer.
It then explains that there are two paradigms in which hosts
in the Internet can exchange services: the client-server
paradigm and the peer-to-peer paradigm.
 The second section introduces client-server programming,
the way in which the client-server paradigm can be
implemented. It first describes application programming
interfaces. It then shows how application programs use the
services of the transport layer. It finally defines iterative
communication using UDP or TCP.
Chapter 25: Objective (continued)
 The third section gives a brief discussion of socket
programming in C. After discussing general issues, it
introduces how iterative client-server programs can be
written in this language.
 The fourth section gives a brief discussion of socket
programming in Java. After discussing general issues, it
introduces how iterative client-server programs can be
written in this language.
25-1 INTRODUCTION
The application layer provides services
to the user. Communication is provided
using a logical connection, which means
that the two application layers assume
that there is an imaginary direct
connection through which they can send
and receive messages. Figure 25.1
shows the idea behind this logical
connection.
25.5
Figure 25.1: Logical connection at the application layer
25.6
25.25.1 Providing Services
All communication networks that started before the
Internet were designed to provide services to network
users. Most of these networks, however, were
originally designed to provide one specific service.
For example, the telephone network was originally
designed to provide voice service: to allow people all
over the world to talk to each other. This network,
however, was later used for some other services,
such as facsimile (fax), enabled by users adding
some extra hardware at both ends.
25.7
25.25.2 Application-Layer Paradigms
It should be clear that to use the Internet we need
two application programs to interact with each
other: one running on a computer somewhere in the
world, the other running on another computer
somewhere else in the world. The two programs
need to send messages to each other through the
Internet infrastructure. However, we have not
discussed what the relationship should be between
these programs. Two paradigms have been
developed : the client-server paradigm and the peerto-peer paradigm. We briefly introduce these two
paradigms here.
25.8
Figure 25.2: Example of a client-server paradigm
25.9
Figure 25.3: Example of a peer-to-peer paradigm
25.10
25-2 CLIENT-SERVER PROGRAMMING
In this paradigm, communication at the
application layer is between two running
application programs called processes: a
client and a server. A client is a running
program that initializes the communication
by sending a request; a server is another
application program that waits for a
request from a client.
25.11
25.2.1 API
How can a client process communicate with a server
process? A computer program is normally written in
a computer language with a predefined set of
instructions that tells the computer what to do. If we
need a process to be able to communicate with
another process, we need a new set of instructions to
tell the lowest four layers of the TCP/IP suite to
open the connection, send and receive data from the
other end, and close the connection. A set of
instructions of this kind is normally referred to as an
application programming interface (API).
25.12
Figure 25.4: Position of the socket interface
25.13
Figure 25.5: A Sockets used like other sources and sinks
25.14
Figure 25.6: Use of sockets in process-to-process communication
25.15
Figure 25.7: A socket address
25.16
25.2.2 Using Transport Layer
A pair of processes provide services to the users of
the Internet, human or programs. A pair of
processes, however, need to use the services provided
by the transport layer for communication because
there is no physical communication at the
application layer. As we discussed in Chapters 23
and 24, there are three common transport-layer
protocols in the TCP/IP suite: UDP, TCP, and SCTP.
Most standard applications have been designed to
use the services of one of these protocols.
25.17
25.2.3 Iterative Using UDP
An iterative server can process one client request at
a time; it receives a request, processes it, and sends
the response to the requestor before handling
another request. When the server is handling the
request from a client, the requests from other clients,
and even other requests from the same client, need
to be queued at the server site and wait for the server
to be freed. The received and queued requests are
handled in the first-in, first-out fashion. In this
section, we discuss iterative communication using
UDP.
25.18
Figure 25.8: Sockets for UDP communication
25.19
Figure 25.9: Flow diagram for iterative UDP communication
25.20
25.2.4 Iterative Using TCP
Although iterative communication using TCP is not
very common, because it is simpler we discuss this
type of communication in this section.
25.21
Figure 25.10: Sockets used in TCP communication
2
Create
5
Create
25.22
Figure 25.11: Flow diagram for iterative TCP communication
25.23
25.2.5 Concurrent Communication
A concurrent server can process several client
requests at the same time. This can be done using
the available provisions in the underlying
programming language. In C, a server can create
several child processes, in which a child can handle
a client. In Java, threading allows several clients to
be handled by each thread. We do not discuss
concurrent server communication in this chapter,
but we briefly discuss it in the book website in the
Extra Material section.
25.24
25-3 ITERATIVE PROGRAMMING IN C
In this section, we show how to write
some simple iterative client-server
programs using C. The section can be
skipped if the reader is not familiar with
the C language. The C language better
reveals some subtleties in this type of
programming.
25.25
25.3.1 General Issues
The important issue in socket interface is to
understand the role of a socket in communication.
The socket has no buffer to store data to be sent or
received. It is capable of neither sending nor
receiving data. The socket just acts as a reference or
a label. The buffers and necessary variables are
created inside the operating system.
25.26
Figure 25.12: Socket data structure
25.27
Header Files
25.28
25.3.2 Iter. Programs Using UDP
As we discussed earlier, UDP provides a
connectionless server, in which a client sends a
request and the server sends back a response.
25.29
Table 25.1: Echo server program using UDP
25.30
Table 25.1: Echo server program using UDP (continued)
25.31
Table 25.2: Echo client program using UDP
25.32
Table 25.2: Echo client program using UDP (continued)
25.33
25.3.3 Iter. Programming Using TCP
As we described before, TCP is a connectionoriented protocol. Before sending or receiving data,
a connection needs to be established between the
client and the server.
25.34
Figure 25.13: Flow diagram for data-transfer boxes
25.35
Figure 25.14: Buffer used for receiving
25.36
Table 25.3: Echo server program using TCP (part I)
25.37
Table 25.3: Echo server program using TCP (part II)
25.38
Table 25.3: Echo server program using TCP (part III)
25.39
Table 25.4: Echo client program using TCP (Part I)
25.40
Table 25.4: Echo client program using TCP (Part II)
25.41
Table 25.4: Echo server program using TCP (Part III)
25.42
25-4 ITERATIVE PROGRAMMING IN JAVA
We have chosen the Java language
because many aspects of programming
can be easily shown using the powerful
classes available in Java. We touch the
main issues in programming using the
traditional socket interface API, but they
can be extended to other areas of
network programming without difficulty.
25.43
25.4.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.
25.44
Table 25.5: Summary of InetAddress class
25.45
Example 25.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 25.6).
25.46
Table 25.6: Example 25.1
25.47
Example 25.2
The program in Table 25.7 shows that if we use a variable of
type short to store a port number, we may get a negative
value. A variable of type integer gives us a correct value.
25.48
Table 25.7: Example 25.2
25.49
Table 25.8: Summary of InetAddress class
25.50
Example 25.3
The program in Table 25.9 shows how we can create a
socket address. Note that the port number is separated by a
colon from the InetAddress in this presentation.
25.51
Table 25.9: Example 25.3
25.52
25.4.2 Iter. Programming Using UDP
To be consistent with the C socket programming
section above, we first discuss Java programming
using the service of UDP, a connectionless service.
25.53
Table 25.10: Some methods in DatagramSocket class
25.54
Table 25.11: Some methods in DatagramPacket class
25.55
Figure 25.15: Design of the UDP server
25.56
Table 25.12: A simple UDP server program (Part I)
25.57
Table 25.12: A simple UDP server program (Part II)
25.58
Table 25.12: A simple UDP server program (Part IV)
25.59
Table 25.12: A simple UDP server program (Part I)
25.60
Example 25.4
The simplest example is to simulate the standard echo
client/server as we discussed in the previous section. This
program is used to check whether a server is alive. 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.
25.61
Example 25.4 (Continued)
25. In the client program, we set the server port to 52007,
and the server name to the computer name or the computer
address (x.y.z.t). We also change the makeRequest and
useResponse methods as shown below:
25.62
Example 25.4 (Continued)
2. In the server program (Table 25.12), we add one
statement at the beginning of the program to be able
to use the Calendar and the Date class (import
java.util.*;). We set the server port to 40013. We also
replace the process methods in the server program to
25.63
Example 25.4 (Continued)
3. We let the server program run on one host and then
run the client program on another host. We can use
both on the same host if we run the server program in
the background.
25.64
Example 25.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.
25.65
Example 25.5 (Continued)
25. In the client program, we set the server port to 40013
and set the server name to the computer name or the
computer address (“x.y.z.t”). We also replace makeRequest
and useResponse methods using the following code:
25.66
Example 25.5 (Continued)
2. In the server program (Table 25.12), we add one
statement at the beginning of the program to be able
to use the Calendar and the Date class (import
java.util.*;). We set the server port to 40013. We also
replace the process methods in the server program to
25.67
Example 25.5 (Continued)
3. The process method uses the Calendar class to get
the time (including the date) and then changes the
date to a string to be stored in the response variable.
4. We let the server program run on one host and then
run the client program on another host. We can use
both on the same host if we run the server program in
the background
25.68
Example 25.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.
25.69
Example 25.6 (Continued)
25. In the client program, we add one statement at the
beginning of the program to be able to use the Date class
(import java.util.*;), we set the server port to 40013, and we
set the server name to the computer name or the computer
address (“x.y.z.t”). We also replace makeRequest and
useResponse methods using the following code:
25.70
Example 25.6 (Continued)
2. In the server program, we set the server port to
40013. We also replace the process methods
in the server program to
3. We let the server program run on one host and then
run the client program on another host. We can use
both on the same host if we run the server program in
the background.
25.71
Figure 25.16: Design of the UDP Client
Create request
Use response
String
String
sendBuff
DatagramPacket sendPacket
object
25.72
recvBuff
recvPacket DatagramPacket
object
Table 25.13: A simple UDP client program (Part I)
25.73
Table 25.13: A simple UDP client program (Part II)
25.74
Table 25.13: A simple UDP client program (Part IV)
25.75
Table 25.13: A simple UDP client program (Part I)
25.76
25.4.3 Iter. Programming Using TCP
We are now ready to discuss network programming
using the service of TCP, a connection-oriented
service.
25.77
Table 25.14: Summary of ServerSocket class
25.78
Table 25.15: Summary of Socket class
25.79
Figure 25.17: Design of the TCP server for each client connection
25.80
Table 25.16: A simple TCP server program (Part I)
25.81
Table 25.16: A simple TCP server program (Part II)
25.82
Table 25.16: A simple TCP server program (Part III)
25.83
Table 25.16: A simple TCP server program (Part IV)
25.84
Figure 25.18: Design of the TCP client
Client application program
Create request
Use response
String
String
sendBuff
recvBuff
Bytes
Bytes
recvStream
sendStream
Socket
Transport layer (TCP)
25.85
Table 25.17: A simple TCP client program (Part I)
25.86
Table 25.17: A simple TCP client program (Part II)
25.87
Table 25.17: A simple TCP client program (Part III)
25.88
Table 25.17: A simple TCP client program (Part IV)
25.89
Download