1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
struct sockaddr_in server; // Structure to store server address
int client_socket; // Socket file descriptor
char rBuf[100] = "", sBuf[100] = ""; // Buffers for receiving and sending data
client_socket = socket(AF_INET, SOCK_STREAM, 0); // Create a socket(address_family,socket_type, protocol=0 (Default))
// SOCK_STREAM indicates that its TCP
// Initialize server address structure
server.sin_family = AF_INET; // AF_INET is used for IPv4 addresses
server.sin_port = 2000; // Port number
server.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP address
printf("\nClient ready....\n"); // Indicate that client is ready
while (1) {
connect(client_socket, (struct sockaddr *)&server, sizeof(server)); // Since TCP is connection oriented we need to Connect
printf("\nClient: "); // Prompt for user input
gets(sBuf); // Get user input
send(client_socket, sBuf, sizeof(sBuf), 0); // Send data to the server
if (strcmp(sBuf, "end") == 0) // If user enters "end", break the loop
break;
recv(client_socket, rBuf, sizeof(rBuf), 0); // Receive data from the server
printf("\nServer: %s", rBuf); // Print the received data
if (strcmp(rBuf, "end") == 0) // If server sends "end", break the loop
break;
printf("\n"); // Add a newline for clarity
}
close(client_socket); // Close the socket
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main() {
struct sockaddr_in client, server; // Structures to store client and server addresses
int lfd, n, confd; // Socket file descriptors and size variable
char rBuf[100] = "", sBuf[100] = ""; // Buffers for receiving and sending data
lfd = socket(AF_INET, SOCK_STREAM, 0); // Create a socket
// Initialize server address structure
server.sin_family = AF_INET; // IPv4
server.sin_port = 2000; // Port number
server.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP address
bind(lfd, (struct sockaddr *)&server, sizeof(server)); // Bind the socket to the server address
listen(lfd, 1); // Listen for incoming connections, since TCP is connection oriented
printf("\nServer ready, waiting for client….\n"); // Indicate that server is ready
confd = accept(lfd, (struct sockaddr *)&client, &n); // Accept a connection from a client
while(1) {
n = sizeof(client); // Get the size of the client address structure
recv(confd, rBuf, sizeof(rBuf), 0); // Receive data from the client
printf("\nClient: %s", rBuf); // Print the received data
if(strcmp(rBuf, "end") == 0) // If client sends "end", break the loop
break;
printf("\nServer: "); // Prompt for server input
gets(sBuf); // Get server input
send(confd, sBuf, sizeof(sBuf), 0); // Send data to the client
if(strcmp(sBuf, "end") == 0) // If server sends "end", break the loop
break;
printf("\n"); // Add a newline for clarity
}
close(confd); // Close the connection with the client
close(lfd); // Close the server socket
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void main() {
struct sockaddr_in server; // Structure to store server address
int lfd, n; // Socket file descriptor and size variable
char rBuf[100] = "", sBuf[100] = ""; // Buffers for receiving and sending data
lfd = socket(AF_INET, SOCK_DGRAM, 0); // Create a socket (UDP)
// SOCK_DGRAM indicates that its UDP
// Initialize server address structure
server.sin_family = AF_INET; // IPv4
server.sin_port = 2001; // Port number
server.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP address
printf("\nClient ready….\n"); // Indicate that client is ready
n = sizeof server; // Get the size of the server address structure
while(1) {
printf("\nClient:"); // Prompt for user input
gets(sBuf); // Get user input
sendto(lfd, sBuf, sizeof sBuf, 0, (struct sockaddr *)&server, n); // Send data to the server
if(strcmp(sBuf, "end") == 0) // If user enters "end", break the loop
break;
recvfrom(lfd, rBuf, sizeof rBuf, 0, (struct sockaddr*)&server, &n); // Receive data from the server
printf("\nServer:%s", rBuf); // Print the received data
if(strcmp(rBuf, "end") == 0) // If server sends "end", break the loop
break;
}
close(lfd); // Close the socket
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void main() {
struct sockaddr_in client, server; // Structures to store client and server addresses
int lfd, n; // Socket file descriptor and size variable
char rBuf[100] = "", sBuf[100] = ""; // Buffers for receiving and sending data
lfd = socket(AF_INET, SOCK_DGRAM, 0); // Create a socket (UDP)
// Initialize server address structure
server.sin_family = AF_INET; // IPv4
server.sin_port = 2001; // Port number
server.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP address
bind(lfd, (struct sockaddr *)&server, sizeof server); // Bind the socket to the server address
printf("\nServer ready, waiting for client….\n"); // Indicate that server is ready
n = sizeof client; // Get the size of the client address structure
while(1) {
recvfrom(lfd, rBuf, sizeof rBuf, 0, (struct sockaddr *)&client, &n); // Receive data from the client
printf("\nClient:%s", rBuf); // Print the received data
if(strcmp(rBuf, "end") == 0) // If client sends "end", break the loop
break;
printf("\nServer:"); // Prompt for server input
gets(sBuf); // Get server input
sendto(lfd, sBuf, sizeof sBuf, 0, (struct sockaddr *)&client, n); // Send data to the client
if(strcmp(sBuf, "end") == 0) // If server sends "end", break the loop
break;
}
close(lfd); // Close the socket
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
int main() {
int incoming, outgoing, buck_size, n, store = 0; // Declare variables for incoming packet size, outgoing rate, bucket size, number of packets, and current buffer size
// Prompt the user to enter bucket size, outgoing rate, and number of packets
printf("Enter the bucket size (Bytes): ");
scanf("%d", &buck_size);
printf("Enter the outgoing rate (Bytes per second): ");
scanf("%d", &outgoing);
printf("Enter the number of packets: ");
scanf("%d", &n);
printf("-----------\n"); // Divider for clarity
// Loop through each packet
while(n != 0) {
// Prompt the user to enter the size of the incoming packet
printf("Enter the incoming packet size (Bytes): ");
scanf("%d", &incoming);
// Check if the incoming packet can be accommodated in the buffer
if(incoming <= (buck_size - store)) {
// If yes, add the packet to the buffer
store += incoming;
printf("Current Bucket buffer size %d out of %d\n", store, buck_size);
} else {
// If not, drop the excess data and update buffer size
printf("Dropped %d bytes of data from Packet\n", incoming - (buck_size - store));
printf("Current Bucket buffer size %d out of %d\n", buck_size - store, buck_size);
store = buck_size;
}
// Simulate outgoing data and update buffer size
store = store - outgoing;
// Ensure buffer size doesn't go negative
if (store < 0) {
store = 0;
}
// Display the remaining buffer size after outgoing data
printf("After outgoing %d bytes left out of %d in buffer\n", store, buck_size);
printf("----------------------------\n"); // Divider for clarity
n--; // Decrement the number of packets
}
return 0;
}
// Output
// Enter the bucket size (Bytes): 100
// Enter the outgoing rate (Bytes per second): 50
// Enter the no of packets: 5
// Enter the incoming packet size (Bytes): 50
// Current Bucket buffer size 50 out of 100
// After outgoing 0 bytes left out of 100 in buffer
// Enter the incoming packet size (Bytes): 200
// Dropped 100 bytes of data from packet
// Current Bucket buffer size 100 out of 100
// After outgoing 50 bytes left out of 100 in buffer
// Enter the incoming packet size (Bytes): 50
// Current Bucket buffer size 100 out of 100
// After outgoing 50 bytes left out of 100 in buffer
// Enter the incoming packet size (Bytes): 20
// Current Bucket buffer size 70 out of 100
// After outgoing 20 bytes left out of 100 in buffer
// Enter the incoming packet size (Bytes): 10
// Current Bucket buffer size 30 out of 100
// After outgoing 0 bytes left out of 100 in buffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <limits.h>
void main() {
int n, i, j, adj[50][50], d, max = 1000;
// Input the number of nodes
printf("\nEnter the Number of Nodes: ");
scanf("%d",&n);
// Input the cost between nodes
printf("Enter the cost between Nodes:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i == j)
adj[i][j] = 0; // No self-loops
else {
printf("Cost from %d -> %d: ", i + 1, j + 1);
scanf("%d", &adj[i][j]);
}
}
}
int dist[n], visited[n], round, v, src;
// Initialize distances and visited array
for (i = 0; i < n; i++) {
dist[i] = max;
visited[i] = 0;
}
// Input the source node
printf("\nEnter the source Node: ");
scanf("%d", &src);
src -= 1;
dist[src] = 0;
int nxhop[n];
for (i = 0; i < n; i++)
nxhop[i] = 0;
// Dijkstra's Algorithm
for (round = 0; round < n - 1; round++) {
int min = max, min_index;
// Find the minimum distance node from the set of unvisited nodes
for (v = 0; v < n; v++) {
if (visited[v] == 0 && dist[v] < min) {
min = dist[v];
min_index = v;
}
}
// Mark the picked vertex as visited
visited[min_index] = 1;
// Update dist value of the adjacent vertices of the picked vertex
for (d = 0; d < n; d++) {
if (!visited[d] && adj[min_index][d] && dist[min_index] != max
&& dist[min_index] + adj[min_index][d] < dist[d]) {
dist[d] = dist[min_index] + adj[min_index][d];
if (min_index != src)
nxhop[d] = min_index + 1;
}
}
// Display routing table for the source node after each round
if (min_index == src) {
printf("\nRouting Table of Node %d", src + 1);
printf("\nDestination\tCost\tNext Hop\n");
for (i = 0; i < n; i++) {
if (dist[i] == 0)
printf("%d\t\t-\t-\n", i + 1);
else
printf("%d\t\t%d\t-\n", i + 1, dist[i]);
}
}
}
// Display final routing table after applying Dijkstra's algorithm
printf("\n");
printf("\nAfter Applying Dijkstra's Algorithm:\n");
printf("\nRouting Table of Node %d", src + 1);
printf("\nDestination\tCost\tNext Hop\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t", i + 1, dist[i]);
if (nxhop[i] == 0)
printf("-\n");
else
printf("%d\n", nxhop[i]);
}
// Display the cost of the shortest path from the source to all other nodes
for (i = 1; i < n; i++)
printf("The cost of the shortest path from router %d to %d is %d\n",
src + 1, i + 1, dist[i]);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/wait.h>
// Buffer to store received data
char r_Buf[10000];
// Function declaration
void get_function(int csd, struct sockaddr_in data, char *data_addr);
int main(int argc, char *argv[]) {
// Check if correct number of arguments is provided
if (argc < 3) {
printf("Usage: ./client <server IP> <control port> \n");
exit(1);
}
int csd, cport;
char command[100];
struct sockaddr_in servaddr, data;
// Convert control port argument to integer
cport = atoi(argv[2]);
// Create a socket
csd = socket(AF_INET, SOCK_STREAM, 0);
if (csd < 0) {
perror("Error creating socket");
exit(1);
}
printf("Socket created\n");
// Configure server address struct
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
servaddr.sin_port = htons(cport);
// Connect to the server
if (connect(csd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("Error in connection");
exit(1);
}
printf("Connected to server\n");
while (1) {
// Prompt user for command
printf("Enter 'get' for receiving file from server\nEnter 'close' for closing the connection\n");
scanf("%s", command);
if (strcmp(command, "get") == 0) {
// Send 'get' command to the server
send(csd, command, sizeof(command), 0);
get_function(csd, data, argv[1]);
} else if (strcmp(command, "close") == 0) {
// Send 'close' command to the server and break the loop
send(csd, command, sizeof(command), 0);
break;
} else {
printf("Invalid command\n");
}
}
// Close the control socket
close(csd);
return 0;
}
void get_function(int csd, struct sockaddr_in data, char *data_addr) {
char name[256];
char valid[10];
// Prompt user for filename
printf("Enter filename: ");
scanf("%s", name);
printf("Name of file=%s\n", name);
// Send filename to server
send(csd, name, sizeof(name), 0);
// Receive validity check from server
recv(csd, valid, sizeof(valid), 0);
if (strcmp(valid, "error") == 0) {
printf("FILE NOT FOUND PROCESS ID = %d\n", getpid());
return;
}
// Create data socket
int dd = socket(AF_INET, SOCK_STREAM, 0);
if (dd < 0) {
perror("Error creating data socket");
exit(1);
}
// Configure data connection address struct
data.sin_family = AF_INET;
data.sin_addr.s_addr = inet_addr(data_addr);
data.sin_port = htons(8080);
// Connect to the server data port
if (connect(dd, (struct sockaddr *)&data, sizeof(data)) < 0) {
perror("Error in connection of data socket");
close(dd);
return;
}
// Receive file data from server
if (recv(dd, r_Buf, sizeof(r_Buf), 0) < 0) {
printf("Failed to receive file\n");
} else {
// Open file to write the received data
FILE *fp = fopen(name, "w");
if (fp == NULL) {
perror("Error opening file");
close(dd);
return;
}
// Write data to file
fprintf(fp, "%s", r_Buf);
fprintf(fp, "FILE %s RECEIVED FROM SERVER WITH PROCESS ID = %d\n", name, getpid());
fclose(fp);
printf("File received successfully\n");
}
// Close the data socket
close(dd);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
// Buffer for sending file data
char send_buf[10000];
// Function to read a file and store its content in the send_buf
void send_file(FILE *fp) {
char ch;
int i = 0;
// Read the file character by character and store it in the buffer
while ((ch = fgetc(fp)) != EOF) {
send_buf[i++] = ch;
}
// Null-terminate the buffer
send_buf[i] = '\0';
}
int main(int argc, char *argv[]) {
// Check for proper command-line argument
if (argc < 2) {
printf("Usage: ./server <control port>\n");
exit(1);
}
// Define structures for client and server addresses
struct sockaddr_in client, control, data, data_client;
FILE *fp;
int lfd, confd, fd, confd2;
char rBuf[100] = "";
int port = atoi(argv[1]);
// Create a socket for control communication
lfd = socket(AF_INET, SOCK_STREAM, 0);
if (lfd < 0) {
perror("Error creating socket");
exit(1);
}
// Set up the control socket address structure
control.sin_family = AF_INET;
control.sin_port = htons(port);
control.sin_addr.s_addr = INADDR_ANY;
// Bind the control socket to the specified port
if (bind(lfd, (struct sockaddr *)&control, sizeof(control)) < 0) {
perror("Bind failed");
close(lfd);
exit(1);
}
// Listen for incoming connections
listen(lfd, 5);
printf("\nServer ready, waiting for client....\n");
// Accept a connection from a client
socklen_t n = sizeof(client);
confd = accept(lfd, (struct sockaddr *)&client, &n);
if (confd < 0) {
perror("Accept failed");
close(lfd);
exit(1);
}
printf("\nClient connected....\n");
while (1) {
// Receive a command from the client
recv(confd, rBuf, sizeof(rBuf), 0);
// Check if the command is "get"
if (strcmp(rBuf, "get") == 0) {
printf("Client command: %s\n", rBuf);
// Receive the filename from the client
recv(confd, rBuf, sizeof(rBuf), 0);
printf("\nServer received the file to fetch: %s\n", rBuf);
// Open the requested file
fp = fopen(rBuf, "r");
if (fp == NULL) {
// If file cannot be opened, send an error message to the client
send(confd, "error", sizeof("error"), 0);
printf("\nError in opening file: %s\n", rBuf);
} else {
// Send a success message to the client
send(confd, "success", sizeof("success"), 0);
// Create a socket for data transfer
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("Error creating data socket");
exit(1);
}
// Set up the data socket address structure
data.sin_family = AF_INET;
data.sin_addr.s_addr = INADDR_ANY;
data.sin_port = htons(8080);
// Bind the data socket to the specified port
if (bind(fd, (struct sockaddr *)&data, sizeof(data)) < 0) {
perror("Bind failed for data socket");
close(fd);
exit(1);
}
// Listen for incoming data connections
listen(fd, 5);
printf("\nServer ready, waiting for client to connect to data socket\n");
// Accept a connection from the client for data transfer
n = sizeof(data_client);
confd2 = accept(fd, (struct sockaddr *)&data_client, &n);
if (confd2 < 0) {
perror("Accept failed for data socket");
close(fd);
exit(1);
}
printf("\nClient connected to data socket\n");
// Read the file content into the buffer
send_file(fp);
// Send the file content to the client
if (send(confd2, send_buf, sizeof(send_buf), 0) < 0) {
printf("Sending failed\n");
} else {
printf("File sent successfully\n");
}
// Close the file and the data socket
fclose(fp);
close(confd2);
close(fd);
}
} else if (strcmp(rBuf, "close") == 0) {
// If the command is "close", break the loop and close the connection
printf("\nClient command: %s\n", rBuf);
printf("\nClient disconnected and server closed\n");
break;
}
}
// Close the control socket and exit
close(confd);
close(lfd);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// Define a struct to represent a frame
struct frame {
int packet[40];
};
// Define a struct to represent an acknowledgement
struct ack {
int acknowledge[40];
};
int main() {
// Create a socket for the client
int clientsocket;
struct sockaddr_in server;
socklen_t len = sizeof(server);
struct frame f1;
int windowsize, totalpackets, totalframes, i = 0, j = 0, framesreceived = 0, k, l, m;
int repacket[40];
struct ack acknowledgement;
char req[50];
// Create the socket
clientsocket = socket(AF_INET, SOCK_DGRAM, 0);
if (clientsocket < 0) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
// Set up the server address
memset((char*)&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(5019);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
// Send a request to the server
printf("\nSending request to the server.\n");
if (sendto(clientsocket, "HI I AM CLIENT.", strlen("HI I AM CLIENT.") + 1, 0, (struct sockaddr*)&server, len) < 0) {
perror("Error sending request");
exit(EXIT_FAILURE);
}
printf("\nWaiting for reply.\n");
// Receive the server's response
if (recvfrom(clientsocket, req, sizeof(req), 0, (struct sockaddr*)&server, &len) < 0) {
perror("Error receiving reply");
exit(EXIT_FAILURE);
}
req[sizeof(req) - 1] = '\0'; // Ensure null-terminated string
printf("\nThe server has sent: %s\n", req);
// Get the window size from the user
printf("\nEnter the window size: ");
scanf("%d", &windowsize);
printf("\n\nSending the window size.\n");
if (sendto(clientsocket, (char*)&windowsize, sizeof(windowsize), 0, (struct sockaddr*)&server, len) < 0) {
perror("Error sending window size");
exit(EXIT_FAILURE);
}
printf("\nWaiting for the server's response.\n");
// Receive the total number of packets and frames from the server
if (recvfrom(clientsocket, (char*)&totalpackets, sizeof(totalpackets), 0, (struct sockaddr*)&server, &len) < 0) {
perror("Error receiving total packets");
exit(EXIT_FAILURE);
}
printf("\nThe total packets are: %d\n", totalpackets);
if (sendto(clientsocket, "RECEIVED.", strlen("RECEIVED.") + 1, 0, (struct sockaddr*)&server, len) < 0) {
perror("Error sending received acknowledgment");
exit(EXIT_FAILURE);
}
if (recvfrom(clientsocket, (char*)&totalframes, sizeof(totalframes), 0, (struct sockaddr*)&server, &len) < 0) {
perror("Error receiving total frames");
exit(EXIT_FAILURE);
}
printf("\nThe total frames/windows are: %d\n", totalframes);
if (sendto(clientsocket, "RECEIVED.", strlen("RECEIVED.") + 1, 0, (struct sockaddr*)&server, len) < 0) {
perror("Error sending received acknowledgment");
exit(EXIT_FAILURE);
}
// Start receiving packets
printf("\nStarting the process of receiving.\n");
j = 0;
l = 0;
while (l < totalpackets) {
// Initialize the receive buffer
printf("\nThe expected frame is %d with packets: ", framesreceived);
for (m = 0; m < j; m++) {
printf("%d ", repacket[m]);
}
while (j < windowsize && i < totalpackets) {
printf("%d ", i);
i++;
j++;
}
printf("\n\nWaiting for the frame.\n");
// Receive a frame
if (recvfrom(clientsocket, (char*)&f1, sizeof(f1), 0, (struct sockaddr*)&server, &len) < 0) {
perror("Error receiving frame");
exit(EXIT_FAILURE);
}
printf("\nReceived frame %d\n", framesreceived);
// Get acknowledgements for received packets
printf("Enter -1 to send negative acknowledgement for the following packets.");
j = 0;
m = 0;
k = l;
while (m < windowsize && k < totalpackets) {
printf("\nPacket: %d\n", f1.packet[m]);
scanf("%d", &acknowledgement.acknowledge[m]);
if (acknowledgement.acknowledge[m] == -1) {
repacket[j] = f1.packet[m];
j++;
} else {
l++;
}
m++;
k++;
}
framesreceived++;
// Send the acknowledgement
if (sendto(clientsocket, (char*)&acknowledgement, sizeof(acknowledgement), 0, (struct sockaddr*)&server, len) < 0) {
perror("Error sending acknowledgement");
exit(EXIT_FAILURE);
}
}
// Close the connection with the server
printf("\nAll frames received successfully.\n\nClosing connection with the server.\n");
close(clientsocket);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// Define a struct to represent a frame
struct frame {
int packet[40];
};
// Define a struct to represent an acknowledgement
struct ack {
int acknowledge[40];
};
int main() {
// Create a socket for the server
struct sockaddr_in server, client;
socklen_t len = sizeof(client);
int serversocket;
int windowsize, totalpackets, totalframes, framessend = 0, i = 0, j = 0, k = 0, l = 0, m = 0, n;
int repacket[40];
char req[50];
struct frame f1;
struct ack acknowledgement;
// Create the server socket
serversocket = socket(AF_INET, SOCK_DGRAM, 0);
if (serversocket < 0) {
perror("Error creating socket");
exit(EXIT_FAILURE);
}
memset((char*)&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(5019);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
// Bind the server socket
if (bind(serversocket, (struct sockaddr*)&server, sizeof(server)) < 0) {
perror("Error binding socket");
close(serversocket);
exit(EXIT_FAILURE);
}
// Wait for a client connection
printf("\nWaiting for client connection...\n");
if (recvfrom(serversocket, req, sizeof(req), 0, (struct sockaddr*)&client, &len) < 0) {
perror("Error receiving client connection");
close(serversocket);
exit(EXIT_FAILURE);
}
req[sizeof(req) - 1] = '\0'; // Ensure null-terminated string
printf("\nThe client connection obtained: %s\n", req);
// Get the window size from the client
printf("\nSending request for window size.\n");
if (sendto(serversocket, "REQUEST FOR WINDOW SIZE.", strlen("REQUEST FOR WINDOW SIZE.") + 1, 0, (struct sockaddr*)&client, len) < 0) {
perror("Error sending window size request");
close(serversocket);
exit(EXIT_FAILURE);
}
printf("\nWaiting for the window size.\n");
if (recvfrom(serversocket, (char*)&windowsize, sizeof(windowsize), 0, (struct sockaddr*)&client, &len) < 0) {
perror("Error receiving window size");
close(serversocket);
exit(EXIT_FAILURE);
}
printf("\nThe window size obtained as: %d\n", windowsize);
// Initialize variables
totalpackets = windowsize * 4;
totalframes = 4;
// Send the total number of packets and frames to the client
printf("\nSending total number of packets.\n");
if (sendto(serversocket, (char*)&totalpackets, sizeof(totalpackets), 0, (struct sockaddr*)&client, len) < 0) {
perror("Error sending total packets");
close(serversocket);
exit(EXIT_FAILURE);
}
if (recvfrom(serversocket, req, sizeof(req), 0, (struct sockaddr*)&client, &len) < 0) {
perror("Error receiving acknowledgment");
close(serversocket);
exit(EXIT_FAILURE);
}
printf("\nSending total number of frames.\n");
if (sendto(serversocket, (char*)&totalframes, sizeof(totalframes), 0, (struct sockaddr*)&client, len) < 0) {
perror("Error sending total frames");
close(serversocket);
exit(EXIT_FAILURE);
}
if (recvfrom(serversocket, req, sizeof(req), 0, (struct sockaddr*)&client, &len) < 0) {
perror("Error receiving acknowledgment");
close(serversocket);
exit(EXIT_FAILURE);
}
// Wait for the client to start the process
printf("\nPRESS ENTER TO START THE PROCESS.\n");
fgets(req, 2, stdin);
j = 0;
l = 0;
while (l < totalpackets) {
memset((char*)&f1, 0, sizeof(f1));
printf("\nInitialising the transmit buffer.\n");
printf("\nThe frame to be sent is %d with packets: ", framessend);
for (m = 0; m < j; m++) {
printf("%d ", repacket[m]);
f1.packet[m] = repacket[m];
}
while (j < windowsize && i < totalpackets) {
printf("%d ", i);
f1.packet[j] = i;
i++;
j++;
}
printf("\nSending frame %d\n", framessend);
if (sendto(serversocket, (char*)&f1, sizeof(f1), 0, (struct sockaddr*)&client, len) < 0) {
perror("Error sending frame");
close(serversocket);
exit(EXIT_FAILURE);
}
printf("\nWaiting for the acknowledgment.\n");
if (recvfrom(serversocket, (char*)&acknowledgement, sizeof(acknowledgement), 0, (struct sockaddr*)&client, &len) < 0) {
perror("Error receiving acknowledgment");
close(serversocket);
exit(EXIT_FAILURE);
}
j = 0;
k = 0;
m = 0;
n = l;
while (m < windowsize && n < totalpackets) {
if (acknowledgement.acknowledge[m] == -1) {
printf("\nNegative acknowledgment received for packet: %d\n", f1.packet[m]);
k = 1;
repacket[j] = f1.packet[m];
j++;
} else {
l++;
}
m++;
n++;
}
if (k == 0) {
printf("\nPositive acknowledgment received for all packets within the frame: %d\n", framessend);
}
framessend++;
// Wait for the client to proceed
printf("\nPRESS ENTER TO PROCEED...\n");
fgets(req, 2, stdin);
}
// Close the connection with the client
printf("\nAll frames sent successfully.\n\nClosing connection with the client.\n");
close(serversocket);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ELF >