Client Server Flow Control
Client Server Flow Control
Client Server Flow Control
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];
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;
while (1) {
close(sockfd);
return 0;
}
clilent code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int sockfd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
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);
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>
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];
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;
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>
int main() {
int sockfd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
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;
}
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>
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
while (1) {
char buffer[MAX_BUFFER_SIZE] = {0};
close(server_socket);
return 0;
}
#include <arpa/inet.h>
int main() {
int client_socket;
struct sockaddr_in server_addr;
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";
char ack[3];
close(client_socket);
return 0;
}
output:
server:
client: