Lab 4 - TCP, UDP - 2020 PDF
Lab 4 - TCP, UDP - 2020 PDF
1.0 Objectives:
2.0 Instructions:
• To obtain credit for this lab, you are supposed to complete the lab tasks and provide
the source codes and the screen shot of your output in this document (please use
red font color) and upload the completed document to your course’s LMS site.
• Avoid plagiarism by copying from the Internet or from your peers. You may refer to
source/ text but you must paraphrase the original work.
3.0 Background:
4.0 Client-Server Socket Programming
We introduce UDP and TCP socket programming by way of a simple UDP application
and a simple TCP application both implemented in Python.
We’ll use the following simple ‘Echo’ client-server application to demonstrate socket
programming for both UDP and TCP:
1. The client reads a line of characters (data) from its keyboard and sends the data to
the server.
4. The client receives the modified data and displays the line on its screen.
• TCP does the error checking also, UDP does not have an option for
Error checking.
• Packets are ordered in case of TCP (i.e they are received in the
same order as they are sent). 7. Application layer protocols like
HTTP, FTP, Telnet, etc. uses TCP to transmit data whereas UDP is
used by protocols like VoIP, DHCP, SNMP, etc.
Figure 2 highlights the main socket-related activity of the client and server that
communicate over the UDP transport service.
Now let’s take a look at the client-server program pair for a UDP implementation of this
The client program is called UDPClient.c, and the server program is called
UDPServer.c. In order to emphasize the key issues, we intentionally provide code that
is minimal. “Good code” would certainly have a few more auxiliary lines, in particular for
handling error cases.
Socket creation in C
Closing a socket
// run indefinitely
while (true) {
char buffer[500];
// read content into buffer from an incoming client
int len = recvfrom(sock, buffer, sizeof(buffer), 0,
(struct sockaddr *)&client_address,
&client_address_len);
// inet_ntoa prints user friendly representation of the
// ip address
buffer[len] = '\0';
Let’s now take a look at the lines that differ significantly from UDPServer and
TCPClient.
Note:connect() is blocking
The server gets a socket for an incoming client connection by calling accept()
int s = accept(sockid, &clientAddr, &addrLen);
sockid: integer, the orig. socket (being listened on)
clientAddr: struct sockaddr, address of the active participant filled in upon return
addrLen: sizeof(clientAddr): value/result parameter must be set appropriately before call
adjusted upon return
Note:accept() is blocking: waits for connection before returning ‰ dequeues the next
connection on the queue for socket (sockid)
TCPServer.c
include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/**
* TCP Uses 2 types of sockets, the connection socket and the listen socket.
* The Goal is to separate the connection phase from the data exchange phase.
* */
int main(int argc, char *argv[]) {
// port to start the server on
int SERVER_PORT = 8877;
socklen_t client_address_len;
// socket address used for the server
struct sockaddr_in server_address;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
// htons: host to network short: transforms a value in host byte
// ordering format to a short value in network byte ordering format
server_address.sin_port = htons(SERVER_PORT);
// htonl: host to network long: same as htons but to long
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
// run indefinitely
while (true) {
// open a new socket to transmit data per connection
int sock;
if ((sock =
accept(listen_sock, (struct sockaddr *)&client_address,
&client_address_len)) < 0) {
printf("could not open a socket to accept data\n");
return 1;
}
int n = 0;
int len = 0, maxlen = 100;
char buffer[maxlen];
char *pbuffer = buffer;
printf("client connected with ip address: %s\n",
inet_ntoa(client_address.sin_addr));
// keep running as long as the client keeps the connection open
while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {
Modify the TCPClient program such that the TCPClient is able to calculate
the Application level Round Trip Time (RTT) for the communication
between the Client and the Server. The Client should also print the time
when connection request is send and time when the Reply (capitalized
words) is received in human readable form.
Please paste your source code for TCPClient and screen shot of the output for Lab
Task 2 here.
4.2.2 Lab Task 3: Compare the values of the RTT for both the UDP and
TCP. Which one has got higher RTT? Why?
4.2.3 Lab Task 4: What happens when your client (both UDP and TCP)
tries to send data to a non-existent server?
Please type your descriptive answer and screen shot of the output for Lab Task 4 here.
Tuturiopoint.com
https://www.csd.uoc.gr/~hy556/material/tutorials/cs556-3rd-tutorial.pdf