CN Lab 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Department of Computing

EE353: Computer Networks

Name: Muhammad Ali Usman


CMS ID: 332608
Class: BESE-11A

Lab 3: Network Programming (Client/Server) Using Python (Part 2)


CLO 1: Understand the fundamental Building blocks of Computer Networks i.e., Layered
approach and protocols that make networking possible

Date: 22-02-2022
Instructor: Dr. Arslan Ahmed
Lab Engineer: Syed Muhammad Ali Musa

EE353: Computer Networks Page 1


Lab 3: Network Programming (Client/Server) Using Python

4.1.1 Lab Task 1: A useful Python UDP client/server application.


Modify the UDPClient program such that the UDPClient 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 request is sent
and time when the Reply is received in human readable form.
UDP Client Code:

#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() {

const char* server_name = "localhost";

const int server_port = 8877;

struct sockaddr_in server_address;

memset(&server_address, 0, sizeof(server_address));

server_address.sin_family = AF_INET;

// creates binary representation of server name

// and stores it as sin_addr

EE353: Computer Networks Page 2


inet_pton(AF_INET, server_name, &server_address.sin_addr);

// htons: port in network order format

server_address.sin_port = htons(server_port);

// open socket

int sock;

if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {

printf("could not create socket\n");

return 1;

// data that will be sent to the server

const char* data_to_send = "Hello Muhammad Ali Usman!!";

//start time

clock_t time1 = clock();

printf("Communication started at %f seconds!\n", double(time1) / CLOCKS_PER_SEC);

// send data

int len =

sendto(sock, data_to_send, strlen(data_to_send), 0,

(struct sockaddr*)&server_address, sizeof(server_address));

EE353: Computer Networks Page 3


// received echoed data back

char buffer[100];

recvfrom(sock, buffer, len, 0, NULL, NULL);

//end time

clock_t time2 = clock();

double rtt = double(time2-time1) / CLOCKS_PER_SEC;

printf("Communication finished at %f seconds!\n", double(time2) /


CLOCKS_PER_SEC);

printf("Round Trip Time: %f seconds\n", rtt);

buffer[len] = '\0';

printf("recieved: '%s'\n", buffer);

// close the socket

close(sock);

return 0;

UDP Server Code:

#include <arpa/inet.h>

#include <netinet/in.h>

#include <stdbool.h>

#include <stdio.h>

#include <string.h>

EE353: Computer Networks Page 4


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);

// htons: host to network long: same as htons but to long

server_address.sin_addr.s_addr = htonl(INADDR_ANY);

// create a UDP socket, creation returns -1 on failure

int sock;

if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {

printf("could not create socket\n");

return 1;

EE353: Computer Networks Page 5


// bind it to listen to the incoming connections on the created server

// address, will return -1 on error

if ((bind(sock, (struct sockaddr *)&server_address,

sizeof(server_address))) < 0) {

printf("could not bind socket\n");

return 1;

// socket address used to store client address

struct sockaddr_in client_address;

// 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';

EE353: Computer Networks Page 6


printf("received: '%s' from client %s\n", buffer,

inet_ntoa(client_address.sin_addr));

// send same content back to the client ("echo")

sendto(sock, buffer, len, 0, (struct sockaddr *)&client_address,

sizeof(client_address));

return 0;

UDP Server Output:

UDP Client Output:

EE353: Computer Networks Page 7


4.2.1 Lab Task 2: A useful TCP client/server application.

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.

TCP Client Code:

#include <arpa/inet.h>

#include <stdio.h>

#include <string.h>

#include <sys/socket.h>

#include <unistd.h>

#include <time.h>

#include <sys/time.h>

EE353: Computer Networks Page 8


int main() {

const char* server_name = "localhost";

const int server_port = 8877;

struct sockaddr_in server_address;

memset(&server_address, 0, sizeof(server_address));

server_address.sin_family = AF_INET;

// creates binary representation of server name

// and stores it as sin_addr

// http://beej.us/guide/bgnet/output/html/multipage/inet_ntopman.html

inet_pton(AF_INET, server_name, &server_address.sin_addr);

// htons: port in network order format

server_address.sin_port = htons(server_port);

// open a stream socket

int sock;

if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {

printf("could not create socket\n");

return 1;

// TCP is connection oriented, a reliable connection

EE353: Computer Networks Page 9


// **must** be established before any data is exchanged

if (connect(sock, (struct sockaddr*)&server_address,

sizeof(server_address)) < 0) {

printf("could not connect to server\n");

return 1;

// send

clock_t time1 = clock();

printf("Connection request sent at %f seconds!\n", double(time1) /


CLOCKS_PER_SEC);

// data that will be sent to the server

const char* data_to_send = "Hello Muhammad Ali Usman!!";

send(sock, data_to_send, strlen(data_to_send), 0);

// receive

int n = 0;

int len = 0, maxlen = 100;

char buffer[maxlen];

char* pbuffer = buffer;

// will remain open until the server terminates the connection

while ((n = recv(sock, pbuffer, maxlen, 0)) > 0) {

EE353: Computer Networks Page 10


pbuffer += n;

maxlen -= n;

len += n;

buffer[len] = '\0';

printf("received: '%s'\n", buffer);

//end time

clock_t time2 = clock();

double rtt = double(time2-time1) / CLOCKS_PER_SEC;

printf("Reply received at %f seconds!\n", double(time2) / CLOCKS_PER_SEC);

printf("Round Trip Time: %f seconds\n", rtt);

// close the socket

close(sock);

return 0;

TCP Server Code:

#include <arpa/inet.h>

#include <netinet/in.h>

#include <stdbool.h>

#include <stdio.h>

EE353: Computer Networks Page 11


#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);

EE353: Computer Networks Page 12


// create a TCP socket, creation returns -1 on failure

int listen_sock;

if ((listen_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {

printf("could not create listen socket\n");

return 1;

// bind it to listen to the incoming connections on the created server

// address, will return -1 on error

if ((bind(listen_sock, (struct sockaddr *)&server_address,

sizeof(server_address))) < 0) {

printf("could not bind socket\n");

return 1;

int wait_size = 16; // maximum number of waiting clients, after which

// dropping begins

if (listen(listen_sock, wait_size) < 0) {

printf("could not open socket for listening\n");

return 1;

// socket address used to store client address

struct sockaddr_in client_address;

EE353: Computer Networks Page 13


// 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) {

pbuffer += n;

EE353: Computer Networks Page 14


maxlen -= n;

len += n;

printf("received: '%s'\n", buffer);

// echo received content back

send(sock, buffer, len, 0);

close(sock);

close(listen_sock);

return 0;

TCP Server Output:

TCP Client Output:

EE353: Computer Networks Page 15


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?

RTT for UDP: 0.000381 seconds

RTT for TCP: 0.000443 seconds

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.

EE353: Computer Networks Page 16


EE353: Computer Networks Page 17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy