0% found this document useful (0 votes)
23 views33 pages

22BCE3591 Assessment3

Uploaded by

Dhruv Jadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views33 pages

22BCE3591 Assessment3

Uploaded by

Dhruv Jadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

DHRUV KALPESH JADAV 23/10/2024 22BCE3591

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>

#define ll long long int

void transmission(ll *i, ll N, ll tf, ll *tt) {


while (*i <= tf) {
int z = 0;
for (int k = *i; k < *i + N && k <= tf; k++) {
printf("Sending Frame %d...\n", k);
(*tt)++;
}
for (int k = *i; k < *i + N && k <= tf; k++) {
int f = rand() % 2;
if (!f) {
printf("Acknowledgment for Frame %d...\n", k);
z++;
} else {
printf("Timeout!! Frame Number : %d Not Received\n", k);
printf("Retransmitting Window...\n");
break;
}
}
printf("\n");
*i = *i + z;
}
}

int main() {
ll tf, N, tt = 0;
srand(time(NULL));

printf("Enter the Total number of frames: ");


scanf("%lld", &tf);
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

printf("Enter the Window Size: ");


scanf("%lld", &N);

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

Stop & Wait


Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to simulate the sending of a frame


void send_frame(int frame_number) {
printf("Sending Frame: %d\n", frame_number);
}

// Function to simulate the receiving of an acknowledgment


int receive_ack(int frame_number) {
// Simulate random acknowledgment loss or success
srand(time(0)); // Seed the random number generator
int ack_received = rand() % 2; // 50% chance of success (1) or failure (0)

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
}
}

// Simulate the Stop-and-Wait protocol


void stop_and_wait_protocol(int num_frames) {
int frame_number = 0;
int ack_received;

while (frame_number < num_frames) {


send_frame(frame_number); // Send frame

// Wait for acknowledgment with a timeout simulation


ack_received = receive_ack(frame_number);

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;

// Input the number of frames to send


printf("Enter the number of frames to send: ");
scanf("%d", &num_frames);

// Simulate Stop-and-Wait Protocol


stop_and_wait_protocol(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 tmp1, tmp2, tmp3, tmp4, tmp5, i, windowsize = 4, noofPacket, morePacket;

int receiver(int tmp1);


int simulate(int windowsize);
int negack(int tmp1);

int main() {
int i;

// Initialize random seed


for (i = 0; i < 10; i++) {
rand();
}

noofPacket = rand() % 10;


printf("Number of frames is: %d\n", noofPacket);
morePacket = noofPacket;

while (morePacket >= 0) {


tmp1 = simulate(windowsize);
windowsize -= tmp1;
tmp4 += tmp1;

if (tmp4 > noofPacket) {


tmp4 = noofPacket;
}

for (i = noofPacket - morePacket; i <= tmp4; i++) {


printf("\nSending Frame %d", i);
}

tmp2 = receiver(tmp1);
tmp3 += tmp2;

if (tmp3 > noofPacket) {


tmp3 = noofPacket;
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

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

printf("\nSelective Repeat Protocol Ends. All packets are successfully


transmitted.\n");
return 0;
}

int receiver(int tmp1) {


int i;
for (i = 0; i < 5; i++) {
rand();
}
i = rand() % tmp1;
return i;
}

int negack(int tmp1) {


int i;
for (i = 0; i < 5; i++) {
rand();
}
i = rand() % tmp1;
return i;
}

int simulate(int windowsize) {


int tmp1, i;
for (i = 0; i < 5; i++) {
tmp1 = rand();
}
if (tmp1 == 0) {
tmp1 = simulate(windowsize);
}
i = tmp1 % windowsize;
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

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 simulate_transmission(int windowsize);


int simulate_acknowledgment(int tmp1);
int selective_reject(int tmp1);

int main() {
int windowsize = 4, totalFrames, remainingFrames;
int sentFrames = 0, ackedFrames = 0, retransmissions = 0, i;
int currentWindowSize;

// Initialize random seed


srand(time(NULL));

// Randomly generate total number of frames


totalFrames = rand() % 10 + 1; // Generates between 1 and 10 frames
printf("Number of frames to be transmitted: %d\n", totalFrames);
remainingFrames = totalFrames;

while (remainingFrames > 0) {


// Simulate the sending of frames in the window
currentWindowSize = simulate_transmission(windowsize);
windowsize -= currentWindowSize;
sentFrames += currentWindowSize;

if (sentFrames > totalFrames) {


sentFrames = totalFrames;
}

printf("Transmitting frames: ");


for (i = totalFrames - remainingFrames; i < sentFrames; i++) {
printf("%d ", i + 1);
}
printf("\n");

// Simulate acknowledgments for frames


int acked = simulate_acknowledgment(currentWindowSize);
ackedFrames += acked;

if (ackedFrames > totalFrames) {


ackedFrames = totalFrames;
}

// Simulate selective reject for failed frames


DHRUV KALPESH JADAV 23/10/2024 22BCE3591

int rejected = selective_reject(currentWindowSize);


if (rejected > 0) {
retransmissions += rejected;
printf("\nSelective Reject: Frame %d was not acknowledged.\n",
rejected);
printf("Retransmitting Frame %d...\n", rejected);
}

// Adjust remaining frames and window size


remainingFrames -= currentWindowSize;

if (windowsize <= 0) {
windowsize = 4;
}
}

printf("\nSelective Reject Protocol Ends. All packets were successfully


transmitted.\n");
printf("Total number of retransmissions: %d\n", retransmissions);
return 0;
}

int simulate_transmission(int windowsize) {


int framesToSend = rand() % windowsize + 1; // Random frames sent within
window size
return framesToSend;
}

int simulate_acknowledgment(int tmp1) {


// Simulate a random acknowledgment process
int ackedFrames = rand() % tmp1; // Acknowledge random number of frames
return ackedFrames;
}

int selective_reject(int tmp1) {


// Simulate if any frame is rejected (lost or not acknowledged)
int rejectedFrame = rand() % tmp1; // Randomly choose one frame to be
rejected
return rejectedFrame == 0 ? 0 : rejectedFrame; // No reject if 0 is chosen
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

Socket programming(TCP and UDP) - Some challenging experiments


on Socket programming
TCP chat application
Code:

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

void func(SOCKET sockfd) {


char buff[MAX];
int n;

for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));

// Read message from client


recv(sockfd, buff, sizeof(buff), 0);
printf("\nMessage received From Client: %s\n", buff);

// Check if the message contains "exit", if so, exit the loop


if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}

// Prompt the server to reply


printf("Enter the Message To Client: ");

// Clear the buffer again


memset(buff, 0, sizeof(buff));

// Get the message to send back to the client


n = 0;
while ((buff[n++] = getchar()) != '\n')
;
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

// Send the message back to the client


send(sockfd, buff, sizeof(buff), 0);
}
}

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

// Initialize server address structure


memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // Bind to any local IP
servaddr.sin_port = htons(PORT);

// Bind the socket


if (bind(sockfd, (SA*)&servaddr, sizeof(servaddr)) == SOCKET_ERROR) {
printf("Socket bind failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Socket successfully binded..\n");
}

// Start listening for connections


if (listen(sockfd, 5) == SOCKET_ERROR) {
printf("Listen failed...\n");
closesocket(sockfd);
WSACleanup();
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

exit(0);
} else {
printf("Server listening..\n");
}

len = sizeof(cli);

// Accept a connection from a client


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

// Call the communication function


func(connfd);

// Close the socket


closesocket(sockfd);
WSACleanup(); // Clean up Winsock
return 0;
}

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

void func(SOCKET sockfd) {


char buff[MAX];
int n;

for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

printf("\nEnter the Message to Server: ");

// Get input from the user and store it in the buffer


n = 0;
while ((buff[n++] = getchar()) != '\n')
;

// Send the message to the server


send(sockfd, buff, sizeof(buff), 0);

// Clear the buffer again


memset(buff, 0, sizeof(buff));

// Read the server's response


recv(sockfd, buff, sizeof(buff), 0);
printf("\nMessage Received From Server: %s", buff);

// Check if the message contains "exit", if so, exit the loop


if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}

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

// Initialize server address structure


memset(&servaddr, 0, sizeof(servaddr));
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Localhost
servaddr.sin_port = htons(PORT);

// Connect to the server


if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("Connection with the server failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Connected to the server..\n");
}

// Call the communication function


func(sockfd);

// Close the socket


closesocket(sockfd);
WSACleanup(); // Clean up Winsock
return 0;
}

Output:
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

Error detection using client/server method -CRC, Hamming code, Checksum

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

// Function to calculate CRC


unsigned int crc(const char *data, int length) {
unsigned int crc = 0; // Initialize CRC value
for (int i = 0; i < length; i++) {
crc ^= (unsigned int)data[i]; // XOR each byte into the CRC
for (int j = 0; j < 8; j++) { // Process each bit
if (crc & 0x01) { // If the last bit is set
crc ^= 0xEDB88320; // Apply the polynomial (reversed)
}
crc >>= 1; // Shift right
}
}
return crc;
}

void func(SOCKET sockfd) {


char buff[MAX];
unsigned int received_crc, computed_crc;

for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));

// Receive message from client


recv(sockfd, buff, sizeof(buff), 0);

// Receive the CRC from client


recv(sockfd, (char*)&received_crc, sizeof(received_crc), 0);

printf("\nMessage received from client: %s\n", buff);

// Compute the CRC of the received data


DHRUV KALPESH JADAV 23/10/2024 22BCE3591

computed_crc = crc(buff, strlen(buff));


printf("Computed CRC on Server: %u\n", computed_crc);

// Check if the received CRC matches the computed CRC


if (received_crc == computed_crc) {
printf("CRC valid. Message integrity verified.\n");
} else {
printf("CRC mismatch. Data corrupted!\n");
}

// Check if the message contains "exit", if so, exit the loop


if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}

// Server reply to the client


printf("Enter the message to client: ");
memset(buff, 0, sizeof(buff));
fgets(buff, MAX, stdin);

// Send the message back to the client


send(sockfd, buff, sizeof(buff), 0);

// Compute and send CRC of the server message


computed_crc = crc(buff, strlen(buff));
send(sockfd, (char*)&computed_crc, sizeof(computed_crc), 0);
}
}

int main() {
WSADATA wsaData;
SOCKET sockfd, connfd;
struct sockaddr_in servaddr, cli;
int len;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


printf("WSAStartup failed...\n");
exit(0);
}

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");
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);

if (bind(sockfd, (SA*)&servaddr, sizeof(servaddr)) == SOCKET_ERROR) {


printf("Socket bind failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Socket successfully binded..\n");
}

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

// Function to calculate CRC


unsigned int crc(const char *data, int length) {
unsigned int crc = 0; // Initialize CRC value
for (int i = 0; i < length; i++) {
crc ^= (unsigned int)data[i]; // XOR each byte into the CRC
for (int j = 0; j < 8; j++) { // Process each bit
if (crc & 0x01) { // If the last bit is set
crc ^= 0xEDB88320; // Apply the polynomial (reversed)
}
crc >>= 1; // Shift right
}
}
return crc;
}

void func(SOCKET sockfd) {


char buff[MAX];
unsigned int computed_crc;

for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
printf("\nEnter the message to server: ");

// Get input from the user


fgets(buff, MAX, stdin);

// Compute CRC of the message


computed_crc = crc(buff, strlen(buff));
printf("Computed CRC on Client: %u\n", computed_crc);

// Send the message to the server


send(sockfd, buff, sizeof(buff), 0);

// Send CRC to the server


send(sockfd, (char*)&computed_crc, sizeof(computed_crc), 0);

// Clear the buffer again


DHRUV KALPESH JADAV 23/10/2024 22BCE3591

memset(buff, 0, sizeof(buff));

// Receive message from server


recv(sockfd, buff, sizeof(buff), 0);
printf("\nMessage received from server: %s", buff);

// Receive CRC from server


recv(sockfd, (char*)&computed_crc, sizeof(computed_crc), 0);
printf("CRC from server: %u\n", computed_crc);

// Validate CRC of received data


if (crc(buff, strlen(buff)) == computed_crc) {
printf("CRC valid. Message integrity verified.\n");
} else {
printf("CRC mismatch. Data corrupted!\n");
}

// Exit condition
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}

int main() {
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


printf("WSAStartup failed...\n");
exit(0);
}

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

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

if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {


printf("Connection with the server failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Connected to the server..\n");
}

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

// Function to calculate the number of parity bits required


int calculate_parity_bits(int m) {
int p = 0;
while ((1 << p) < (m + p + 1)) {
p++;
}
return p;
}

// Function to insert parity bits and calculate their values


void generate_hamming_code(int data[], int m, char *hamming_code) {
int p = calculate_parity_bits(m);
int total_bits = m + p;
int k = 0;

// Initialize Hamming code array with '0's


for (int i = 0, j = 0; i < total_bits; i++) {
// Insert parity bits at positions that are powers of 2
if (i == (1 << j) - 1) {
hamming_code[i] = '0'; // Temporary value for parity bits
j++;
} else {
hamming_code[i] = data[k++] + '0'; // Insert data bits
}
}

// Calculate parity bits


for (int i = 0; i < p; i++) {
int parity_pos = (1 << i) - 1;
int parity = 0;
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

// Check the bits that this parity bit is responsible for


for (int j = parity_pos; j < total_bits; j++) {
if ((j + 1) & (1 << i)) {
parity ^= (hamming_code[j] - '0');
}
}
hamming_code[parity_pos] = parity + '0'; // Set the parity bit value
}
hamming_code[total_bits] = '\0'; // Null-terminate the string
}

// Function to communicate with the client


void func(SOCKET sockfd) {
int m;
char hamming_code[MAX];

// Receive number of data bits


recv(sockfd, (char*)&m, sizeof(m), 0);
printf("\nNumber of data bits received: %d\n", m);

// Receive data bits


int data[m];
recv(sockfd, (char*)data, sizeof(data), 0);

// Generate Hamming code


generate_hamming_code(data, m, hamming_code);
printf("Generated Hamming Code: %s\n", hamming_code);

// Send the Hamming code back to the client


send(sockfd, hamming_code, sizeof(hamming_code), 0);
}

int main() {
WSADATA wsaData;
SOCKET sockfd, connfd;
struct sockaddr_in servaddr, cli;
int len;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


printf("WSAStartup failed...\n");
exit(0);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd == INVALID_SOCKET) {
printf("Socket creation failed...\n");
WSACleanup();
exit(0);
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

} 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 (bind(sockfd, (SA*)&servaddr, sizeof(servaddr)) == SOCKET_ERROR) {


printf("Socket bind failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Socket successfully binded..\n");
}

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

void func(SOCKET sockfd) {


int m; // Number of data bits
printf("Enter the number of data bits: ");
scanf("%d", &m);
send(sockfd, (char*)&m, sizeof(m), 0); // Send number of data bits

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

// Send data bits to the server


send(sockfd, (char*)data, sizeof(data), 0);

// Receive the generated Hamming code from the server


char hamming_code[MAX];
recv(sockfd, hamming_code, sizeof(hamming_code), 0);
printf("Received Hamming Code: %s\n", hamming_code);
}

int main() {
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


printf("WSAStartup failed...\n");
exit(0);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd == INVALID_SOCKET) {
printf("Socket creation failed...\n");
WSACleanup();
exit(0);
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

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

if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {


printf("Connection with the server failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Connected to the server..\n");
}

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

void func(SOCKET sockfd) {


char buff[MAX];
unsigned int received_checksum, computed_checksum;

for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));

// Receive message from client


recv(sockfd, buff, sizeof(buff), 0);

// Receive the checksum from client


recv(sockfd, (char*)&received_checksum, sizeof(received_checksum), 0);

printf("\nMessage received from client: %s\n", buff);

// Compute the checksum of the received data


computed_checksum = checksum(buff, strlen(buff));
printf("Computed Checksum on Server: %u\n", computed_checksum);

// Check if the received checksum matches the computed checksum


if (received_checksum == computed_checksum) {
printf("Checksum valid. Message integrity verified.\n");
} else {
printf("Checksum mismatch. Data corrupted!\n");
}

// Check if the message contains "exit", if so, exit the loop


if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}

// Server reply to the client


printf("Enter the message to client: ");
memset(buff, 0, sizeof(buff));
fgets(buff, MAX, stdin);

// Send the message back to the client


send(sockfd, buff, sizeof(buff), 0);

// Compute and send checksum of server message


computed_checksum = checksum(buff, strlen(buff));
send(sockfd, (char*)&computed_checksum, sizeof(computed_checksum), 0);
}
}

int main() {
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

WSADATA wsaData;
SOCKET sockfd, connfd;
struct sockaddr_in servaddr, cli;
int len;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


printf("WSAStartup failed...\n");
exit(0);
}

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

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);

if (bind(sockfd, (SA*)&servaddr, sizeof(servaddr)) == SOCKET_ERROR) {


printf("Socket bind failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Socket successfully binded..\n");
}

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

void func(SOCKET sockfd) {


char buff[MAX];
unsigned int computed_checksum;

for (;;) {
// Clear the buffer
memset(buff, 0, sizeof(buff));
printf("\nEnter the message to server: ");

// Get input from the user


fgets(buff, MAX, stdin);

// Compute checksum of the message


computed_checksum = checksum(buff, strlen(buff));
printf("Computed Checksum on Client: %u\n", computed_checksum);
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

// Send the message to the server


send(sockfd, buff, sizeof(buff), 0);

// Send checksum to the server


send(sockfd, (char*)&computed_checksum, sizeof(computed_checksum), 0);

// Clear the buffer again


memset(buff, 0, sizeof(buff));

// Receive message from server


recv(sockfd, buff, sizeof(buff), 0);
printf("\nMessage received from server: %s", buff);

// Receive checksum from server


recv(sockfd, (char*)&computed_checksum, sizeof(computed_checksum), 0);
printf("Checksum from server: %u\n", computed_checksum);

// Validate checksum of received data


if (checksum(buff, strlen(buff)) == computed_checksum) {
printf("Checksum valid. Message integrity verified.\n");
} else {
printf("Checksum mismatch. Data corrupted!\n");
}

// Exit condition
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}

int main() {
WSADATA wsaData;
SOCKET sockfd;
struct sockaddr_in servaddr;

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {


printf("WSAStartup failed...\n");
exit(0);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd == INVALID_SOCKET) {
printf("Socket creation failed...\n");
WSACleanup();
exit(0);
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

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

if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {


printf("Connection with the server failed...\n");
closesocket(sockfd);
WSACleanup();
exit(0);
} else {
printf("Connected to the server..\n");
}

func(sockfd);
closesocket(sockfd);
WSACleanup();
return 0;
}
DHRUV KALPESH JADAV 23/10/2024 22BCE3591

Output:

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