CN Lab 3
CN Lab 3
CN Lab 3
Date: 22-02-2022
Instructor: Dr. Arslan Ahmed
Lab Engineer: Syed Muhammad Ali Musa
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
int main() {
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(server_port);
// open socket
int sock;
return 1;
//start time
// send data
int len =
char buffer[100];
//end time
buffer[len] = '\0';
close(sock);
return 0;
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
socklen_t client_address_len;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
int sock;
return 1;
sizeof(server_address))) < 0) {
return 1;
// run indefinitely
while (true) {
char buffer[500];
&client_address_len);
// ip address
buffer[len] = '\0';
inet_ntoa(client_address.sin_addr));
sizeof(client_address));
return 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.
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
// http://beej.us/guide/bgnet/output/html/multipage/inet_ntopman.html
server_address.sin_port = htons(server_port);
int sock;
return 1;
sizeof(server_address)) < 0) {
return 1;
// send
// receive
int n = 0;
char buffer[maxlen];
maxlen -= n;
len += n;
buffer[len] = '\0';
//end time
close(sock);
return 0;
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.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.
* */
socklen_t client_address_len;
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
int listen_sock;
return 1;
sizeof(server_address))) < 0) {
return 1;
// dropping begins
return 1;
while (true) {
int sock;
if ((sock =
&client_address_len)) < 0) {
return 1;
int n = 0;
char buffer[maxlen];
inet_ntoa(client_address.sin_addr));
pbuffer += n;
len += n;
close(sock);
close(listen_sock);
return 0;
RTT for TCP is higher than UDP. This is because TCP takes extra time to establish a stable
connection before the transaction of the data and checks whether the packets are error free
while UDP just sends out the packets.
4.2.3 Lab Task 4: What happens when your client (both UDP and TCP) tries to
send data to a non-existent server?
In UDP connection, if the client tries to send data to a non-existent server, then the sent packet
is just dropped. There is no concept of timeout error or connection problem. In the given code,
the client just sends the data and the process freezes.
TCP first establishes a stable connection before sending data. If the server does not exist, the
connection will not be established with an error (time-out, connection problem etc.). Hence, the
data will not be sent in this case.