Network Programs

01.tcpclient.c

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

02.tcpserver.c

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

03.udpclient.c

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 }

04.udpserver.c

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 }

05.LeakyBucket.c

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

06.LSR.c

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

07.ftpclient.c

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

08.ftpserver.c

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

09.selectivearqclient.c

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

10.selectivearqserver.c

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

a.out

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ELF>�@�9@8 @@@@��� � ��  - = =�0*0-0=0=��88800hhhDDS�td88800P�td"""<<Q�tdR�td - = =��/lib64/ld-linux-x86-64.so.2 GNU���GNU���3IR=��`���3�/HGNU��e�m$� F]�xd>E^�6=� W~Ri� K"__cxa_finalizeconnectgetpidrecv__libc_start_mainstrcmpfprintffputssocketatoifopenfclosesend__isoc99_scanfhtonsperrorexit__stack_chk_failinet_addrlibc.so.6GLIBC_2.7GLIBC_2.4GLIBC_2.34GLIBC_2.2.5_ITM_deregisterTMCloneTable__gmon_start___ITM_registerTMCloneTable�ii �ii �����ui � =�(=`@@�?�?�?�?�?8?@?H?P?X?`?h? p? x? �? �? �?�?�?�?�?�?�?�?�?��H��H��/H��t��H����5/��%/��h���������h���������h���������h��������h��������h��������h��������h��q������h��a������h ��Q������h ��A������h ��1������h ��!������h ��������h��������h���������h���������h���������h���������h����������%}.D����%-D����%-D����%-D����%-D����%-D����%-D����%}-D����%u-D����%m-D����%e-D����%]-D����%U-D����%M-D����%E-D����%=-D����%5-D����%--D����%%-D����%-D����%-D��1�I��^H��H���PTE1�1�H�=���,�f.�H�=-H�-H9�tH��,H��t �����H�=,H�5,H)�H��H��?H��H�H��tH��,H��t��fD�����=,u+UH�=,H��t H�=,������d�����,]������w�����UH��H����\���H��P���dH�%(H�E�1���\���H�$ H�����������H��P���H��H�H���d�����h������������l�����l���yH�� H��������B���H�� H���3���fDžp���H��P���H��H�H��������t�����h��������>���f��r���H��p�����l����H�Ή��������yH�� H�����������H�� H������H�� H������H�E�H��H�� H�Ǹ�u���H�E�H�� H��H��������uCH�u���l�����d������H��P���H��H�H�u�H�U���l������y�y���H�E�H�p H��H��������u?H�u���l�����d���C������l������e����H�U�dH+%(t�H�! H�������� ������������UH��H��P������H��H��H��H������H������H������dH�%(H�E�1�H�� H�Ǹ����H������H��H�� H�Ǹ�0���H������H��H�� H�Ǹ����H�����������������T���H�������������� �������H������H�c H��H���m�����u �������H�S H�Ǹ������������������������yH�7 H���G�����m���fDž����H������H�������������������f������H�������������H�Ή��2�����y!H�� H�����������������������������'H�5�(�������H��yH�� H��������H������H�� H��H���m���H������H������uH�� H���]����������������mH������H��H�(H��������p�����H������H������H�5q H�Ǹ�����H������H���M���H�� H������������������H�E�dH+%(t�-�������H��H���Usage: ./client <server IP> <control port> Error creating socketSocket createdError in connectionConnected to serverEnter 'get' for receiving file from server Enter 'close' for closing the connection%sgetcloseInvalid commandEnter filename: Name of file=%s errorFILE NOT FOUND PROCESS ID = %d Error creating data socketError in connection of data socketFailed to receive filewError opening fileFILE %s RECEIVED FROM SERVER WITH PROCESS ID = %d File received successfully;<���ph����x��������X����������zRx �X���&D$4����PFJ w�?:*3$"\����t����@�����3E�C * ������E�C � �`�  � =(=���o�H�   ?���� ���o���o����o���oh���o0=0@P`p�������� 0@P`@GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0�� � ���  3`I @U(=|�� =������#���0=�"� ?�$ � @@@@'�FY@`s�y�� ����a�@��   /@< KPg��&P@\�3as�����@� �" Scrt1.o__abi_tagcrtstuff.cderegister_tm_clones__do_global_dtors_auxcompleted.0__do_global_dtors_aux_fini_array_entryframe_dummy__frame_dummy_init_array_entry07.ftpclient.c__FRAME_END___DYNAMIC__GNU_EH_FRAME_HDR_GLOBAL_OFFSET_TABLE_recv@GLIBC_2.2.5__libc_start_main@GLIBC_2.34_ITM_deregisterTMCloneTabler_Bufgetpid@GLIBC_2.2.5_edatafclose@GLIBC_2.2.5_fini__stack_chk_fail@GLIBC_2.4htons@GLIBC_2.2.5send@GLIBC_2.2.5get_functionfputs@GLIBC_2.2.5__data_startinet_addr@GLIBC_2.2.5strcmp@GLIBC_2.2.5fprintf@GLIBC_2.2.5__gmon_start____dso_handle_IO_stdin_used_end__bss_startmainfopen@GLIBC_2.2.5perror@GLIBC_2.2.5atoi@GLIBC_2.2.5__isoc99_scanf@GLIBC_2.7exit@GLIBC_2.2.5connect@GLIBC_2.2.5__TMC_END___ITM_registerTMCloneTable__cxa_finalize@GLIBC_2.2.5_initsocket@GLIBC_2.2.5.symtab.strtab.shstrtab.interp.note.gnu.property.note.gnu.build-id.note.ABI-tag.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rela.dyn.rela.plt.init.plt.got.plt.sec.text.fini.rodata.eh_frame_hdr.eh_frame.init_array.fini_array.dynamic.data.bss.comment#8806hh$I�� W���o��$a ��piHHq���ohh4~���o��P�����B�����  P�pp���@������� � �""<�H"H"�� = -�(=(-�0=0-�� ? /�@0  @00' 00+@0X �5/�8

udpclient

1 2 3 4 5 6 ELF>�@08@8 @@@@��PP�� p-p=p=���-==��88800hhhDDS�td88800P�td8 8 8 44Q�tdR�tdp-p=p=��/lib64/ld-linux-x86-64.so.2 GNU���GNU������N�D�����B��$GNU��e�m� EPaJh)� 7<� 0"__cxa_finalizesendto__libc_start_mainstrcmpsocketgetsrecvfromputsclose__stack_chk_failprintfinet_addrlibc.so.6GLIBC_2.4GLIBC_2.2.5GLIBC_2.34_ITM_deregisterTMCloneTable__gmon_start___ITM_registerTMCloneTablerii |ui �����p=`x= @@�?�?�? �? �?�?�?�?�?�?�?�? �? �? �?��H��H��/H��t��H����5R/��%S/��h���������h���������h���������h��������h��������h��������h��������h��q������h��a������h ��Q��������%/D����%.D����%.D����%.D����%.D����%}.D����%u.D����%m.D����%e.D����%].D����%U.D��1�I��^H��H���PTE1�1�H�=��3.�f.�H�=Y.H�R.H9�tH�.H��t �����H�=).H�5".H)�H��H��?H��H�H��tH��-H��t��fD�����=-u+UH�=-H��t H�=-�����d�����-]������w�����UH��H��dH�%(H�E�1�HDž ���HDž(���HDž0���HDž8���HDž@���HDžH���HDžP���HDžX���HDž`���HDžh���HDžp���HDžx����E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E��E����������� ���fDž���fDž����H�X H���|��������H�M H������Dž���H�F H�Ǹ����H�E�H�Ǹ�X����������H�����H�u��� ���A��I�й�d�������H�E�H�� H��H��������trH�����H�����H�� ����� ���I��I�й�d�������H�� ���H��H�� H�Ǹ�n���H�� ���H�� H��H��������t��������� ����Ǹ�V����H�E�dH+%(t��������H��H���127.0.0.1 Client ready…. Client:end Server:%s;4����h����������H���P1����zRx �����&D$4x����FJ w�?:*3$"\���t������i���xE�C o ` r  �p=x=���o�X� � p?�`�� ���o���o`���o���o:���o�=0@P`p�����@GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0�� � ��� �3 I@Ux=|`�p=������!����=�8 �p?� � @/@@G�Mh{��@��� �@� '@��&,@8ix=@I c"~ �Scrt1.o__abi_tagcrtstuff.cderegister_tm_clones__do_global_dtors_auxcompleted.0__do_global_dtors_aux_fini_array_entryframe_dummy__frame_dummy_init_array_entry03.udpclient.c__FRAME_END___DYNAMIC__GNU_EH_FRAME_HDR_GLOBAL_OFFSET_TABLE___libc_start_main@GLIBC_2.34_ITM_deregisterTMCloneTableputs@GLIBC_2.2.5_edata_fini__stack_chk_fail@GLIBC_2.4printf@GLIBC_2.2.5sendto@GLIBC_2.2.5close@GLIBC_2.2.5__data_startinet_addr@GLIBC_2.2.5strcmp@GLIBC_2.2.5__gmon_start____dso_handle_IO_stdin_usedgets@GLIBC_2.2.5recvfrom@GLIBC_2.2.5_end__bss_startmain__TMC_END___ITM_registerTMCloneTable__cxa_finalize@GLIBC_2.2.5_initsocket@GLIBC_2.2.5.symtab.strtab.shstrtab.interp.note.gnu.property.note.gnu.build-id.note.ABI-tag.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rela.dyn.rela.plt.init.plt.got.plt.sec.text.fini.rodata.eh_frame_hdr.eh_frame.init_array.fini_array.dynamic.data.bss.comment#8806hh$I�� W���o��$a ���iXX�q���o:: ~���o``@�����B``���  �����������a��� � 8�8 8 4�p p ��p=p-�x=x-��=-��p?p/�@0 @000+@08 x4�7

udpserver

1 2 3 4 5 6 7 ELF>�@X8@8 @@@@����-- <<h-h=h=��x-x=x=��88800hhhDDS�td88800P�tdX X X 44Q�tdR�tdh-h=h=��/lib64/ld-linux-x86-64.so.2 GNU���GNU�B�AJ����g"�O�� �GNU��e�m� EUfJm)� 7<P� 0"__cxa_finalizesendto__libc_start_mainstrcmpsocketgetsrecvfromputsclosebind__stack_chk_failprintfinet_addrlibc.so.6GLIBC_2.4GLIBC_2.2.5GLIBC_2.34_ITM_deregisterTMCloneTable__gmon_start___ITM_registerTMCloneTablewii �ui �����h=�p=@@@�?�?�? �?�?�?�?�?�?�?�?�? �? �? �? �?��H��H��/H��t��H����5J/��%K/��h���������h���������h���������h��������h��������h��������h��������h��q������h��a������h ��Q������h ��A��������% /D����%.D����%}.D����%u.D����%m.D����%e.D����%].D����%U.D����%M.D����%E.D����%=.D����%5.D��1�I��^H��H���PTE1�1�H�=��.�f.�H�=9.H�2.H9�tH��-H��t �����H�= .H�5.H)�H��H��?H��H�H��tH��-H��t��fD�����=-u+UH�=-H��t H�=-�y����d�����-]������w�����UH��H��dH�%(H�E�1�HDž ���HDž(���HDž0���HDž8���HDž@���HDžH���HDžP���HDžX���HDž`���HDžh���HDžp���HDžx����E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E�H�E��E���������������fDž���fDž����H�< H���l��������H������������H�Ή�����H� H�������Dž����H������H�����H�� ���������I��I�й�d���/���H�� ���H��H�� H�Ǹ����H�� ���H�� H��H���������ttH�� H�Ǹ����H�E�H�Ǹ������������H�����H�u�������A��I�й�d���O���H�E�H�m H��H���i�����t��������������Ǹ�*����H�E�dH+%(t���������H��H���127.0.0.1 Server ready, waiting for client…. Client:%send Server:;4����h����������H���P1����zRx �����&D$4X����FJ w�?:*3$"\����t������i����E�C � �@w  h=p=���o�p� � h?��� ���o���o����o���oX���ox=0@P`p������@GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0�� � ��� 3@I@Up=|��h=������8!���x=�X �h?� � @/@@G Mh{��@��� �@� '@��&,@8��=N@Z t"� �Scrt1.o__abi_tagcrtstuff.cderegister_tm_clones__do_global_dtors_auxcompleted.0__do_global_dtors_aux_fini_array_entryframe_dummy__frame_dummy_init_array_entry04.udpserver.c__FRAME_END___DYNAMIC__GNU_EH_FRAME_HDR_GLOBAL_OFFSET_TABLE___libc_start_main@GLIBC_2.34_ITM_deregisterTMCloneTableputs@GLIBC_2.2.5_edata_fini__stack_chk_fail@GLIBC_2.4printf@GLIBC_2.2.5sendto@GLIBC_2.2.5close@GLIBC_2.2.5__data_startinet_addr@GLIBC_2.2.5strcmp@GLIBC_2.2.5__gmon_start____dso_handle_IO_stdin_usedgets@GLIBC_2.2.5recvfrom@GLIBC_2.2.5_end__bss_startmainbind@GLIBC_2.2.5__TMC_END___ITM_registerTMCloneTable__cxa_finalize@GLIBC_2.2.5_initsocket@GLIBC_2.2.5.symtab.strtab.shstrtab.interp.note.gnu.property.note.gnu.build-id.note.ABI-tag.gnu.hash.dynsym.dynstr.gnu.version.gnu.version_r.rela.dyn.rela.plt.init.plt.got.plt.sec.text.fini.rodata.eh_frame_hdr.eh_frame.init_array.fini_array.dynamic.data.bss.comment#8806hh$I�� W���o��$a ���ipp�q���oXX"~���o��@�����B����  �����������}�   � V�X X 4�� � ��h=h-�p=p-�x=x-��h?h/�@0 @000+@0P �4�87