Client Server Flow Control

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

EXPERIMENT 5 Flow Control Mechanisms

NIsha dipak gote


22BRS1303
5.1 Write a program to implement the Stop and wait with ARQ using socket programing.
Server code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 12345


#define BUFFER_SIZE 1024

void die(char *s) {


perror(s);
exit(1);
}

int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {


die("socket");
}

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

if (bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {


die("bind");
}

printf("Server waiting for messages...\n");

while (1) {

ssize_t recv_len = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&client_addr,


&client_len);
if (recv_len == -1) {
die("recvfrom");
}
buffer[recv_len] = '\0';
printf("Received: %s\n", buffer);
sendto(sockfd, "ACK", 3, 0, (struct sockaddr*)&client_addr, client_len);
}

close(sockfd);
return 0;
}
clilent code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_IP "127.0.0.1"


#define SERVER_PORT 12345
#define BUFFER_SIZE 1024

void die(char *s) {


perror(s);
exit(1);
}

int main() {
int sockfd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {


die("socket");
}

memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);

if (inet_aton(SERVER_IP, &server_addr.sin_addr) == 0) {
fprintf(stderr, "Invalid IP address\n");
exit(1);
}

while (1) {
printf("Enter message to send (or 'exit' to quit): ");
fgets(buffer, BUFFER_SIZE, stdin);

ssize_t send_len = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr*)&server_addr,


sizeof(server_addr));
if (send_len == -1) {
die("sendto");
}

char ack[3];
ssize_t recv_len = recvfrom(sockfd, ack, sizeof(ack), 0, NULL, NULL);
if (recv_len == -1) {
die("recvfrom");
}

ack[recv_len] = '\0';
printf("Received ACK: %s\n", ack);

if (strncmp(buffer, "exit", 4) == 0) {
break;
}
}

close(sockfd);
return 0;
}
output :
server:

client:
5.2 Write a program to implement the Go-back-N with ARQ using socket programing.
Server side code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#define PORT 12345


#define WINDOW_SIZE 4
#define BUFFER_SIZE 1024

void die(char *s) {


perror(s);
exit(1);
}

int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {


die("socket");
}

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

if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {


die("bind");
}

printf("Server waiting for messages...\n");


int expected_seq_num = 0;
while (1) {
ssize_t recv_len = recvfrom(sockfd, buffer, BUFFER_SIZE, 0, (struct sockaddr
*)&client_addr, &client_len);
if (recv_len == -1) {
die("recvfrom");
}
buffer[recv_len] = '\0';

int seq_num;
char data[BUFFER_SIZE];
if (sscanf(buffer, "%d%s", &seq_num, data) != 2) {
continue;
}
if (seq_num == expected_seq_num) {
printf("Received: %s\n", buffer);
expected_seq_num = (expected_seq_num + 1) % WINDOW_SIZE;
}

char ack[32];
snprintf(ack, sizeof(ack), "ACK%d", seq_num);
sendto(sockfd, ack, strlen(ack), 0, (struct sockaddr *)&client_addr, client_len);
}

close(sockfd);
return 0;
}
client side code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SERVER_IP "127.0.0.1"


#define SERVER_PORT 12345
#define WINDOW_SIZE 4
#define BUFFER_SIZE 1024

void die(char *s) {


perror(s);
exit(1);
}

int main() {
int sockfd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];

if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {


die("socket");
}

memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_aton(SERVER_IP, &server_addr.sin_addr) == 0) {
fprintf(stderr, "Invalid IP address\n");
exit(1);
}

int next_seq_num = 0;
while (1) {
printf("Enter message to send (or 'exit' to quit): ");
fgets(buffer, BUFFER_SIZE - 10, stdin);
if (strcmp(buffer, "exit\n") == 0) {
break;
}

if (next_seq_num < WINDOW_SIZE) {


printf("Sending: %d\n", next_seq_num);
int chars_written = snprintf(buffer, BUFFER_SIZE - 1, "%d %s", next_seq_num, buffer);
if (chars_written >= BUFFER_SIZE - 1) {
fprintf(stderr, "Message is too long. Truncating.\n");
buffer[BUFFER_SIZE - 1] = '\0';
}
sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&server_addr,
sizeof(server_addr));
next_seq_num++;
}

char ack[32];
ssize_t recv_len = recvfrom(sockfd, ack, sizeof(ack), 0, NULL, NULL);
if (recv_len == -1) {
die("recvfrom");
}
ack[recv_len] = '\0';
int ack_num;
sscanf(ack, "ACK%d", &ack_num);
printf("Received ACK: %d\n", ack_num);
if (next_seq_num >= WINDOW_SIZE) {
next_seq_num = 0;
}
}

close(sockfd);
return 0;
}
output:
server side:
client side:

5.3 Write a program to implement the Selective repeat with using socket programing.

Serverside code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8080


#define MAX_BUFFER_SIZE 1024
#define WINDOW_SIZE 4

void die(char *message) {


perror(message);
exit(1);
}

int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);

if ((server_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {


die("Error creaƟng socket");
}

server_addr.sin_family = AF_INET;

server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);

if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {


die("Error binding");
}

printf("Server listening on port %d...\n", PORT);

while (1) {
char buffer[MAX_BUFFER_SIZE] = {0};

recvfrom(server_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &addr_len);

printf("Received from client: %s\n", buffer);

sendto(server_socket, "ACK", strlen("ACK"), 0, (struct sockaddr *)&client_addr, addr_len);


}

close(server_socket);
return 0;
}

client side code:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <arpa/inet.h>

#define PORT 8080


#define MAX_BUFFER_SIZE 1024
#define WINDOW_SIZE 4

void die(char *message) {


perror(message);
exit(1);
}

int main() {
int client_socket;
struct sockaddr_in server_addr;

if ((client_socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {


die("Error creaƟng socket");
}

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
char *message = "this is krithik";

sendto(client_socket, message, strlen(message), 0, (struct sockaddr *)&server_addr,


sizeof(server_addr));

printf("Message sent to server: %s\n", message);

char ack[3];

recvfrom(client_socket, ack, sizeof(ack), 0, NULL, NULL);

printf("Received acknowledgment from server: %s\n", ack);

close(client_socket);
return 0;
}
output:
server:

client:

You might also like