程序代写 SEHH2238 Computer Networking

Appendix – Socket Programming
(reference only, not exam)
Textbook: Ch. 25
SEHH2238 Computer Networking

Copyright By PowCoder代写 加微信 powcoder

SEHH2238 L12 Appendix 1

SEHH2238 Computer Networking
􏰀 Application using UDP 􏰁UDP server socket program 􏰁UDP client socket program
􏰀 Application using TCP 􏰁TCP server socket program 􏰁TCP client socket program
SEHH2238 L12 Appendix 2

SEHH2238 Computer Networking
Demo using Java
􏰀 We follow the textbook to use Java as example. 􏰁Using other languages would be similar.
􏰀 In Java, an IP address is defined as an object, the instance of InetAddress class.
􏰁to deal with IP addresses and port numbers. 􏰀 For details,
􏰁 http://docs.oracle.com/javase/7/docs/api/java/net/InetAd dress.html
SEHH2238 L12 Appendix 3

SEHH2238 Computer Networking
Iterative Programming Using UDP
􏰀 Aniterativeservercanprocessoneclientrequestata 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.
Compare to
concurrent server
SEHH2238 L12 Appendix 4

SEHH2238 Computer Networking
UDP communication
Figure 25.8: Sockets for UDP communication
SEHH2238 L12 Appendix 5

SEHH2238 Computer Networking
Get IP Addresses
SEHH2238 L12 Appendix 6

SEHH2238 Computer Networking
First Example – Classes
􏰀 getByName(String host)
􏰁Determines the IP address of a host, given the host’s
􏰀 getLocalHost()
􏰁Returns the address of the local host. 􏰀 getHostAddress()
􏰁Returns the IP address string in textual presentation. 􏰀 public String getCanonicalHostName()
􏰁Gets the fully qualified domain name for this IP address. SEHH2238 L12 Appendix 7

SEHH2238 Computer Networking
Socket Address
􏰀 This class implements an IP Socket Address (IP address + port number)
􏰁 http://download.java.net/jdk7/archive/b123/docs/api/java/net/InetSocketAddress.html SEHH2238 L12 Appendix 8

SEHH2238 Computer Networking
Figure 25.9: Flow diagram for iterative UDP communication
SEHH2238 L12 Appendix 9

SEHH2238 Computer Networking
Classes for UDP
􏰀 DatagramSocket
􏰁DatagramSocket Class is used to create sockets in the
client and the server.
􏰁It also provides methods to send a datagram, to receive a datagram, and to close the socket.
Table 25.10: Some methods in DatagramSocket class
SEHH2238 L12 Appendix 10

SEHH2238 Computer Networking
Classes for UDP
􏰀 DatagramPacket Class 􏰁DatagramPacket class is used to create
datagram packets.
Table 25.11: Some methods in DatagramPacket class
SEHH2238 L12 Appendix 11

SEHH2238 Computer Networking
Figure 25.15:
SEHH2238 L12 Appendix 12
Design of the UDP server

SEHH2238 Computer Networking
Table 25.12:
A simple UDP server program (Part I)
SEHH2238 L12 Appendix
Allocate buffer

SEHH2238 Computer Networking
Table 25.12:
A simple UDP server program (Part II)
SEHH2238 L12 Appendix
Wait for receiving

SEHH2238 Computer Networking
Table 25.12:
A simple UDP server program (Part III)
SEHH2238 L12 Appendix 15

SEHH2238 Computer Networking
Table 25.12:
A simple UDP server program (Part IV)
SEHH2238 L12 Appendix 16

SEHH2238 Computer Networking
UDP Server Program Structure
􏰀 main method
􏰁It creates an instance of the DatagramSocket
class using the defined port number. 􏰁Then it runs an infinite loop.
􏰀Each client is served in one iteration of the loop by 􏰁Create a new instance of the UDPServer class and 􏰁Call its three instance methods
􏰀 getRequest() 􏰀 process()
􏰀 sendResponse()
SEHH2238 L12 Appendix 17

SEHH2238 Computer Networking
UDP Server Program Structure
􏰀 getRequestmethod
􏰁 Creates a receiver buffer (line 22)
􏰁 Creates the datagram packet and attaches it to buffer (line 23)
􏰁 Receives the datagram contents (line 24).
􏰁 Extracts the data part of the datagram and stores it in the buffer (line 25).
􏰁 Converts the bytes in the receiver buffer to the string request (line 26).
􏰁 Extracts the IP address of the client that sends the packet (line 27).
􏰁 Extracts the port number of the client that sends the request (line 28).
SEHH2238 L12 Appendix 18

SEHH2238 Computer Networking
UDP Server Program Structure
􏰀 sendRequestmethod
􏰁 Creates an empty send buffer (line 49).
􏰁 Changes the response string to bytes and stores them in the send buffer (line 50).
􏰁 Creates a new datagram and fills it with data in the buffer (line 51 and 52).
􏰁 Sends the datagram packet (line 53). 􏰀 processmethod
􏰁 To be worked out as application specific.
SEHH2238 L12 Appendix 19

SEHH2238 Computer Networking
Figure 25.16:
Create request String
Use response String
Design of the UDP Client
DatagramPacket sendPacket object
recvPacket DatagramPacket object
recv EHH2238 L12 Appendix

SEHH2238 Computer Networking
Table 25.13:
A simple UDP client program (Part I)
SEHH2238 L12 Appendix 21

SEHH2238 Computer Networking
Table 25.13:
A simple UDP client program (Part II)
Wait for receiving SEHH2238 L12 Appendix 22

SEHH2238 Computer Networking
Table 25.13:
A simple UDP client program (Part III)
SEHH2238 L12 Appendix 23

SEHH2238 Computer Networking
Table 25.13:
A simple UDP client program (Part IV)
SEHH2238 L12 Appendix 24

SEHH2238 Computer Networking
UDP Client Program Structure
􏰀 main method
􏰁Creates an instance of the DatagramSocket.
􏰁Creates an instance of UDPClient class using the socket, server Name and server port number.
􏰁Then it call its five instance methods 􏰀 makeRequest()
􏰀 sendRequest()
􏰀 getResponse()
􏰀 useResponse() 􏰀 close()
SEHH2238 L12 Appendix 25

SEHH2238 Computer Networking
UDP Client Program Structure
􏰀 makeRequest method
􏰁 To be worked out as application specific to create request string.
􏰀 sendRequestmethod
􏰁 Creates an empty send buffer (line 30).
􏰁 Fills up the send buffer with the request string created in the makeRequest method (line 31).
􏰁 Creates the datagram packet and attaches it to send buffer, server address, and the server port (line 32 and 33).
􏰁 Sends the packet using the send method defined in the DatagramSocket class (line 34).
SEHH2238 L12 Appendix 26

SEHH2238 Computer Networking
UDP Client Program Structure
􏰀 getResponsemethod
􏰁 Creates an empty send buffer (line 46).
􏰁 Creates the receive datagram and attaches it to send buffer (line 47)
􏰁 Uses the receive method of the DatagramSocket to receive the response of the server and fills up the datagram with it (line 48).
􏰁 Extracts the data in the receive packet and stores it in the receive buffer (line 49).
􏰁 Creates the string response to be used by the useResponse method (line 50).
􏰀 useResponsemethod
􏰁 To be worked out as application specific.
􏰀 close method 􏰁 Close the socket.
SEHH2238 L12 Appendix 27

SEHH2238 Computer Networking
Example 25.4
Echo client/server
􏰀 The simplest example is to simulate the standard echo client/server.
􏰀 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.
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Example 25.4 (Continued)
Client Program
1. 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:
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Example 25.4 (Continued)
Server Program
2. In the server, we set the server port to 52007.
We also replace the process methods in the server program to
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
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.
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Example 25.5
Simple date/time service – Client
1. 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:
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Example 25.5 (Continued)
Simple date/time service – Server
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
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
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
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Example 25.6
Measuring the time
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.
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Example 25.6 (Continued)
Client Program
1. 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 52007, 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:
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Example 25.6 (Continued)
Server Program
2. In the server program, we set the server port to 52007. 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.
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
D. Iterative Programming Using TCP
􏰀 We are now ready to discuss network programming using the service of TCP, a connection-oriented service.
􏰀 Two classes designed for TCP: 􏰁ServerSocket Class
􏰀Create the listen sockets that are used for establishing communication in TCP (handshaking).
􏰁Socket Class
􏰀Used in TCP for data transfer.
SEHH2238 L12 Appendix

SEHH2238 Computer Networking
Figure 25.11: Flow diagram for iterative TCP communication
SEHH2238 L12 Appendix
Create TCP connection with each client, put them into a queue and wait.
Accept and process each connection one by one.

SEHH2238 Computer Networking
Figure 25.17:
Design of the TCP server for each client connection
SEHH2238 L12 Appendix 40

SEHH2238 Computer Networking
Table 25.16: A simple TCP server program (Part I)
SEHH2238 L12 Appendix 41

SEHH2238 Computer Networking
Table 25.16: A simple TCP server program (Part II)
SEHH2238 L12 Appendix
Wait until there is data received

SEHH2238 Computer Networking
Table 25.16: A simple TCP server program (Part III)
SEHH2238 L12 Appendix 43

SEHH2238 Computer Networking
Table 25.16: A simple TCP server program (Part IV)
SEHH2238 L12 Appendix 44

SEHH2238 Computer Networking
Figure 25.18: Design of the TCP client
Create request String
Use response String
Bytes sendStream
Bytes recvStream
recv ocket
Transport layer (TCP)
SEHH2238 L12 Appendix
Client application program

SEHH2238 Computer Networking
Table 25.17: A simple TCP client program (Part I)
SEHH2238 L12 Appendix 46

SEHH2238 Computer Networking
Table 25.17: A simple TCP client program (Part II)
SEHH2238 L12 Appendix 47

SEHH2238 Computer Networking
Table 25.17: A simple TCP client program (Part III)
SEHH2238 L12 Appendix
Wait until there is data received

SEHH2238 Computer Networking
Table 25.17: A simple TCP client program (Part IV)
SEHH2238 L12 Appendix 49

SEHH2238 Computer Networking
􏰀 Socket Address
􏰁IP Address + Port Number
􏰀 Socket Programming
􏰁Connect applications to the TCP/IP Protocols suite 􏰁Client side vs. Server side, UDP vs. TCP
􏰀 Revision Quiz
􏰁 http://highered.mheducation.com/sites/0073376221/student_view0/
chapter25/quizzes.html
SEHH2238 L12 Appendix 50

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com