22BCE3591 Assessment3
22BCE3591 Assessment3
Computer Networks
Lab Assessment – 3
Flow control mechanisms (Normal C Program-Single side)
Go-Back-N ARQ
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
ll tf, N, tt = 0;
srand(time(NULL));
ll i = 1;
transmission(&i, N, tf, &tt);
printf("Total number of frames which were sent and resent are: %lld\n",
tt);
return 0;
}
Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
if (ack_received) {
printf("Acknowledgment received for Frame: %d\n", frame_number);
return 1; // Acknowledgment received
} else {
printf("Acknowledgment lost for Frame: %d\n", frame_number);
return 0; // Acknowledgment lost, need to resend
}
}
if (ack_received) {
// If acknowledgment received, move to next frame
frame_number++;
} else {
// If acknowledgment lost, resend the same frame
printf("Resending Frame: %d\n", frame_number);
}
}
printf("All frames transmitted successfully.\n");
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
int main() {
int num_frames;
return 0;
}
Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Selective repeat
Code:
/*We are assuming the window size =4. both at sender and receiver's side.
There might be different order of transmission but the no of retransmissions
are same.
The order of transmission will depend upon the acknowledgement timer and
Timeout timer*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
tmp2 = receiver(tmp1);
tmp3 += tmp2;
tmp2 = negack(tmp1);
tmp5 += tmp2;
if (tmp5 != 0) {
printf("\nNo acknowledgement for the frame %d", tmp5);
printf("\nRetransmitting frame %d", tmp5);
}
morePacket -= tmp1;
if (windowsize <= 0) {
windowsize = 4;
}
}
if (i == 0) {
return windowsize;
} else {
return tmp1 % windowsize;
}
}
Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Selective Reject
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int windowsize = 4, totalFrames, remainingFrames;
int sentFrames = 0, ackedFrames = 0, retransmissions = 0, i;
int currentWindowSize;
if (windowsize <= 0) {
windowsize = 4;
}
}
Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Server:
#include <stdio.h>
#include <winsock2.h> // For Winsock functions
#include <ws2tcpip.h> // For sockaddr_in and sockaddr
#include <stdlib.h> // For exit()
#include <string.h> // For memset()
//Dhruv Kalpesh Jadav
//22BCE3591
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
int main() {
WSADATA wsaData;
SOCKET sockfd, connfd;
struct sockaddr_in servaddr, cli;
int len;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed...\n");
exit(0);
}
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == INVALID_SOCKET) {
printf("Socket creation failed...\n");
WSACleanup();
exit(0);
} else {
printf("Socket successfully created..\n");
}
exit(0);
} else {
printf("Server listening..\n");
}
len = sizeof(cli);
Client:
#include <stdio.h>
#include <winsock2.h> // For Winsock functions
#include <ws2tcpip.h> // For sockaddr_in and sockaddr
#include <stdlib.h> // For exit()
#include <string.h> // For memset()
//Dhruv Kalpesh Jadav
//22BCE3591
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
int main() {
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed...\n");
exit(0);
}
// Create socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == INVALID_SOCKET) {
printf("Socket creation failed...\n");
WSACleanup();
exit(0);
} else {
printf("Socket successfully created..\n");
}
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Localhost
servaddr.sin_port = htons(PORT);
Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
CRC:
Code:
Server:
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
int main() {
WSADATA wsaData;
SOCKET sockfd, connfd;
struct sockaddr_in servaddr, cli;
int len;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
if (listen(sockfd, 5) == SOCKET_ERROR) {
printf("Listen failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Server listening..\n");
}
len = sizeof(cli);
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd == INVALID_SOCKET) {
printf("Server accept failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Server accepted the client...\n");
}
func(connfd);
closesocket(sockfd);
WSACleanup();
return 0;
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Client:
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
printf("\nEnter the message to server: ");
memset(buff, 0, sizeof(buff));
// Exit condition
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main() {
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
func(sockfd);
closesocket(sockfd);
WSACleanup();
return 0;
}
Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Hamming Code:
Code:
Server:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <math.h>
#include <string.h>
//Dhruv Kalpesh Jadav
//22BCE3951
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
int main() {
WSADATA wsaData;
SOCKET sockfd, connfd;
struct sockaddr_in servaddr, cli;
int len;
} else {
printf("Socket successfully created..\n");
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
if (listen(sockfd, 5) == SOCKET_ERROR) {
printf("Listen failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Server listening..\n");
}
len = sizeof(cli);
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd == INVALID_SOCKET) {
printf("Server accept failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Server accepted the client...\n");
}
func(connfd);
closesocket(sockfd);
WSACleanup();
return 0;
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Client:
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
//Dhruv Kalpesh Jadav
//22BCE3591
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
int data[m];
printf("Enter the data bits (0 or 1, one by one): ");
for (int i = 0; i < m; i++) {
scanf("%d", &data[i]);
}
int main() {
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;
} else {
printf("Socket successfully created..\n");
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
func(sockfd);
closesocket(sockfd);
WSACleanup();
return 0;
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Output:
CheckSum:
Code:
Server:
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
//Dhruv Kalpesh Jadav
//22BCE3591
// Function to compute checksum (simple sum of bytes)
unsigned int checksum(char *buff, int length) {
unsigned int sum = 0;
for (int i = 0; i < length; i++) {
sum += buff[i];
}
return sum;
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
int main() {
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
WSADATA wsaData;
SOCKET sockfd, connfd;
struct sockaddr_in servaddr, cli;
int len;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
if (listen(sockfd, 5) == SOCKET_ERROR) {
printf("Listen failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Server listening..\n");
}
len = sizeof(cli);
connfd = accept(sockfd, (SA*)&cli, &len);
if (connfd == INVALID_SOCKET) {
printf("Server accept failed...\n");
closesocket(sockfd);
WSACleanup();
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
exit(0);
} else {
printf("Server accepted the client...\n");
}
func(connfd);
closesocket(sockfd);
WSACleanup();
return 0;
}
Client:
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <string.h>
#define MAX 80
#define PORT 43454
#define SA struct sockaddr
//Dhruv Kalpesh Jadav
//22BCE3591
// Function to compute checksum (simple sum of bytes)
unsigned int checksum(char *buff, int length) {
unsigned int sum = 0;
for (int i = 0; i < length; i++) {
sum += buff[i];
}
return sum;
}
for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
printf("\nEnter the message to server: ");
// Exit condition
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
int main() {
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;
} else {
printf("Socket successfully created..\n");
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
func(sockfd);
closesocket(sockfd);
WSACleanup();
return 0;
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591
Output: