Socket programming is a means of communicating data between two computers across a network. One socket, or node, listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. Connections can be made using either a connection-oriented protocol (TCP) or a connectionless protocol (UDP).
In this lab, you will be using Python to write a simple client-server application that uses sockets to communicate with each other. Throughout the lab, you will gain familiarity with a Ping application and its usefulness in computing statistics such as packet loss rate. You will first study a simple Internet ping server written in the Python, and implement a corresponding client.
UDP
Socket programming for UDP in Python is quite simple. You can easily send and receive datagram packets, specify the buffer size, and socket timeout using the Python socket library.
Part 1: UDP Pinger
The functionality provided by these programs is similar to the functionality provided by standard ping programs available in modern operating systems. However, these programs use a simpler protocol, UDP, rather than the standard Internet Control Message Protocol (ICMP) to communicate with each other. The ping protocol allows a client machine to send a packet of data to a remote machine, and have the remote machine return the data back to the client unchanged (an action referred to as echoing). Among other uses, the ping protocol allows hosts to determine round-trip times to other machines.
You are given the complete code for the Ping server below. Your task is to write the Ping client. The following code fully implements a ping server. You need to compile and run this code before running your client program. You do not need to modify this code.
In this server code, 30% of the client’s packets are simulated to be lost. You should study this code carefully, as it will help you write your ping client.
# UDPPingerServer.py
# We will need the following module to generate randomized lost packets
import random
from socket import *
# Create a UDP socket
# Notice the use of SOCK_DGRAM for UDP packets
serverSocket = socket(AF_INET, SOCK_DGRAM)
# Assign IP address and port number to socket
serverSocket.bind(('', 12000))
while True:
# Generate random number in the range of 0 to 10
rand = random.randint(0, 10)
# Receive the client packet along with the address it is coming from
message, address = serverSocket.recvfrom(1024)
# Capitalize the message from the client
message = message.upper()
# If rand is less is than 4, we consider the packet lost and do not respond
if rand < 4:
continue
# Otherwise, the server responds
serverSocket.sendto(message, address)
The server sits in an infinite loop listening for incoming UDP packets. When a packet comes in and if a randomized integer is greater than or equal to 4, the server simply capitalizes the encapsulated data and sends it back to the client. UDP provides applications with an unreliable transport service. Messages may get lost in the network due to router queue overflows, faulty hardware or some other reasons. Because packet loss is rare or even non-existent in typical campus networks, the server in this lab injects artificial loss to simulate the effects of network packet loss. The server creates a variable randomized integer which determines whether a particular incoming packet is lost or not.
You need to implement the following client program. The client should send 10 pings to the server. Because UDP is an unreliable protocol, a packet sent from the client to the server may be lost in the network, or vice versa. For this reason, the client cannot wait indefinitely for a reply to a ping message. You should get the client wait up to one second for a reply; if no reply is received within one second, your client program should assume that the packet was lost during transmission across the network. You will need to look up the Python documentation to find out how to set the timeout value on a datagram socket.
Specifically, your client program should:
- send the ping message using UDP (Note: Unlike TCP, you do not need to establish a connection first, since UDP is a connectionless protocol.)
- print the response message from server, if any
- calculate and print the round trip time (RTT), in seconds, of each packet, if server responses
- otherwise, print
Request timed out
During development, you should run the UDPPingerServer.py on your machine, and test your client by sending packets to localhost (or, 127.0.0.1). After you have fully debugged your code, you should see how your application communicates across the network with the ping server and ping client running on different machines. The ping messages in this lab are formatted in a simple way. The client message is one line, consisting of ASCII characters in the format Ping sequence_number time where sequence_number starts at 1 and progresses to 10 for each successive ping message sent by the client, and time is the time when the client sends the message.
- Please write up a paragraph answering the following questions.
- Explain at a high level how the programs could be altered for continuously reporting things like average RTT and heartbeat healthchecks. How could those reports be helpful?
- Submit the client program code.
Part 2: UDP Packet Capture
UDP is a streamlined, no-frills protocol. Because UDP is simple and sweet, we’ll be able to cover it pretty quickly in this lab.
- Start capturing packets in Wireshark.
- Run the UDP Pinger from above to cause your host to send and receive several UDP packets. The Domain Name System (DNS) protocol; and the DNS Wireshark Lab) typically sends DNS query and response messages inside of UDP, so it’s likely that you’ll find some DNS messages (and therefore UDP packets) in your trace.
- Also run the
nslookup
tool for a hostname that you haven’t visited for a while.
- Stop packet capture.
- Set your Wireshark packet filter so that Wireshark only displays the UDP segments sent and received at your host.
- Pick the first couple of UDP segments and expand the UDP fields in the details window.
- Please write up a paragraph answering the following questions.
- Select the first couple of UDP segments in your trace.
Create a table for the following values of each segment:
- The packet number
- The type of application-layer payload or protocol message being carried
- The names of the fields in the UDP header
- The length (in bytes) of each of the UDP header fields
- What is the protocol number for UDP? Give your answer in decimal notation. To answer this question, you’ll need to investigate the Protocol field of the IP datagram containing this UDP segment.
- Examine the pair of UDP packets in which your host sends the first UDP packet and the second UDP packet is a reply to this first UDP packet. What is the packet number of the first of these two UDP segments in the trace file? What is the packet number of the second of these two UDP segments in the trace file? Describe the relationship between the port numbers in the two packets.
- Select the first couple of UDP segments in your trace.
Create a table for the following values of each segment: