KSHITIJ SINGH CN_LAB5 CS22B1047
Q1:
#include <stdio.h>
#include <string.h>
int main() {
char frame[50][50], str[50][50];
char flag[] = "0x7E";
char esc[] = "0x7D";
int i, j, k = 0, n;
strcpy(frame[k++], flag);
printf("Enter number of strings: ");
scanf("%d", &n);
getchar();
printf("Enter the strings:\n");
for (i = 0; i < n; i++) {
fgets(str[i], sizeof(str[i]), stdin);
str[i][strcspn(str[i], "\n")] = 0;
}
for (i = 0; i < n; i++) {
if (strcmp(str[i], flag) != 0 && strcmp(str[i], esc) != 0) {
strcpy(frame[k++], str[i]);
} else {
strcpy(frame[k++], esc);
strcpy(frame[k++], str[i]);
}
}
strcpy(frame[k++], flag);
printf("------------------------------\n\n");
printf("Byte stuffing at sender side:\n\n");
printf("------------------------------\n\n");
for (i = 0; i < k; i++) {
printf("%s\t", frame[i]);
}
printf("\n");
char destuffed[50][50];
int d = 0;
i = 1;
while (i < k - 1) {
if (strcmp(frame[i], esc) == 0) {
strcpy(destuffed[d++], frame[++i]);
} else {
strcpy(destuffed[d++], frame[i]);
}
i++;
}
printf("\n------------------------------\n\n");
printf("Destuffing at receiver side:\n\n");
printf("------------------------------\n\n");
printf("0x7E\t");
for (i = 0; i < d; i++) {
printf("%s\t", destuffed[i]);
}
printf("0x7E");
printf("\n");
return 0;
}
Output:
Q2:
SENDER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 1047
#define TIMEOUT 2 // seconds
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
int total_packets, packet_no = 1;
int ack;
srand(time(0)); // Seed for random number generation
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 address from text to binary form
if (inet_pton(AF_INET, "172.16.14.42", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
// Connect to the receiver
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr))
< 0) {
printf("\nConnection Failed \n");
return -1;
}
// Input number of packets
printf("Enter total number of packets to send: ");
scanf("%d", &total_packets);
while (packet_no <= total_packets) {
printf("Sending packet %d...\n", packet_no);
// Send packet number to receiver
send(sock, &packet_no, sizeof(packet_no), 0);
// Wait for acknowledgment with a timeout
fd_set fds;
struct timeval tv;
FD_ZERO(&fds);
FD_SET(sock, &fds);
tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;
int retval = select(sock + 1, &fds, NULL, NULL, &tv);
if (retval == -1) {
perror("select() error");
return -1;
} else if (retval) {
// Receive acknowledgment
recv(sock, &ack, sizeof(ack), 0);
if (ack == packet_no) {
printf("Packet %d acknowledged.\n", packet_no);
packet_no++; // Move to the next packet
} else {
printf("Packet %d lost or corrupted. Retransmitting...\
n", packet_no);
}
} else {
printf("Timeout! Retransmitting packet %d...\n",
packet_no);
}
}
printf("All packets sent successfully.\n");
close(sock);
return 0;
}
RECIEVER:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 1047
int simulate_packet_loss() {
return rand() % 5 == 0; // 20% chance of loss
}
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
int packet_no, expected_packet_no = 1;
srand(time(0)); // Seed for random number generation
// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Bind the socket
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) <
0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Start listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Waiting for connection...\n");
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("Connected to sender.\n");
while (1) {
// Receive packet number
int valread = recv(new_socket, &packet_no, sizeof(packet_no),
0);
if (valread <= 0) break;
// Simulate packet loss
if (simulate_packet_loss()) {
printf("Packet %d lost or corrupted. No ACK sent.\n",
packet_no);
continue; // No acknowledgment sent
}
if (packet_no == expected_packet_no) {
printf("Packet %d received successfully.\n", packet_no);
// Send acknowledgment
send(new_socket, &packet_no, sizeof(packet_no), 0);
expected_packet_no++; // Move to the next expected packet
} else {
printf("Received out of order packet %d. Expected %d.\n",
packet_no, expected_packet_no);
}
}
close(new_socket);
close(server_fd);
return 0;
}
OUTPUT:
Q3:
SENDER:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 8080
#define TIMEOUT 2 // Timeout in seconds
int window_size;
int simulate_packet_loss() {
return rand() % 5 == 0; // Simulate packet loss/corruption with 20%
probability
}
void send_packets(int sock, int total_packets) {
int base = 1;
int next_seq_num = 1;
int ack = 0;
srand(time(0)); // Seed for random loss simulation
while (base <= total_packets) {
// Send packets in the current window
while (next_seq_num < base + window_size && next_seq_num <=
total_packets) {
printf("Sending packet %d...\n", next_seq_num);
send(sock, &next_seq_num, sizeof(next_seq_num), 0);
next_seq_num++;
}
// Wait for acknowledgment
fd_set fds;
struct timeval tv;
FD_ZERO(&fds);
FD_SET(sock, &fds);
tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;
int retval = select(sock + 1, &fds, NULL, NULL, &tv);
if (retval == -1) {
perror("select() error");
return;
} else if (retval) {
// Receive acknowledgment
recv(sock, &ack, sizeof(ack), 0);
printf("Received ACK for packet %d.\n", ack);
if (ack >= base) {
base = ack + 1; // Slide the window
}
} else {
// Timeout, go back and retransmit
printf("Timeout! Retransmitting from packet %d...\n",
base);
next_seq_num = base; // Retransmit from the base packet
}
}
printf("All packets sent successfully.\n");
}
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
int total_packets;
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 address from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
// Connect to the receiver
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr))
< 0) {
printf("\nConnection Failed \n");
return -1;
}
// Get total number of packets and window size from user
printf("Enter total number of packets to send: ");
scanf("%d", &total_packets);
printf("Enter window size: ");
scanf("%d", &window_size);
// Send packets with GBN ARQ
send_packets(sock, total_packets);
close(sock);
return 0;
}
RECIEVER:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 8080
int simulate_packet_loss() {
return rand() % 5 == 0; // Simulate packet loss with 20% chance
}
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
int expected_seq_num = 1, packet_no;
srand(time(0)); // Seed for random loss simulation
// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// Bind the socket
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) <
0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// Start listening for incoming connections
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Waiting for connection...\n");
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("Connected to sender.\n");
while (1) {
int valread = recv(new_socket, &packet_no, sizeof(packet_no),
0);
if (valread <= 0) break;
// Simulate packet loss
if (simulate_packet_loss()) {
printf("Packet %d lost or corrupted.\n", packet_no);
continue; // Don't send acknowledgment for lost/corrupted
packet
}
// Only accept the expected packet in sequence
if (packet_no == expected_seq_num) {
printf("Packet %d received successfully.\n", packet_no);
expected_seq_num++;
// Send cumulative acknowledgment
send(new_socket, &packet_no, sizeof(packet_no), 0);
} else {
printf("Out of order packet %d received, expected %d.\n",
packet_no, expected_seq_num);
// Send the ACK for the last correctly received packet
int last_ack = expected_seq_num - 1;
send(new_socket, &last_ack, sizeof(last_ack), 0);
}
}
close(new_socket);
close(server_fd);
return 0;
}
Output: