├── .gitignore ├── exp5-FTP ├── sample.txt ├── a.out ├── outputs.md ├── client.c └── server.c ├── exp3-SlidingWindowProtocols ├── selectiveRepeat │ ├── README.md │ ├── a.out │ ├── outputs.md │ ├── client.c │ └── server.c ├── goBack │ ├── a.out │ ├── gobackclient.png │ ├── README.md │ ├── outputs.md │ ├── server.c │ └── client.c ├── stopAndWait │ ├── a.out │ ├── client.c │ └── server.c └── README.md ├── exp1-TCP ├── a.out ├── client.png ├── server.png ├── outputs.md ├── client.c ├── server.c └── README.md ├── exp2-UDP ├── a.out ├── udpclient.png ├── udpserver.png ├── outputs.md ├── client.c ├── server.c └── README.md ├── exp6-MultiUserChat ├── a.out ├── output.md ├── client.c └── server.c ├── exp7-LeakyBucket ├── a.out ├── leakybkt.c └── outputs.md ├── exp4-DistanceVectorRouting ├── a.out ├── outputs.md ├── dvr.c └── README.md ├── simplified ├── exp3_SlidingWindowProtocols │ ├── GoBack │ │ ├── a.out │ │ ├── server.c │ │ └── client.c │ └── StopAndWait │ │ ├── a.out │ │ ├── server.c │ │ └── client.c ├── exp2_UDP │ ├── client.c │ └── server.c └── exp1_TCP │ ├── client.c │ └── server.c ├── DistanceVectorRouting.c └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /exp6-Mutlichat -------------------------------------------------------------------------------- /exp5-FTP/sample.txt: -------------------------------------------------------------------------------- 1 | its the sample file for ftp -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/selectiveRepeat/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /exp1-TCP/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp1-TCP/a.out -------------------------------------------------------------------------------- /exp2-UDP/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp2-UDP/a.out -------------------------------------------------------------------------------- /exp5-FTP/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp5-FTP/a.out -------------------------------------------------------------------------------- /exp1-TCP/client.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp1-TCP/client.png -------------------------------------------------------------------------------- /exp1-TCP/server.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp1-TCP/server.png -------------------------------------------------------------------------------- /exp2-UDP/udpclient.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp2-UDP/udpclient.png -------------------------------------------------------------------------------- /exp2-UDP/udpserver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp2-UDP/udpserver.png -------------------------------------------------------------------------------- /exp6-MultiUserChat/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp6-MultiUserChat/a.out -------------------------------------------------------------------------------- /exp7-LeakyBucket/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp7-LeakyBucket/a.out -------------------------------------------------------------------------------- /exp4-DistanceVectorRouting/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp4-DistanceVectorRouting/a.out -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/goBack/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp3-SlidingWindowProtocols/goBack/a.out -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/stopAndWait/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp3-SlidingWindowProtocols/stopAndWait/a.out -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/selectiveRepeat/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp3-SlidingWindowProtocols/selectiveRepeat/a.out -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/goBack/gobackclient.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/exp3-SlidingWindowProtocols/goBack/gobackclient.png -------------------------------------------------------------------------------- /simplified/exp3_SlidingWindowProtocols/GoBack/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/simplified/exp3_SlidingWindowProtocols/GoBack/a.out -------------------------------------------------------------------------------- /simplified/exp3_SlidingWindowProtocols/StopAndWait/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xrg360/networkLabS6/HEAD/simplified/exp3_SlidingWindowProtocols/StopAndWait/a.out -------------------------------------------------------------------------------- /exp5-FTP/outputs.md: -------------------------------------------------------------------------------- 1 | # server 2 | ``` 3 | Server socket created. 4 | Server bind completed. 5 | Server listening for client connection... 6 | 7 | The requested file from the client is sample.txt. 8 | 9 | The contents of the file: 10 | its the sample file for ftp 11 | ``` 12 | 13 | # client 14 | ``` 15 | Enter the name of the file : sample.txt 16 | 17 | The contents of the file: 18 | 19 | its the sample file for ftp 20 | 21 | ``` 22 | -------------------------------------------------------------------------------- /exp2-UDP/outputs.md: -------------------------------------------------------------------------------- 1 | # server 2 | ``` 3 | Socket created successfully 4 | Done with binding 5 | Listening for incoming messages... 6 | 7 | Received message from IP: 127.0.0.1 and port: 38084 8 | Msg from client: hello from client 9 | ``` 10 | 11 | 12 | 13 | 14 | # client 15 | 16 | ``` 17 | Socket created successfully 18 | Enter message: hello from client 19 | Server's response: hello from client --> it returns the same message as the client message 20 | ``` -------------------------------------------------------------------------------- /exp1-TCP/outputs.md: -------------------------------------------------------------------------------- 1 | # server 2 | 3 | ``` 4 | Socket created successfully 5 | Done with binding 6 | 7 | Listening for incoming connections..... 8 | Client connected at IP: 127.0.0.1 and port: 38754 9 | Msg from client: this is the message from client 10 | ``` 11 | 12 | # client 13 | 14 | ``` 15 | Socket created successfully 16 | Connected with server successfully 17 | Enter message: this is the message from client 18 | Server's response: This is the server's message. 19 | ``` -------------------------------------------------------------------------------- /exp4-DistanceVectorRouting/outputs.md: -------------------------------------------------------------------------------- 1 | # output distance vector routing 2 | 3 | ``` 4 | Enter the number of nodes : 3 5 | 6 | Enter the cost matrix : 7 | 0 1 4 8 | 1 0 2 9 | 4 2 0 10 | 11 | 12 | For router 1 13 | 14 | node 1 via 1 Distance 0 15 | node 2 via 2 Distance 1 16 | node 3 via 2 Distance 3 17 | 18 | For router 2 19 | 20 | node 1 via 1 Distance 1 21 | node 2 via 2 Distance 0 22 | node 3 via 3 Distance 2 23 | 24 | For router 3 25 | 26 | node 1 via 2 Distance 3 27 | node 2 via 2 Distance 2 28 | node 3 via 3 Distance 0 29 | 30 | ``` 31 | -------------------------------------------------------------------------------- /exp6-MultiUserChat/output.md: -------------------------------------------------------------------------------- 1 | # server 2 | ``` 3 | TCPServer Waiting for client on port 4950 4 | new connection from 127.0.0.1 on port 36308 5 | new connection from 127.0.0.1 on port 35030 6 | new connection from 127.0.0.1 on port 40166 7 | ``` 8 | 9 | 10 | # client 1 11 | ``` 12 | this is the message from client 1 13 | this is the message from client 2 14 | this is the message from client 3 15 | ``` 16 | 17 | 18 | # client 2 19 | ``` 20 | this is the message from client 1 21 | this is the message from client 2 22 | this is the message from client 3 23 | ``` 24 | 25 | # client 3 26 | ``` 27 | this is the message from client 1 28 | this is the message from client 2 29 | this is the message from client 3 30 | 31 | ``` 32 | -------------------------------------------------------------------------------- /simplified/exp2_UDP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main(){ 7 | int sockd; 8 | struct sockaddr_in server,client; 9 | char server_msg[200],client_msg[200]; 10 | int client_len = sizeof(client); 11 | int server_len = sizeof(server); 12 | 13 | memset(server_msg,'\0', sizeof(server_msg)); 14 | memset(client_msg,'\0', sizeof(client_msg)); 15 | 16 | sockd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); 17 | 18 | server.sin_family= AF_INET; 19 | server.sin_port = htons(2000); 20 | server.sin_addr.s_addr = inet_addr("127.0.0.1"); 21 | 22 | sendto(sockd,"hello there from clinet",strlen("hello there from clinet"),0,(struct sockaddr *)&server,server_len); 23 | 24 | } -------------------------------------------------------------------------------- /simplified/exp2_UDP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main(){ 7 | int sockd; 8 | struct sockaddr_in server,client; 9 | char server_msg[200],client_msg[200]; 10 | int client_len = sizeof(client); 11 | 12 | memset(server_msg,'\0', sizeof(server_msg)); 13 | memset(client_msg,'\0', sizeof(client_msg)); 14 | 15 | sockd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); 16 | 17 | server.sin_family= AF_INET; 18 | server.sin_port = htons(2000); 19 | server.sin_addr.s_addr = inet_addr("127.0.0.1"); 20 | 21 | bind(sockd,(struct sockaddr*)&server,sizeof(server)); 22 | printf("listening\n\n"); 23 | 24 | recvfrom(sockd,client_msg,sizeof(client_msg),0,(struct sockaddr *)&client,&client_len); 25 | printf("%s\n\n",client_msg); 26 | 27 | send 28 | } -------------------------------------------------------------------------------- /simplified/exp1_TCP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | void main(){ 8 | struct sockaddr_in server,client; 9 | int sockd, client_sock,client_size; 10 | char server_msg[200],client_msg[200]; 11 | 12 | memset(server_msg,'\0',sizeof(server_msg)); 13 | memset(client_msg,'\0',sizeof(client_msg)); 14 | 15 | sockd = socket(AF_INET,SOCK_STREAM,0); 16 | 17 | server.sin_family = AF_INET; 18 | server.sin_port = htons(2000); 19 | server.sin_addr.s_addr = inet_addr("127.0.0.1"); 20 | 21 | connect(sockd,(struct sockaddr * )&server,sizeof(server)); 22 | 23 | //ASSIGN A NUMBER AND TYPECAST THIS NUMBER TO STRING AND THEN SEND IT TO THE SERVER 24 | int number_to_send = 900; 25 | sprintf(client_msg, "%d", number_to_send); 26 | 27 | // Send the number 28 | send(sockd, client_msg, strlen(client_msg), 0); 29 | } 30 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/selectiveRepeat/outputs.md: -------------------------------------------------------------------------------- 1 | # server.c 2 | ``` 3 | Socket created 4 | Bind done 5 | Server listening .. 6 | Server acccept the client 7 | acceptedEnter the window size of the server: 2 8 | Frame 1 received ackknowledgement sent for 1 9 | Frame 3 received instead of 2 10 | sending NAK -2Frame 2 received ackknowledgement sent for 3 11 | Frame 4 received ackknowledgement sent for 4 12 | Frame 6 received instead of 5 13 | sending NAK -5Frame 5 received ackknowledgement sent for 6 14 | 15 | ``` 16 | 17 | # client.c 18 | ``` 19 | Socket created 20 | Connected to server 21 | Enter the number of frames to be sent 22 | 6 23 | Enter the window size 24 | 2 25 | Frame 1 sent 26 | Frame 2 sent 27 | Ack not received for 1 28 | RESENDING ... 29 | Ack received 1 30 | Frame 3 sent 31 | Ack not received for -2 32 | RESENDING ... 33 | Ack not received for 2 34 | RESENDING ... 35 | Ack received 3 36 | Frame 4 sent 37 | Frame 5 sent 38 | Ack not received for 4 39 | RESENDING ... 40 | Ack received 4 41 | Frame 6 sent 42 | Ack not received for -5 43 | RESENDING ... 44 | Ack not received for 5 45 | RESENDING ... 46 | Ack received 6 47 | EXIT 48 | 49 | ``` 50 | -------------------------------------------------------------------------------- /exp5-FTP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | main() 13 | { 14 | struct sockaddr_in serveraddr; 15 | int clientsock, n, rdret, length; 16 | char filename[20], filedata[300]; 17 | bzero((char *)&serveraddr, sizeof(serveraddr)); 18 | serveraddr.sin_family = AF_INET; 19 | serveraddr.sin_port = 2000; 20 | serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 21 | clientsock = socket(AF_INET, SOCK_STREAM, 0); 22 | if (connect(clientsock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) 23 | { 24 | printf("\nError:Cannot connect..."); 25 | exit(0); 26 | } 27 | printf("Enter the name of the file : "); 28 | scanf("%s", filename); 29 | length = strlen(filename); 30 | write(clientsock, filename, length); 31 | rdret = read(clientsock, filedata, 300); 32 | printf("\nThe contents of the file: \n\n"); 33 | printf("%s", filedata); 34 | close(clientsock); 35 | } 36 | -------------------------------------------------------------------------------- /simplified/exp1_TCP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void main(){ 7 | struct sockaddr_in server,client; 8 | int sockd, client_sock,client_size; 9 | char server_msg[200],client_msg[200]; 10 | int client_num; 11 | memset(server_msg,'\0',sizeof(server_msg)); 12 | memset(client_msg,'\0',sizeof(client_msg)); 13 | 14 | sockd = socket(AF_INET,SOCK_STREAM,0); 15 | 16 | server.sin_family = AF_INET; 17 | server.sin_port = htons(2000); 18 | server.sin_addr.s_addr = inet_addr("127.0.0.1"); 19 | 20 | 21 | bind(sockd,(struct sockaddr * )&server,sizeof(server)); 22 | 23 | listen(sockd,1); 24 | printf("listening\n\n"); 25 | 26 | client_size = sizeof(client); 27 | client_sock = accept(sockd,(struct sockaddr * )&client,&client_size ); 28 | 29 | recv(client_sock,client_msg,sizeof(client_msg),0); 30 | 31 | printf("%s\n\n",client_msg); 32 | 33 | 34 | //RECIEVE A NUMBER FROM CLIENT AND ADD 2 TO IT AND PRINT TO SERVER CONSOLE 35 | sscanf(client_msg , "%d",&client_num); 36 | printf("%d",client_num+2); 37 | 38 | send(client_sock,"hello",strlen("hello"),0); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /DistanceVectorRouting.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_NODES 20 4 | 5 | struct node { 6 | unsigned dist[MAX_NODES]; 7 | unsigned from[MAX_NODES]; 8 | } rt[MAX_NODES]; 9 | 10 | int main() { 11 | int costmat[MAX_NODES][MAX_NODES]; 12 | int nodes, i, j, k, count; 13 | 14 | printf("\nEnter the number of nodes : "); 15 | scanf("%d", &nodes); 16 | 17 | printf("\nEnter the cost matrix :\n"); 18 | for (i = 0; i < nodes; i++) { 19 | for (j = 0; j < nodes; j++) { 20 | scanf("%d", &costmat[i][j]); 21 | costmat[i][i] = 0; 22 | rt[i].dist[j] = costmat[i][j]; 23 | rt[i].from[j] = j; 24 | } 25 | } 26 | 27 | do { 28 | count = 0; 29 | for (i = 0; i < nodes; i++) { 30 | for (j = 0; j < nodes; j++) { 31 | for (k = 0; k < nodes; k++) { 32 | if (rt[i].dist[j] > costmat[i][k] + rt[k].dist[j]) { 33 | rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j]; 34 | rt[i].from[j] = k; 35 | count++; 36 | } 37 | } 38 | } 39 | } 40 | } while (count != 0); 41 | 42 | for (i = 0; i < nodes; i++) { 43 | printf("\n\n For router %d\n", i + 1); 44 | for (j = 0; j < nodes; j++) { 45 | printf("\t\nnode %d via %d Distance %d ", j + 1, rt[i].from[j] + 1, rt[i].dist[j]); 46 | } 47 | } 48 | 49 | printf("\n\n"); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /exp4-DistanceVectorRouting/dvr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX_NODES 20 4 | 5 | struct node { 6 | unsigned dist[MAX_NODES]; 7 | unsigned from[MAX_NODES]; 8 | } rt[MAX_NODES]; 9 | 10 | int main() { 11 | int costmat[MAX_NODES][MAX_NODES]; 12 | int nodes, i, j, k, count; 13 | 14 | printf("\nEnter the number of nodes : "); 15 | scanf("%d", &nodes); 16 | 17 | printf("\nEnter the cost matrix :\n"); 18 | for (i = 0; i < nodes; i++) { 19 | for (j = 0; j < nodes; j++) { 20 | scanf("%d", &costmat[i][j]); 21 | costmat[i][i] = 0; 22 | rt[i].dist[j] = costmat[i][j]; 23 | rt[i].from[j] = j; 24 | } 25 | } 26 | 27 | do { 28 | count = 0; 29 | for (i = 0; i < nodes; i++) { 30 | for (j = 0; j < nodes; j++) { 31 | for (k = 0; k < nodes; k++) { 32 | if (rt[i].dist[j] > costmat[i][k] + rt[k].dist[j]) { 33 | rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j]; 34 | rt[i].from[j] = k; 35 | count++; 36 | } 37 | } 38 | } 39 | } 40 | } while (count != 0); 41 | 42 | for (i = 0; i < nodes; i++) { 43 | printf("\n\n For router %d\n", i + 1); 44 | for (j = 0; j < nodes; j++) { 45 | printf("\t\nnode %d via %d Distance %d ", j + 1, rt[i].from[j] + 1, rt[i].dist[j]); 46 | } 47 | } 48 | 49 | printf("\n\n"); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /exp5-FTP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | main() 9 | { 10 | struct sockaddr_in clientaddr, serveraddr; 11 | int serversock, newserversock, clientsize, n, f, rc; 12 | char filename[100], filedata[300]; 13 | fflush(stdin); 14 | serversock = socket(AF_INET, SOCK_STREAM, 0); 15 | printf("\nServer socket created."); 16 | bzero((char *)&serveraddr, sizeof(serveraddr)); 17 | 18 | serveraddr.sin_family = AF_INET; 19 | serveraddr.sin_port = 2000; 20 | serveraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 21 | bind(serversock, (struct sockaddr *)&serveraddr, sizeof(serveraddr)); 22 | printf("\nServer bind completed.\n"); 23 | 24 | sizeof(serveraddr); 25 | listen(serversock, 5); 26 | printf("Server listening for client connection...\n"); 27 | while (1) 28 | { 29 | clientsize = sizeof(clientaddr); 30 | newserversock = accept(serversock, (struct sockaddr *)&clientaddr, &clientsize); 31 | n = read(newserversock, filename, 100); 32 | filename[n] = 0; 33 | printf("\nThe requested file from the client is %s.\n", filename); 34 | f = open(filename, O_RDWR); 35 | n = read(f, filedata, 300); 36 | printf("\nThe contents of the file: \n"); 37 | printf("%s\n", filedata); 38 | write(newserversock, filedata, n); 39 | } 40 | close(serversock); 41 | close(newserversock); 42 | } -------------------------------------------------------------------------------- /exp2-UDP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | int socket_desc; 8 | struct sockaddr_in server_addr; 9 | char server_message[2000], client_message[2000]; 10 | int server_struct_length = sizeof(server_addr); 11 | 12 | memset(server_message, '\0', sizeof(server_message)); 13 | memset(client_message, '\0', sizeof(client_message)); 14 | 15 | socket_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 16 | if (socket_desc < 0) 17 | { 18 | printf("Error while creating socket\n"); 19 | return -1; 20 | } 21 | printf("Socket created successfully\n"); 22 | 23 | server_addr.sin_family = AF_INET; 24 | server_addr.sin_port = htons(2000); 25 | server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 26 | 27 | printf("Enter message: "); 28 | gets(client_message); 29 | 30 | if (sendto(socket_desc, client_message, strlen(client_message), 0, 31 | (struct sockaddr *)&server_addr, server_struct_length) < 0) 32 | { 33 | printf("Unable to send message\n"); 34 | return -1; 35 | } 36 | 37 | if (recvfrom(socket_desc, server_message, sizeof(server_message), 0, 38 | (struct sockaddr *)&server_addr, &server_struct_length) < 0) 39 | { 40 | printf("Error while receiving server's msg\n"); 41 | return -1; 42 | } 43 | printf("Server's response: %s\n", server_message); 44 | 45 | close(socket_desc); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /exp1-TCP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | int socket_desc; 8 | struct sockaddr_in server_addr; 9 | char server_message[2000], client_message[2000]; 10 | // Clean buffers: 11 | memset(server_message, '\0', sizeof(server_message)); 12 | memset(client_message, '\0', sizeof(client_message)); 13 | // Create socket: 14 | socket_desc = socket(AF_INET, SOCK_STREAM, 0); 15 | if (socket_desc < 0) 16 | { 17 | printf("Unable to create socket\n"); 18 | return -1; 19 | } 20 | printf("Socket created successfully\n"); 21 | 22 | server_addr.sin_family = AF_INET; 23 | server_addr.sin_port = htons(2000); 24 | server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 25 | 26 | if (connect(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 27 | { 28 | printf("Unable to connect\n"); 29 | return -1; 30 | } 31 | printf("Connected with server successfully\n"); 32 | 33 | printf("Enter message: "); 34 | gets(client_message); 35 | 36 | if (send(socket_desc, client_message, strlen(client_message), 0) < 0) 37 | { 38 | printf("Unable to send message\n"); 39 | return -1; 40 | } 41 | 42 | if (recv(socket_desc, server_message, sizeof(server_message), 0) < 0) 43 | { 44 | printf("Error while receiving server's msg\n"); 45 | return -1; 46 | } 47 | printf("Server's response: %s\n", server_message); 48 | 49 | close(socket_desc); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /exp7-LeakyBucket/leakybkt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define No_of_Packets 10 5 | int randm(int a) 6 | { 7 | int rn =(random() % 10) % a; 8 | return rn == 0 ? 1 : rn; 9 | } 10 | int main() 11 | { 12 | int psize[No_of_Packets],i,clk,bsize,oprate,p_sz_rm=0,p_sz,p_time,op; 13 | for(i=0;ibsize) 24 | if(psize[i]>bsize) 25 | printf("\nIncoming Packet size (%d bytes) is greater than bucket capcity(%d bytes) - Packet Rejected",psize[i],bsize); 26 | else 27 | printf("\nBucket capacity exceeded - Packet Rejected"); 28 | else 29 | { 30 | p_sz_rm+=psize[i]; 31 | printf("\nIncoming packet size: %d",psize[i]); 32 | printf("\nBytes remaining to Transmit: %d",p_sz_rm); 33 | p_time=randm(4) * 10; 34 | printf("\nTime left for transmission: %d units",p_time); 35 | for(clk=10;clk<=p_time;clk+=10) 36 | { 37 | sleep(1); 38 | if(p_sz_rm) 39 | { 40 | if(p_sz_rm<=oprate) 41 | op=p_sz_rm,p_sz_rm=0; 42 | else 43 | op = oprate, p_sz_rm -= oprate; 44 | printf("\nPacket of size %d Transmitted",op); 45 | printf("\n%d bytes remaining to transmit",p_sz_rm); 46 | } 47 | else 48 | { 49 | printf("\nTime left for transmission: %d units", p_time-clk); 50 | printf("\nNo packets to transmit"); 51 | } 52 | } 53 | } 54 | } 55 | } 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/goBack/README.md: -------------------------------------------------------------------------------- 1 | # Go-Back-N ARQ 2 | 3 | This repository contains the implementation of the Go-Back-N Automatic Repeat Request (ARQ) protocol. The protocol ensures reliable data transmission between a client and a server over an unreliable network. 4 | 5 | ## Files 6 | 7 | 1. [`server.c`](https://github.com/Xrg360/networkLabS6/blob/master/exp3-SlidingWindowProtocols/goBack/server.c): This file contains the implementation of the server-side code for the Go-Back-N ARQ protocol. 8 | 2. [`client.c`](https://github.com/Xrg360/networkLabS6/blob/master/exp3-SlidingWindowProtocols/goBack/client.c): This file contains the implementation of the client-side code for the Go-Back-N ARQ protocol. 9 | 3. [`outputs`](https://github.com/Xrg360/networkLabS6/blob/master/exp3-SlidingWindowProtocols/goBack/outputs.md): This file contains the sample output generated by running the client and server programs. 10 | 11 | ## Usage 12 | 13 | To use this implementation, follow these steps: 14 | 15 | 1. Compile the `server.c` file using the following command: 16 | ```bash 17 | gcc server.c 18 | ``` 19 | 2. Compile the `client.c` file using a C compiler. 20 | ```bash 21 | gcc client.c 22 | ``` 23 | 3. Run the server program first by executing the generated executable. 24 | ```bash 25 | ./a.out 26 | ``` 27 | 4. Run the client program by executing the generated executable, providing the necessary inputs. 28 | 5. Observe the output in the console and refer to the [`outputs`](https://github.com/Xrg360/networkLabS6/blob/master/exp3-SlidingWindowProtocols/goBack/outputs.md) file for a sample output. 29 | 30 | ## Contributing 31 | 32 | Contributions are welcome! If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request. 33 | 34 | ## License 35 | 36 | This project is licensed under the [XRG License](LICENSE). 37 | 38 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/goBack/outputs.md: -------------------------------------------------------------------------------- 1 | # server.c 2 | 3 | ``` 4 | socket created successfully 5 | Bind done 6 | server listening .. 7 | Frame 0 received 8 | Acknowledgement sent:0 9 | Frame 1 received 10 | Acknowledgement sent:1 11 | Frame 2 not received 12 | Frame 3 discarded 13 | acknoledgement sent:1 14 | Frame 4 discarded 15 | acknoledgement sent:1 16 | Frame 2 received 17 | Acknowledgement sent:2 18 | Frame 3 received 19 | Acknowledgement sent:3 20 | Frame 4 received 21 | Acknowledgement sent:4 22 | Frame 5 received 23 | Acknowledgement sent:5 24 | Frame 6 not received 25 | Frame 7 discarded 26 | acknoledgement sent:5 27 | Frame 8 discarded 28 | acknoledgement sent:5 29 | Frame 6 not received 30 | Frame 7 discarded 31 | acknoledgement sent:5 32 | Frame 8 discarded 33 | acknoledgement sent:5 34 | Frame 6 received 35 | Acknowledgement sent:6 36 | Frame 7 received 37 | Acknowledgement sent:7 38 | Frame 8 received 39 | Acknowledgement sent:8 40 | Frame 9 received 41 | Acknowledgement sent:9 42 | Frame 0 discarded 43 | acknoledgement sent:9 44 | Frame 0 discarded 45 | acknoledgement sent:9 46 | ``` 47 | 48 | 49 | # clent.c 50 | 51 | ``` 52 | Socket created 53 | Connected successfully 54 | Enter the number of frames: 10 55 | Enter the window size: 3 56 | Frame 0 sent 57 | Frame 1 sent 58 | Frame 2 sent 59 | Ack received 0 60 | Frame 3 sent 61 | Ack received 1 62 | Frame 4 sent 63 | Ack not received for 1 64 | RESENDING ... 65 | Frame 2 sent 66 | Frame 3 sent 67 | Frame 4 sent 68 | Ack received 2 69 | Frame 5 sent 70 | Ack received 3 71 | Frame 6 sent 72 | Ack received 4 73 | Frame 7 sent 74 | Ack received 5 75 | Frame 8 sent 76 | Ack not received for 5 77 | RESENDING ... 78 | Frame 6 sent 79 | Frame 7 sent 80 | Frame 8 sent 81 | Ack not received for 5 82 | RESENDING ... 83 | Frame 6 sent 84 | Frame 7 sent 85 | Frame 8 sent 86 | Ack received 6 87 | Frame 9 sent 88 | Ack received 7 89 | Ack received 8 90 | Ack received 9 91 | EXIT 92 | ``` 93 | -------------------------------------------------------------------------------- /exp2-UDP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int socket_desc; 9 | struct sockaddr_in server_addr, client_addr; 10 | char server_message[2000], client_message[2000]; 11 | int client_struct_length = sizeof(client_addr); 12 | 13 | memset(server_message, '\0', sizeof(server_message)); 14 | memset(client_message, '\0', sizeof(client_message)); 15 | 16 | socket_desc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 17 | if (socket_desc < 0) 18 | { 19 | printf("Error while creating socket\n"); 20 | return -1; 21 | } 22 | printf("Socket created successfully\n"); 23 | 24 | server_addr.sin_family = AF_INET; 25 | server_addr.sin_port = htons(2000); 26 | server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 27 | 28 | if (bind(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 29 | { 30 | printf("Couldn't bind to the port\n"); 31 | return -1; 32 | } 33 | printf("Done with binding\n"); 34 | printf("Listening for incoming messages...\n\n"); 35 | 36 | if (recvfrom(socket_desc, client_message, sizeof(client_message), 0, 37 | (struct sockaddr *)&client_addr, &client_struct_length) < 0) 38 | { 39 | printf("Couldn't receive\n"); 40 | return -1; 41 | } 42 | printf("Received message from IP: %s and port: %i\n", 43 | inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); 44 | 45 | printf("Msg from client: %s\n", client_message); 46 | 47 | strcpy(server_message, client_message); 48 | if (sendto(socket_desc, server_message, strlen(server_message), 0, 49 | (struct sockaddr *)&client_addr, client_struct_length) < 0) 50 | { 51 | printf("Can't send\n"); 52 | return -1; 53 | } 54 | 55 | close(socket_desc); 56 | return 0; 57 | } -------------------------------------------------------------------------------- /exp1-TCP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | int socket_desc, client_sock, client_size; 8 | struct sockaddr_in server_addr, client_addr; 9 | char server_message[2000], client_message[2000]; 10 | 11 | memset(server_message, '\0', sizeof(server_message)); 12 | memset(client_message, '\0', sizeof(client_message)); 13 | 14 | socket_desc = socket(AF_INET, SOCK_STREAM, 0); 15 | if (socket_desc < 0) 16 | { 17 | printf("Error while creating socket\n"); 18 | return -1; 19 | } 20 | printf("Socket created successfully\n"); 21 | 22 | server_addr.sin_family = AF_INET; 23 | server_addr.sin_port = htons(2000); 24 | server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); 25 | 26 | if (bind(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 27 | { 28 | printf("Couldn't bind to the port\n"); 29 | return -1; 30 | } 31 | printf("Done with binding\n"); 32 | 33 | if (listen(socket_desc, 1) < 0) 34 | { 35 | printf("Error while listening\n"); 36 | return -1; 37 | } 38 | printf("\nListening for incoming connections.....\n"); 39 | 40 | client_size = sizeof(client_addr); 41 | client_sock = accept(socket_desc, (struct sockaddr *)&client_addr, &client_size); 42 | if (client_sock < 0) 43 | { 44 | printf("Can't accept\n"); 45 | return -1; 46 | } 47 | printf("Client connected at IP: %s and port: %i\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); 48 | 49 | if (recv(client_sock, client_message, sizeof(client_message), 0) < 0) 50 | { 51 | printf("Couldn't receive\n"); 52 | return -1; 53 | } 54 | printf("Msg from client: %s\n", client_message); 55 | 56 | strcpy(server_message, "This is the server's message."); 57 | if (send(client_sock, server_message, strlen(server_message), 0) < 0) 58 | { 59 | printf("Can't send\n"); 60 | return -1; 61 | } 62 | 63 | close(client_sock); 64 | close(socket_desc); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /exp6-MultiUserChat/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define BUFSIZE 1024 11 | 12 | void send_recv(int i, int sockfd) 13 | { 14 | char send_buf[BUFSIZE]; 15 | char recv_buf[BUFSIZE]; 16 | int nbyte_recvd; 17 | if (i == 0) 18 | { 19 | fgets(send_buf, BUFSIZE, stdin); 20 | if (strcmp(send_buf , "quit\n") == 0) 21 | { 22 | exit(0); 23 | } 24 | else 25 | send(sockfd, send_buf, strlen(send_buf), 0); 26 | } 27 | else 28 | { 29 | nbyte_recvd = recv(sockfd, recv_buf, BUFSIZE, 0); 30 | 31 | if(nbyte_recvd>0) 32 | { 33 | recv_buf[nbyte_recvd] = '\0'; 34 | printf("%s" , recv_buf); 35 | fflush(stdout); 36 | } 37 | } 38 | } 39 | 40 | void connect_request(int *sockfd, struct sockaddr_in *server_addr) 41 | { 42 | if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 43 | { 44 | perror("Socket"); 45 | exit(1); 46 | } 47 | server_addr->sin_family = AF_INET; 48 | server_addr->sin_port = htons(4950); 49 | server_addr->sin_addr.s_addr = inet_addr("127.0.0.1"); 50 | memset(server_addr->sin_zero, ' ', sizeof server_addr->sin_zero); 51 | if(connect(*sockfd, (struct sockaddr *)server_addr, sizeof(struct sockaddr)) == -1) 52 | { 53 | perror("connect"); 54 | exit(1); 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | int sockfd, fdmax, i; 61 | struct sockaddr_in server_addr; 62 | fd_set master; 63 | fd_set read_fds; 64 | connect_request(&sockfd, &server_addr); 65 | FD_ZERO(&master); 66 | FD_ZERO(&read_fds); 67 | FD_SET(0, &master); 68 | FD_SET(sockfd, &master); 69 | fdmax = sockfd; 70 | while(1) 71 | { 72 | read_fds = master; 73 | if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) 74 | { 75 | perror("select"); 76 | exit(4); 77 | } 78 | for(i=0; i <= fdmax; i++ ) 79 | if(FD_ISSET(i, &read_fds)) 80 | send_recv(i, sockfd); 81 | } 82 | printf("client-quited\n"); 83 | close(sockfd); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /simplified/exp3_SlidingWindowProtocols/GoBack/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAX 80 10 | #define PORT 8080 11 | #define SA struct sockaddr 12 | 13 | void sendMessage(int sockfd, int ack) 14 | { 15 | char buffer[MAX]; 16 | sprintf(buffer, "%d", ack); 17 | send(sockfd, buffer, strlen(buffer), 0); 18 | } 19 | 20 | int main() 21 | { 22 | int sockfd, clientfd, clientLen; 23 | struct sockaddr_in serverAddr, clientAddr; 24 | 25 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 26 | 27 | memset(&serverAddr, 0, sizeof(serverAddr)); 28 | serverAddr.sin_family = AF_INET; 29 | serverAddr.sin_addr.s_addr = INADDR_ANY; 30 | serverAddr.sin_port = htons(PORT); 31 | 32 | bind(sockfd, (SA *)&serverAddr, sizeof(serverAddr)); 33 | listen(sockfd, 5); 34 | 35 | clientLen = sizeof(clientAddr); 36 | clientfd = accept(sockfd, (SA *)&clientAddr, &clientLen); 37 | 38 | char buffer[MAX]; 39 | int frame, ack, nextFrame = 0; 40 | 41 | while (1) 42 | { 43 | sleep(1); 44 | memset(buffer, 0, MAX); 45 | recv(clientfd, buffer, MAX, 0); 46 | 47 | if (strcmp("Exit", buffer) == 0) 48 | { 49 | break; 50 | } 51 | 52 | frame = atoi(buffer); 53 | 54 | if (frame != nextFrame) 55 | { 56 | printf("Frame %d discarded\n acknoledgement sent:%d\n", frame, ack); 57 | sendMessage(clientfd, ack); 58 | continue; 59 | } 60 | 61 | int randomChoice = rand() % 3; 62 | 63 | switch (randomChoice) 64 | { 65 | case 0: 66 | printf("Frame %d not received\n", frame); 67 | break; 68 | case 1: 69 | case 2: 70 | ack = frame; 71 | printf("Frame %d received\nAcknowledgement sent:%d\n", frame, ack); 72 | sendMessage(clientfd, ack); 73 | nextFrame = ack + 1; 74 | break; 75 | } 76 | } 77 | 78 | close(sockfd); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KTU S6 NETWORK LAB Repository 2 | 3 | ## Overview 4 | Welcome to the KTU S6 Network Lab repository! This repository contains all the necessary files and experiments for the KTU S6 Network Lab course. 5 | 6 | ## Instructions 7 | For detailed instructions on setting up the lab environment and performing the experiments, please refer to the [SETTING UP THE LAB PROCESS](). 8 | 9 | ## Experiments 10 | 11 | ### Operating Systems Experiments 12 | 13 | - [**SERVER CLIENT USING TCP**](https://github.com/Xrg360/NetworkLabS6/tree/master/exp1-TCP) 14 | - [server.c](https://github.com/Xrg360/NetworkLabS6/blob/master/exp1-TCP/server.c) 15 | - [client.c](https://github.com/Xrg360/NetworkLabS6/blob/master/exp1-TCP/client.c) 16 | 17 | - [**SERVER CLIENT USING UDP**](https://github.com/Xrg360/NetworkLabS6/tree/master/exp2-UDP) 18 | - [server.c](https://github.com/Xrg360/NetworkLabS6/tree/master/exp2-UDP/server.c) 19 | - [client.c](https://github.com/Xrg360/NetworkLabS6/tree/master/exp2-UDP/client.c) 20 | 21 | - [**SLIDING WINDOW PROTOCOLS**](https://github.com/Xrg360/NetworkLabS6/tree/master/exp3-SlidingWindowProtocols) 22 | - [STOP & WAIT](https://github.com/Xrg360/NetworkLabS6/tree/master/exp3-SlidingWindowProtocols/stopAndWait) 23 | - [GO BACK](https://github.com/Xrg360/NetworkLabS6/tree/master/exp3-SlidingWindowProtocols/goBack) 24 | - [SELECTIVE REPEAT](https://github.com/Xrg360/NetworkLabS6/tree/master/exp3-SlidingWindowProtocols/selectiveRepeat) 25 | 26 | - [**DISTANCE VECTOR ROUTING ALGORITHM**](https://github.com/Xrg360/NetworkLabS6/blob/master/exp4-DistanceVectorRouting/dvr.c) 27 | 28 | - [**FTP**](https://github.com/Xrg360/NetworkLabS6/tree/master/exp5-FTP) 29 | - [SERVER](https://github.com/Xrg360/NetworkLabS6/tree/master/exp5-FTP/server.c) 30 | - [CLIENT](https://github.com/Xrg360/NetworkLabS6/tree/master/exp5-FTP/client.c) 31 | 32 | - [**MULTIUSER CHAT**](https://github.com/Xrg360/NetworkLabS6/tree/master/exp6-MultiUserChat) 33 | - [SERVER](https://github.com/Xrg360/NetworkLabS6/tree/master/exp6-MultiUserChat/server.c) 34 | - [CLIENT](https://github.com/Xrg360/NetworkLabS6/tree/master/exp6-MultiUserChat/client.c) 35 | 36 | - [**LEAKY BUCKET ALGORITHM**](https://github.com/Xrg360/NetworkLabS6/blob/master/exp7-LeakyBucket/leakybkt.c) 37 | 38 | -------------------------------------------------------------------------------- /exp7-LeakyBucket/outputs.md: -------------------------------------------------------------------------------- 1 | # output for leaky bucket 2 | ``` 3 | Packet[0]:30 bytes 4 | Packet[1]:10 bytes 5 | Packet[2]:10 bytes 6 | Packet[3]:50 bytes 7 | Packet[4]:30 bytes 8 | Packet[5]:50 bytes 9 | Packet[6]:10 bytes 10 | Packet[7]:20 bytes 11 | Packet[8]:30 bytes 12 | Packet[9]:10 bytes 13 | Enter the output rate:10 14 | 15 | Enter the bucket size:50 16 | 17 | Incoming packet size: 30 18 | Bytes remaining to Transmit: 30 19 | Time left for transmission: 20 units 20 | Packet of size 10 Transmitted 21 | 20 bytes remaining to transmit 22 | Packet of size 10 Transmitted 23 | 10 bytes remaining to transmit 24 | Incoming packet size: 10 25 | Bytes remaining to Transmit: 20 26 | Time left for transmission: 30 units 27 | Packet of size 10 Transmitted 28 | 10 bytes remaining to transmit 29 | Packet of size 10 Transmitted 30 | 0 bytes remaining to transmit 31 | Time left for transmission: 0 units 32 | No packets to transmit 33 | Incoming packet size: 10 34 | Bytes remaining to Transmit: 10 35 | Time left for transmission: 10 units 36 | Packet of size 10 Transmitted 37 | 0 bytes remaining to transmit 38 | Incoming packet size: 50 39 | Bytes remaining to Transmit: 50 40 | Time left for transmission: 10 units 41 | Packet of size 10 Transmitted 42 | 40 bytes remaining to transmit 43 | Bucket capacity exceeded - Packet Rejected 44 | Bucket capacity exceeded - Packet Rejected 45 | Incoming packet size: 10 46 | Bytes remaining to Transmit: 50 47 | Time left for transmission: 30 units 48 | Packet of size 10 Transmitted 49 | 40 bytes remaining to transmit 50 | Packet of size 10 Transmitted 51 | 30 bytes remaining to transmit 52 | Packet of size 10 Transmitted 53 | 20 bytes remaining to transmit 54 | Incoming packet size: 20 55 | Bytes remaining to Transmit: 40 56 | Time left for transmission: 20 units 57 | Packet of size 10 Transmitted 58 | 30 bytes remaining to transmit 59 | Packet of size 10 Transmitted 60 | 20 bytes remaining to transmit 61 | Incoming packet size: 30 62 | Bytes remaining to Transmit: 50 63 | Time left for transmission: 10 units 64 | Packet of size 10 Transmitted 65 | 40 bytes remaining to transmit 66 | Incoming packet size: 10 67 | Bytes remaining to Transmit: 50 68 | Time left for transmission: 20 units 69 | Packet of size 10 Transmitted 70 | 40 bytes remaining to transmit 71 | Packet of size 10 Transmitted 72 | ``` 73 | -------------------------------------------------------------------------------- /simplified/exp3_SlidingWindowProtocols/StopAndWait/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int server_socket, client_socket, client_size; 9 | struct sockaddr_in server_addr, client_addr; 10 | char server_response[2000], client_message[2000]; 11 | 12 | // Clear message buffers 13 | memset(server_response, '\0', sizeof(server_response)); 14 | memset(client_message, '\0', sizeof(client_message)); 15 | 16 | // Create socket 17 | server_socket = socket(AF_INET, SOCK_STREAM, 0); 18 | 19 | // Set server address properties 20 | server_addr.sin_family = AF_INET; 21 | server_addr.sin_port = htons(8080); 22 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 23 | 24 | // Bind socket to the specified port and address 25 | bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)); 26 | 27 | // Listen for incoming connections 28 | listen(server_socket, 1); 29 | 30 | // Accept an incoming connection 31 | client_size = sizeof(client_addr); 32 | client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_size); 33 | 34 | printf("Client connected at IP: %s and port: %i\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); 35 | 36 | while (1) 37 | { 38 | // Receive message from client 39 | recv(client_socket, client_message, sizeof(client_message), 0); 40 | printf("Msg from client: %s\n", client_message); 41 | z: 42 | // Clear message buffers 43 | memset(server_response, '\0', sizeof(server_response)); 44 | memset(client_message, '\0', sizeof(client_message)); 45 | 46 | // Prompt for server response 47 | printf("Server Acknowledges or not? Ack-Y or y, NACK- N or n: "); 48 | scanf("%s", server_response); 49 | 50 | // Send server response to client 51 | send(client_socket, server_response, strlen(server_response), 0); 52 | 53 | // If acknowledgment not received by client, prompt again 54 | if (strcmp(server_response, "N") == 0 || strcmp(server_response, "n") == 0) 55 | { 56 | printf("Acknowledgment not received by client\n"); 57 | goto z; 58 | } 59 | } 60 | 61 | // Close sockets 62 | close(client_socket); 63 | close(server_socket); 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/goBack/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAX 80 10 | #define PORT 8080 11 | #define SA struct sockaddr 12 | 13 | void sendMessage(int sockfd, int ack) { 14 | char buff[MAX]; 15 | sprintf(buff, "%d", ack); 16 | send(sockfd, buff, strlen(buff), 0); 17 | } 18 | 19 | int main() { 20 | int sockfd, connfd, len; 21 | struct sockaddr_in serveraddr, client; 22 | 23 | // Create socket 24 | if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 25 | printf("socket creation failed\n"); 26 | exit(0); 27 | } 28 | else 29 | printf("socket created successfully\n"); 30 | 31 | memset(&serveraddr, 0, sizeof(serveraddr)); 32 | serveraddr.sin_family = AF_INET; 33 | serveraddr.sin_addr.s_addr = INADDR_ANY; 34 | serveraddr.sin_port = htons(PORT); 35 | 36 | if (bind(sockfd, (SA *) &serveraddr, sizeof(serveraddr)) < 0) { 37 | perror("Bind failed. Error"); 38 | return 1; 39 | } 40 | printf("Bind done\n"); 41 | 42 | listen(sockfd, 5); 43 | printf("server listening .. \n"); 44 | 45 | len = sizeof(client); 46 | connfd = accept(sockfd, (SA *) &client, &len); 47 | 48 | char buff[MAX]; 49 | int f, c, ack, next = 0; 50 | 51 | while(1) { 52 | sleep(1); 53 | memset(buff, 0, MAX); 54 | recv(connfd, buff, MAX, 0); 55 | 56 | if(strcmp("Exit", buff) == 0) { 57 | printf("exit\n"); 58 | break; 59 | } 60 | 61 | f = atoi(buff); 62 | 63 | if(f != next) { 64 | printf("Frame %d discarded\n acknoledgement sent:%d\n", f, ack); 65 | sendMessage(connfd, ack); 66 | continue; 67 | } 68 | 69 | c = rand() % 3; 70 | 71 | switch(c) { 72 | case 0: 73 | printf("Frame %d not received\n", f); 74 | break; 75 | case 1: 76 | ack = f; 77 | sleep(2); 78 | printf("Frame %d received\nAcknowledgement sent:%d\n", f, ack); 79 | sendMessage(connfd, ack); 80 | next = ack + 1; 81 | break; 82 | case 2: 83 | ack = f; 84 | printf("Frame %d received\nAcknowledgement sent:%d\n", f, ack); 85 | sendMessage(connfd, ack); 86 | next = ack + 1; 87 | break; 88 | } 89 | } 90 | 91 | close(sockfd); 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/stopAndWait/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | int socket_desc; 8 | int timer, frame; 9 | struct sockaddr_in server_addr; 10 | char server_message[2000], client_message[2000]; 11 | 12 | memset(server_message, '\0', sizeof(server_message)); 13 | memset(client_message, '\0', sizeof(client_message)); 14 | 15 | socket_desc = socket(AF_INET, SOCK_STREAM, 0); 16 | if (socket_desc < 0) 17 | { 18 | printf("Unable to create socket\n"); 19 | return -1; 20 | } 21 | printf("Socket created successfully\n"); 22 | 23 | server_addr.sin_family = AF_INET; 24 | server_addr.sin_port = htons(8080); 25 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 26 | 27 | if (connect(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 28 | { 29 | printf("Unable to connect\n"); 30 | return -1; 31 | } 32 | printf("Connected with server successfully\n"); 33 | printf("Enter the total frames: "); 34 | scanf("%d", &frame); 35 | 36 | while (frame > 0) 37 | { 38 | 39 | memset(server_message, '\0', sizeof(server_message)); 40 | memset(client_message, '\0', sizeof(client_message)); 41 | printf("\nEnter frame message: "); 42 | scanf("%s", client_message); 43 | 44 | x: 45 | timer = 10; 46 | if (send(socket_desc, client_message, strlen(client_message), 0) < 0) 47 | { 48 | printf("Unable to send message\n"); 49 | return -1; 50 | } 51 | 52 | y: 53 | if (timer > 0) 54 | { 55 | if (recv(socket_desc, server_message, sizeof(server_message), 0) < 0) 56 | { 57 | printf("Error while receiving server's msg\n"); 58 | timer--; 59 | goto y; 60 | } 61 | if (strcmp(server_message, "N") == 0 || strcmp(server_message, "n") == 0) 62 | { 63 | printf("\nWaiting..."); 64 | timer--; 65 | goto y; 66 | } 67 | 68 | if (strcmp(server_message, "Y") == 0 || strcmp(server_message, "y") == 0) 69 | { 70 | 71 | printf("\nServer acknowledged"); 72 | timer = 0; 73 | frame--; 74 | } 75 | } 76 | else 77 | { 78 | 79 | printf("\nServer donot acknowledged\nResending...."); 80 | 81 | goto x; 82 | } 83 | } 84 | close(socket_desc); 85 | return 0; 86 | } -------------------------------------------------------------------------------- /simplified/exp3_SlidingWindowProtocols/StopAndWait/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(void) 7 | { 8 | int client_socket, frame_count, timer; 9 | struct sockaddr_in server_addr; 10 | char server_response[2000], frame_message[2000]; 11 | 12 | // Clear message buffers 13 | memset(server_response, '\0', sizeof(server_response)); 14 | memset(frame_message, '\0', sizeof(frame_message)); 15 | 16 | // Create socket 17 | client_socket = socket(AF_INET, SOCK_STREAM, 0); 18 | 19 | // Set server address properties 20 | server_addr.sin_family = AF_INET; 21 | server_addr.sin_port = htons(8080); 22 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 23 | 24 | // Connect to the server 25 | connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)); 26 | 27 | printf("Connected to the server\n"); 28 | printf("Enter the total number of frames: "); 29 | scanf("%d", &frame_count); 30 | 31 | while (frame_count > 0) 32 | { 33 | // Clear message buffers 34 | memset(server_response, '\0', sizeof(server_response)); 35 | memset(frame_message, '\0', sizeof(frame_message)); 36 | 37 | printf("Enter frame message: "); 38 | scanf("%s", frame_message); 39 | 40 | while (1) 41 | { 42 | timer = 10; 43 | 44 | // Send frame message to server 45 | send(client_socket, frame_message, strlen(frame_message), 0); 46 | 47 | while (timer > 0) 48 | { 49 | // Receive server response 50 | recv(client_socket, server_response, sizeof(server_response), 0); 51 | 52 | if (strcmp(server_response, "Y") == 0 || strcmp(server_response, "y") == 0) 53 | { 54 | printf("Server acknowledged\n"); 55 | frame_count--; 56 | break; 57 | } 58 | 59 | if (strcmp(server_response, "N") == 0 || strcmp(server_response, "n") == 0) 60 | { 61 | printf("Waiting for acknowledgment...\n"); 62 | timer--; 63 | } 64 | } 65 | 66 | if (timer == 0) 67 | { 68 | printf("Server did not acknowledge. Resending...\n"); 69 | } 70 | else 71 | { 72 | break; 73 | } 74 | } 75 | } 76 | 77 | // Close socket 78 | close(client_socket); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/stopAndWait/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main() 6 | { 7 | int socket_desc, client_sock, client_size, frame; 8 | struct sockaddr_in server_addr, client_addr; 9 | char server_message[2000], client_message[2000]; 10 | 11 | memset(server_message, '\0', sizeof(server_message)); 12 | memset(client_message, '\0', sizeof(client_message)); 13 | 14 | socket_desc = socket(AF_INET, SOCK_STREAM, 0); 15 | if (socket_desc < 0) 16 | { 17 | printf("Error while creating socket\n"); 18 | return -1; 19 | } 20 | printf("Socket created successfully\n"); 21 | 22 | server_addr.sin_family = AF_INET; 23 | server_addr.sin_port = htons(8080); 24 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 25 | 26 | if (bind(socket_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) 27 | { 28 | printf("Couldn't bind to the port\n"); 29 | return -1; 30 | } 31 | printf("Done with binding\n"); 32 | 33 | if (listen(socket_desc, 1) < 0) 34 | { 35 | printf("Error while listening\n"); 36 | return -1; 37 | } 38 | printf("\nListening for incoming connections.....\n"); 39 | 40 | client_size = sizeof(client_addr); 41 | client_sock = accept(socket_desc, (struct sockaddr *)&client_addr, &client_size); 42 | if (client_sock < 0) 43 | { 44 | printf("Can't accept\n"); 45 | return -1; 46 | } 47 | 48 | printf("Client connected at IP: %s and port: %i\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); 49 | 50 | while (1) 51 | { 52 | 53 | if (recv(client_sock, client_message, sizeof(client_message), 0) < 0) 54 | { 55 | printf("Couldn't receive\n"); 56 | return -1; 57 | } 58 | printf("Msg from client: %s\n", client_message); 59 | 60 | memset(server_message, '\0', sizeof(server_message)); 61 | memset(client_message, '\0', sizeof(client_message)); 62 | printf("\nServer Acknowledges or not:? Ack-Y or y, NACK- N or n"); 63 | z: 64 | memset(server_message, '\0', sizeof(server_message)); 65 | scanf("%s", server_message); 66 | 67 | if (send(client_sock, server_message, strlen(server_message), 0) < 0) 68 | { 69 | printf("Can't send\n"); 70 | return -1; 71 | } 72 | if (strcmp(server_message, "N") == 0 || strcmp(server_message, "n") == 0) 73 | { 74 | printf("Acknowledgement not received by client"); 75 | goto z; 76 | } 77 | } 78 | 79 | close(client_sock); 80 | close(socket_desc); 81 | return 0; 82 | } -------------------------------------------------------------------------------- /exp4-DistanceVectorRouting/README.md: -------------------------------------------------------------------------------- 1 | # Network Lab - Experiment: Distance Vector Routing 2 | 3 | ## Objective: 4 | The objective of this experiment is to understand the principles and implementations of distance vector routing algorithms in computer networks. 5 | 6 | ## Experiment Overview: 7 | In this experiment, we explore distance vector routing, a type of distributed routing algorithm used in computer networks. Distance vector routing algorithms enable routers to dynamically calculate the best path to reach a destination based on distance metrics. 8 | 9 | ## Experiment Procedure: 10 | 1. **Setup Network Topology:** 11 | - Create a network topology consisting of routers interconnected through links. 12 | 13 | 2. **Implement Distance Vector Routing Algorithm:** 14 | - Implement the distance vector routing algorithm on each router to calculate the shortest path to reach all destinations in the network. 15 | 16 | 3. **Exchange Routing Information:** 17 | - Simulate the exchange of routing information between neighboring routers to update their routing tables. 18 | 19 | 4. **Convergence and Stability:** 20 | - Monitor the convergence of the routing algorithm to ensure that all routers have consistent and stable routing tables. 21 | 22 | 5. **Performance Evaluation:** 23 | - Evaluate the performance of the distance vector routing algorithm in terms of convergence time, routing table size, and scalability under various network conditions. 24 | 25 | ## Viva Questions: 26 | 1. What is distance vector routing, and how does it differ from link-state routing? 27 | 2. Explain the concept of distance vectors and routing tables in distance vector routing algorithms. 28 | 3. How does a router calculate the shortest path to reach a destination in distance vector routing? 29 | 4. Discuss the Bellman-Ford equation and its significance in distance vector routing. 30 | 5. What are the advantages and limitations of distance vector routing algorithms? 31 | 6. Describe the process of exchanging routing information between neighboring routers in distance vector routing. 32 | 7. How does distance vector routing handle routing loops and count-to-infinity problem? 33 | 8. Explain the concept of split horizon and poison reverse in distance vector routing. 34 | 9. Discuss the impact of network topology changes on the convergence of distance vector routing algorithms. 35 | 10. How does distance vector routing handle network partitions and route failures? 36 | 11. What are the common metrics used in distance vector routing for calculating distances between routers? 37 | 12. Describe the process of route advertisement and route updates in distance vector routing protocols. 38 | 13. What is the role of periodic updates and triggered updates in distance vector routing algorithms? 39 | 14. Discuss the scalability issues associated with distance vector routing algorithms. 40 | 15. Compare and contrast distance vector routing with other routing algorithms such as link-state routing and path-vector routing. 41 | -------------------------------------------------------------------------------- /exp2-UDP/README.md: -------------------------------------------------------------------------------- 1 | # Network Lab - Experiment: Server-Client Communication using UDP 2 | 3 | ## Contents: 4 | 1. [Objective](https://github.com/Xrg360/networkLabS6/blob/master/exp2-UDP/README.md#objective) 5 | 2. [Experiment Overview](https://github.com/Xrg360/networkLabS6/tree/master/exp2-UDP#experiment-overview) 6 | 1. SERVER ALGORITHM 7 | 2. CLIENT ALGORITHM 8 | 3. [Outputs](https://github.com/Xrg360/networkLabS6/blob/master/exp2-UDP/outputs.md) 9 | 4. [Viva Questions](https://github.com/Xrg360/networkLabS6/tree/master/exp2-UDP#viva-questions) 10 | 11 | ## Objective: 12 | The objective of this experiment is to understand the basics of server-client communication using UDP (User Datagram Protocol). 13 | 14 | ## Experiment Overview: 15 | In this experiment, we implemented a simple client-server model using UDP sockets. Unlike TCP, UDP provides a connectionless communication model, where data is sent as packets without establishing a direct connection between the client and server. This experiment focuses on understanding UDP's characteristics and its application in network communication. 16 | 17 | ## Experiment Procedure: 18 | 1. **Setting up the Server:** 19 | - The server program is executed, which binds to a specific port and starts listening for incoming packets. 20 | - Upon receiving a packet from a client, the server processes the data and sends back a response (if required). 21 | 22 | 2. **Setting up the Client:** 23 | - The client program is executed, which sends packets containing data to the server's IP address and port number. 24 | - The client may or may not receive a response from the server, depending on the application logic. 25 | 26 | 3. **Server-Client Communication:** 27 | - The client sends data packets to the server without establishing a direct connection. 28 | - The server processes incoming packets and responds accordingly, if necessary. 29 | 30 | 4. **Termination:** 31 | - After communication is complete, both the client and server close their respective sockets and terminate the connection. 32 | 33 | ## Viva Questions: 34 | 1. What is UDP (User Datagram Protocol), and how does it differ from TCP (Transmission Control Protocol)? 35 | 2. Explain the advantages and disadvantages of UDP compared to TCP. 36 | 3. What is the purpose of the checksum field in the UDP header? How does it ensure data integrity? 37 | 4. Describe the process of establishing a connection in UDP communication. 38 | 5. How does UDP handle packet loss and reordering? 39 | 6. Discuss the significance of the connectionless nature of UDP. 40 | 7. Explain the role of ports in UDP communication. 41 | 8. How does UDP ensure message delivery without acknowledgments or retransmissions? 42 | 9. What are the common applications of UDP in networking? 43 | 10. Describe the differences in error handling between UDP and TCP. 44 | 11. Discuss the impact of packet size on UDP communication. 45 | 12. How can you implement reliability and flow control mechanisms in UDP applications? 46 | 13. Explain the concept of multicast in UDP communication. 47 | 14. How does UDP handle congestion control? 48 | 15. Compare and contrast UDP socket programming with TCP socket programming. 49 | 50 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/README.md: -------------------------------------------------------------------------------- 1 | # Network Lab - Experiment: Sliding Window Protocol 2 | 3 | ## Objective: 4 | The objective of this experiment is to understand the principles and implementations of the sliding window protocol in computer networks. This includes exploring three variations: Stop-and-Wait, Go-Back-N, and Selective Repeat. 5 | 6 | ## Experiment Overview: 7 | In this experiment, we explore the sliding window protocol, which is used for reliable data transfer in computer networks. The sliding window protocol allows for efficient utilization of network resources by allowing multiple packets to be in transit simultaneously. 8 | 9 | ## Experiment Procedure: 10 | 1. [**Go-Back-N Protocol:**](https://github.com/Xrg360/networkLabS6/tree/master/exp3-SlidingWindowProtocols/goBack) 11 | - Implement the Go-Back-N protocol, where the sender can transmit multiple packets before receiving acknowledgments from the receiver. However, if an acknowledgment for a packet is not received within a specified timeout period, the sender retransmits all packets starting from the lost packet. 12 | 13 | 2. [**Selective Repeat Protocol:**](https://github.com/Xrg360/networkLabS6/tree/master/exp3-SlidingWindowProtocols/selectiveRepeat) 14 | - Implement the Selective Repeat protocol, where the sender can transmit multiple packets before receiving acknowledgments from the receiver. In this protocol, the receiver individually acknowledges each packet, allowing the sender to retransmit only the lost packets. 15 | 16 | 3. [**Stop-and-Wait Protocol:**](https://github.com/Xrg360/networkLabS6/tree/master/exp3-SlidingWindowProtocols/stopAndWait) 17 | - Implement the stop-and-wait protocol, where the sender sends one packet and waits for an acknowledgment from the receiver before sending the next packet. 18 | 19 | 20 | 21 | ## Viva Questions: 22 | 1. What is the sliding window protocol, and what problem does it solve in computer networks? 23 | 2. Explain the concept of flow control in the sliding window protocol. 24 | 3. What is the difference between Stop-and-Wait, Go-Back-N, and Selective Repeat protocols? 25 | 4. How does the Stop-and-Wait protocol work? Discuss its advantages and limitations. 26 | 5. Describe the operation of the Go-Back-N protocol. What is the significance of the window size in this protocol? 27 | 6. How does the Selective Repeat protocol differ from Go-Back-N? Discuss its advantages and limitations. 28 | 7. What are the key parameters affecting the performance of the sliding window protocol? 29 | 8. Explain the concept of acknowledgment and acknowledgment number in sliding window protocols. 30 | 9. How does the sender handle timeouts and retransmissions in Go-Back-N and Selective Repeat protocols? 31 | 10. Discuss the impact of window size on the efficiency and reliability of the sliding window protocol. 32 | 11. What are the common scenarios in which Go-Back-N outperforms Selective Repeat, and vice versa? 33 | 12. Explain the role of sequence numbers in the sliding window protocol. 34 | 13. Discuss the mechanisms used for error detection and correction in sliding window protocols. 35 | 14. How does the sliding window protocol handle out-of-order packets and packet loss? 36 | 15. Compare and contrast the performance of Stop-and-Wait, Go-Back-N, and Selective Repeat protocols under various network conditions. 37 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/goBack/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAX 80 10 | #define PORT 8080 11 | #define SA struct sockaddr 12 | 13 | void sendData(int sockfd, int nf, int ws) { 14 | char buff[MAX]; 15 | int ack, i = 0, n, k, w1 = 0, w2 = ws - 1, j, flag = 0; 16 | struct timeval timeout; 17 | timeout.tv_sec = 3; 18 | timeout.tv_usec = 0; 19 | 20 | if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)) < 0) 21 | perror("setsockopt(SO_RCVTIMEO) failed"); 22 | 23 | while(1) { 24 | for(i = 0; i < nf && i <= w2; i++) { 25 | snprintf(buff, sizeof(buff), "%d", i); 26 | k = send(sockfd, buff, sizeof(buff), 0); 27 | printf("Frame %d sent\n", i); 28 | } 29 | 30 | while(1) { 31 | if(w2 - w1 != ws - 1 && flag == 0 && i != nf) { 32 | snprintf(buff, sizeof(buff), "%d", i); 33 | k = send(sockfd, buff, sizeof(buff), 0); 34 | printf("Frame %d sent\n", i); 35 | w2++; 36 | i++; 37 | } 38 | 39 | flag = 0; 40 | n = recv(sockfd, buff, MAX, 0); 41 | ack = atoi(buff); 42 | 43 | if(n > 0) { 44 | if(ack + 1 == nf) { 45 | printf("Ack received %d\nEXIT\n", ack); 46 | strcpy(buff, "EXIT"); 47 | k = send(sockfd, buff, sizeof(buff), 0); 48 | return; 49 | } 50 | 51 | if(ack == w1) { 52 | w1++; 53 | printf("Ack received %d\n", ack); 54 | } 55 | } else { 56 | printf("Ack not received for %d\nRESENDING ...\n", ack); 57 | 58 | for(j = w1; j < nf && j < w1 + ws; j++) { 59 | snprintf(buff, sizeof(buff), "%d", j); 60 | k = send(sockfd, buff, sizeof(buff), 0); 61 | printf("Frame %d sent\n", j); 62 | } 63 | flag = 1; 64 | } 65 | } 66 | } 67 | } 68 | 69 | int main() { 70 | int sockfd, f, w; 71 | struct sockaddr_in server; 72 | 73 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 74 | 75 | if(sockfd < 0) { 76 | printf("Socket creation failed\n"); 77 | exit(0); 78 | } 79 | 80 | printf("Socket created\n"); 81 | 82 | memset(&server, 0, sizeof(server)); 83 | 84 | server.sin_family = AF_INET; 85 | server.sin_addr.s_addr = INADDR_ANY; 86 | server.sin_port = htons(PORT); 87 | 88 | if(connect(sockfd, (SA*)&server, sizeof(server)) != 0) { 89 | printf("Connection failed\n"); 90 | exit(0); 91 | } 92 | 93 | printf("Connected successfully\n"); 94 | 95 | printf("Enter the number of frames: "); 96 | scanf("%d", &f); 97 | 98 | printf("Enter the window size: "); 99 | scanf("%d", &w); 100 | 101 | sendData(sockfd, f, w); 102 | 103 | close(sockfd); 104 | 105 | return 0; 106 | } 107 | -------------------------------------------------------------------------------- /simplified/exp3_SlidingWindowProtocols/GoBack/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define MAX 80 8 | #define PORT 8080 9 | #define SA struct sockaddr 10 | 11 | void sendData(int sockfd, int totalFrames, int windowSize) 12 | { 13 | char buffer[MAX]; 14 | int ack, frameIndex = 0, n, sendStatus, windowStart = 0, windowEnd = windowSize - 1, retryFlag = 0; 15 | struct timeval timeout; 16 | timeout.tv_sec = 3; 17 | timeout.tv_usec = 0; 18 | 19 | setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)); 20 | 21 | while (1) 22 | { 23 | for (frameIndex = 0; frameIndex < totalFrames && frameIndex <= windowEnd; frameIndex++) 24 | { 25 | snprintf(buffer, sizeof(buffer), "%d", frameIndex); 26 | sendStatus = send(sockfd, buffer, sizeof(buffer), 0); 27 | printf("Frame %d sent\n", frameIndex); 28 | } 29 | 30 | while (1) 31 | { 32 | if (windowEnd - windowStart != windowSize - 1 && retryFlag == 0 && frameIndex != totalFrames) 33 | { 34 | snprintf(buffer, sizeof(buffer), "%d", frameIndex); 35 | sendStatus = send(sockfd, buffer, sizeof(buffer), 0); 36 | printf("Frame %d sent\n", frameIndex); 37 | windowEnd++; 38 | frameIndex++; 39 | } 40 | 41 | retryFlag = 0; 42 | n = recv(sockfd, buffer, MAX, 0); 43 | ack = atoi(buffer); 44 | 45 | if (n > 0) 46 | { 47 | if (ack + 1 == totalFrames) 48 | { 49 | printf("Ack received %d\nEXIT\n", ack); 50 | strcpy(buffer, "EXIT"); 51 | send(sockfd, buffer, sizeof(buffer), 0); 52 | return; 53 | } 54 | 55 | if (ack == windowStart) 56 | { 57 | windowStart++; 58 | printf("Ack received %d\n", ack); 59 | } 60 | } 61 | else 62 | { 63 | printf("Ack not received for %d\nRESENDING ...\n", ack); 64 | for (int j = windowStart; j < totalFrames && j < windowStart + windowSize; j++) 65 | { 66 | snprintf(buffer, sizeof(buffer), "%d", j); 67 | sendStatus = send(sockfd, buffer, sizeof(buffer), 0); 68 | printf("Frame %d sent\n", j); 69 | } 70 | retryFlag = 1; 71 | } 72 | } 73 | } 74 | } 75 | 76 | int main() 77 | { 78 | int sockfd, totalFrames, windowSize; 79 | struct sockaddr_in server; 80 | 81 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 82 | 83 | memset(&server, 0, sizeof(server)); 84 | 85 | server.sin_family = AF_INET; 86 | server.sin_addr.s_addr = INADDR_ANY; 87 | server.sin_port = htons(PORT); 88 | 89 | connect(sockfd, (SA *)&server, sizeof(server)); 90 | 91 | printf("Enter the number of frames: "); 92 | scanf("%d", &totalFrames); 93 | printf("Enter the window size: "); 94 | scanf("%d", &windowSize); 95 | 96 | sendData(sockfd, totalFrames, windowSize); 97 | 98 | close(sockfd); 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/selectiveRepeat/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define MAX 80 10 | #define PORT 8080 11 | #define SA struct sockaddr 12 | 13 | struct timeval timeout; 14 | 15 | void func(int sockfd,int nf,int ws){ 16 | char buff[MAX]; 17 | int ack,i=0,n,nk,w1=1,w2=ws,j,flag=0; 18 | if(setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(const char*)&timeout,sizeof(timeout))<0){ 19 | perror("setsockopt(SO_RCVTIMEO) failed"); 20 | } 21 | 22 | for(i=1;i<=nf && i<=w2;i++){ 23 | bzero(buff,sizeof(buff)); 24 | nk=send(sockfd,buff,sizeof(buff),0); 25 | printf("Frame %d sent\n",i); 26 | } 27 | 28 | while(1){ 29 | while (w2-w1!=ws-1 &&w2!=nf) 30 | { 31 | w2++; 32 | bzero(buff,sizeof(buff)); 33 | snprintf(buff,sizeof(buff),"%d",w2); 34 | nk=send(sockfd,buff,sizeof(buff),0); 35 | printf("Frame %d sent\n",w2); 36 | } 37 | flag=0; 38 | bzero(buff,sizeof(buff)); 39 | n=recv(sockfd,buff,MAX,0); 40 | ack=atoi(buff); 41 | if (n>0) 42 | { 43 | if (ack<0) 44 | { 45 | printf("Ack not received for %d\nRESENDING ...\n",ack); 46 | bzero(buff,sizeof(buff)); 47 | snprintf(buff,sizeof(buff),"%d",ack); 48 | nk=send(sockfd,buff,sizeof(buff),0); 49 | } 50 | else if (ack>=nf) 51 | { 52 | printf("Ack received %d\nEXIT\n",ack); 53 | bzero(buff,sizeof(buff)); 54 | strcpy(buff,"EXIT"); 55 | break; 56 | } 57 | else if (ack>=w1 && ack<=w2) 58 | { 59 | w1=ack+1; 60 | printf("Ack received %d\n",ack); 61 | 62 | } 63 | 64 | 65 | } 66 | else 67 | { 68 | printf("Ack not received for %d\nRESENDING ...\n",w1); 69 | bzero(buff,sizeof(buff)); 70 | snprintf(buff,sizeof(buff),"%d",w1); 71 | nk=send(sockfd,buff,sizeof(buff),0); 72 | flag=1; 73 | } 74 | 75 | 76 | } 77 | } 78 | 79 | 80 | void main(){ 81 | int sockfd,connfd; 82 | struct sockaddr_in serveraddr,client; 83 | 84 | sockfd = socket(AF_INET,SOCK_STREAM,0); 85 | if(sockfd<0){ 86 | printf("Socket creation failed\n"); 87 | exit(0); 88 | } 89 | else 90 | { 91 | printf("Socket created\n"); 92 | } 93 | memset(&serveraddr,0,sizeof(serveraddr)); 94 | serveraddr.sin_family=AF_INET; 95 | serveraddr.sin_addr.s_addr=INADDR_ANY; 96 | serveraddr.sin_port=htons(PORT); 97 | 98 | timeout.tv_sec=3; 99 | timeout.tv_usec=0; 100 | 101 | if (connect(sockfd,(SA*)&serveraddr,sizeof(serveraddr))!=0) 102 | { 103 | printf("Connection with server failed\n"); 104 | exit(0); 105 | } 106 | else 107 | { 108 | printf("Connected to server\n"); 109 | } 110 | printf("Enter the number of frames to be sent\n"); 111 | int nf; 112 | scanf("%d",&nf); 113 | printf("Enter the window size\n"); 114 | int ws; 115 | scanf("%d",&ws); 116 | func(sockfd,nf,ws); 117 | close(sockfd); 118 | } -------------------------------------------------------------------------------- /exp3-SlidingWindowProtocols/selectiveRepeat/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define MAX 80 12 | #define PORT 8080 13 | #define SA struct sockaddr 14 | 15 | struct timeval timeout; 16 | 17 | void func(int connfd){ 18 | char buff[MAX]; 19 | int flag[20]= {0}; 20 | int f,c,ack,next=0,w1=1,w2,ws,nacksend=0; 21 | printf("Enter the window size of the server: "); 22 | scanf("%d",&ws); 23 | w2=ws-1; 24 | while (1) 25 | { 26 | sleep(1); 27 | memset(buff,0,MAX); 28 | recv(connfd,buff,MAX,0); 29 | if (strcmp("Exit",buff)==0) 30 | { 31 | printf("Exit\n"); 32 | break; 33 | } 34 | f=atoi(buff); 35 | c = rand()%2; 36 | switch (c) 37 | { 38 | case 0: 39 | continue; 40 | break; 41 | case 1: sleep(2); 42 | break; 43 | 44 | default: 45 | break; 46 | } 47 | 48 | if (f>=w1 && flag[f]==0) 49 | { 50 | if (f==w1) 51 | { 52 | nacksend =0 ; 53 | flag[f]=1; 54 | for(;w1<=w2 && flag[w1]==1;w1++); 55 | printf("Frame %d received ackknowledgement sent for %d\n",f,w1-1); 56 | bzero(buff,sizeof(buff)); 57 | snprintf(buff,sizeof(buff),"%d",w1-1); 58 | send(connfd,buff,sizeof(buff),0); 59 | w2 = w1+ws; 60 | } 61 | else{ 62 | if (nacksend == 0) 63 | { 64 | printf("Frame %d received instead of %d\n sending NAK %d",f,w1,-1*w1); 65 | bzero(buff,sizeof(buff)); 66 | snprintf(buff,sizeof(buff),"%d",-1*w1); 67 | send(connfd,buff,sizeof(buff),0); 68 | nacksend = 1; 69 | flag[f]=1; 70 | } 71 | else{ 72 | printf("Frame %d received instead of %d\n",f,w1); 73 | flag[f]=1; 74 | continue; 75 | } 76 | 77 | } 78 | 79 | } 80 | // else 81 | // printf("Frame %d discarded\n",f); 82 | 83 | 84 | } 85 | 86 | 87 | } 88 | 89 | void main(){ 90 | int sockfd,connfd,len; 91 | struct sockaddr_in serveraddr,client; 92 | sockfd = socket(AF_INET,SOCK_STREAM,0); 93 | if(sockfd<0){ 94 | printf("Socket creation failed\n"); 95 | exit(0); 96 | } 97 | else 98 | printf("Socket created\n"); 99 | 100 | memset(&serveraddr,0,sizeof(serveraddr)); 101 | serveraddr.sin_family = AF_INET; 102 | serveraddr.sin_addr.s_addr = INADDR_ANY; 103 | serveraddr.sin_port = htons(PORT); 104 | 105 | if(bind(sockfd,(SA*)&serveraddr,sizeof(serveraddr))<0){ 106 | perror("Bind failed. Error"); 107 | return; 108 | } 109 | printf("Bind done\n"); 110 | 111 | listen(sockfd,5); 112 | printf("Server listening .. \n"); 113 | 114 | len = sizeof(client); 115 | connfd = accept(sockfd,(SA*)&client,&len); 116 | 117 | if (connfd<0) 118 | { 119 | printf("Server acccept failed\n"); 120 | exit(0); 121 | } 122 | else 123 | { 124 | printf("Server acccept the client\n"); 125 | } 126 | 127 | printf("accepted"); 128 | func(connfd); 129 | close(sockfd); 130 | 131 | } -------------------------------------------------------------------------------- /exp6-MultiUserChat/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define PORT 4950 11 | #define BUFSIZE 1024 12 | 13 | void send_to_all(int j, int i, int sockfd, int nbytes_recvd, char *recv_buf, fd_set *master) 14 | { 15 | if (FD_ISSET(j, master)) 16 | { 17 | if (j != sockfd && j != i) 18 | { 19 | if (send(j, recv_buf, nbytes_recvd, 0) == -1) 20 | { 21 | perror("send"); 22 | } 23 | } 24 | } 25 | } 26 | 27 | void send_recv(int i, fd_set *master, int sockfd, int fdmax) 28 | { 29 | int nbytes_recvd, j; 30 | char recv_buf[BUFSIZE], buf[BUFSIZE]; 31 | if ((nbytes_recvd = recv(i, recv_buf, BUFSIZE, 0)) <= 0) 32 | { 33 | if (nbytes_recvd == 0) 34 | { 35 | printf("socket %d hung up\n", i); 36 | } 37 | else 38 | { 39 | perror("recv"); 40 | } 41 | close(i); 42 | FD_CLR(i, master); 43 | } 44 | else 45 | { 46 | //printf("%s\n", recv_buf); 47 | for(j = 0; j <= fdmax; j++) 48 | { 49 | send_to_all(j, i, sockfd, nbytes_recvd, recv_buf, master ); 50 | } 51 | } 52 | } 53 | 54 | void connection_accept(fd_set *master, int *fdmax, int sockfd, struct sockaddr_in *client_addr) 55 | { 56 | socklen_t addrlen; 57 | int newsockfd; 58 | addrlen = sizeof(struct sockaddr_in); 59 | if((newsockfd = accept(sockfd, (struct sockaddr *)client_addr, &addrlen)) == -1) 60 | { 61 | perror("accept"); 62 | exit(1); 63 | } 64 | else 65 | { 66 | FD_SET(newsockfd, master); 67 | if(newsockfd > *fdmax) 68 | { 69 | *fdmax = newsockfd; 70 | } 71 | printf("new connection from %s on port %d \n",inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); 72 | } 73 | } 74 | 75 | void connect_request(int *sockfd, struct sockaddr_in *my_addr) 76 | { 77 | int yes = 1; 78 | if ((*sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 79 | { 80 | perror("Socket"); 81 | exit(1); 82 | } 83 | my_addr->sin_family = AF_INET; 84 | my_addr->sin_port = htons(4950); 85 | my_addr->sin_addr.s_addr = INADDR_ANY; 86 | memset(my_addr->sin_zero, ' ', sizeof my_addr->sin_zero); 87 | if (setsockopt(*sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) 88 | { 89 | perror("setsockopt"); 90 | exit(1); 91 | } 92 | if (bind(*sockfd, (struct sockaddr *)my_addr, sizeof(struct sockaddr)) == -1) 93 | { 94 | perror("Unable to bind"); 95 | exit(1); 96 | } 97 | if (listen(*sockfd, 10) == -1) 98 | { 99 | perror("listen"); 100 | exit(1); 101 | } 102 | printf("\nTCPServer Waiting for client on port 4950\n"); 103 | fflush(stdout); 104 | } 105 | 106 | int main() 107 | { 108 | fd_set master; 109 | fd_set read_fds; 110 | int fdmax, i; 111 | int sockfd= 0; 112 | struct sockaddr_in my_addr, client_addr; 113 | FD_ZERO(&master); 114 | FD_ZERO(&read_fds); 115 | connect_request(&sockfd, &my_addr); 116 | FD_SET(sockfd, &master); 117 | fdmax = sockfd; 118 | while(1) 119 | { 120 | read_fds = master; 121 | if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) 122 | { 123 | perror("select"); 124 | exit(4); 125 | } 126 | for (i = 0; i <= fdmax; i++) 127 | { 128 | if (FD_ISSET(i, &read_fds)) 129 | { 130 | if (i == sockfd) 131 | connection_accept(&master, &fdmax, sockfd, &client_addr); 132 | else 133 | send_recv(i, &master, sockfd, fdmax); 134 | } 135 | } 136 | } 137 | return 0; 138 | } 139 | -------------------------------------------------------------------------------- /exp1-TCP/README.md: -------------------------------------------------------------------------------- 1 | # Network Lab - Experiment: Server-Client Communication using TCP 2 | 3 | ## Contents: 4 | 1. [Objective](https://github.com/Xrg360/networkLabS6/blob/master/exp1-TCP/README.md#objective) 5 | 2. [Experiment Overview](https://github.com/Xrg360/networkLabS6/tree/master/exp1-TCP#experiment-overview) 6 | 1. SERVER ALGORITHM 7 | 2. CLIENT ALGORITHM 8 | 3. [Outputs](https://github.com/Xrg360/networkLabS6/blob/master/exp1-TCP/outputs.md) 9 | 4. [Viva Questions](https://github.com/Xrg360/networkLabS6/tree/master/exp1-TCP#viva-questions) 10 | 11 | 12 | ## Objective: 13 | The objective of this experiment is to understand the basics of server-client communication using TCP (Transmission Control Protocol). 14 | 15 | ## Experiment Overview: 16 | In this experiment, we implemented a simple client-server model using TCP sockets. The server listens for incoming connections from clients and responds to their requests. The client establishes a connection with the server, sends a message, and receives a response. 17 | 18 | ## Experiment Procedure: 19 | ### ALGORITHM 20 | 21 | - SERVER 22 | 1. Include necessary header files such as `stdio.h`, `string.h`, `sys/socket.h`, and `arpa/inet.h`. 23 | 24 | 2. Declare variables for socket descriptors, server and client addresses, and messages to be sent and received,and Initialize the server and client messages with null characters using `memset()`. 25 | 26 | 3. Call `socket()` to create a socket for communication, and Check if the socket creation was successful. If not, print an error message and exit. 27 | 28 | 4. **Bind to Port:** 29 | - Set up the server address structure (`server_addr`) with the desired port and IP address (`127.0.0.1` in this case). 30 | - Call `bind()` to bind the socket to the server address. 31 | - Check for errors in binding. If there's an error, print a message and exit. 32 | 33 | 5. Call `listen()` to make the server listen for incoming connections. 34 | 35 | 6. Call `accept()` to accept incoming client connections. If the connection is accepted successfully, print the client's IP address and port number. 36 | 37 | 7. Call `recv()` to receive data from the client. 38 | 39 | 8. Print the message received from the client. 40 | 41 | 9. Prepare a response message for the client. 42 | - Call `send()` to send the response message to the client. 43 | - Check for errors in sending. If there's an error, print a message and exit. 44 | 45 | 10. Close Connections: 46 | 47 | 11. Exit the program with status `0`. 48 | 49 | - CLIENT 50 | 1. Include necessary header files such as `stdio.h`, `string.h`, `sys/socket.h`, and `arpa/inet.h`. 51 | 52 | 2. Declare variables for socket descriptor, server address, and messages to be sent and received.Initialize the server and client messages with null characters using `memset()`. 53 | 54 | 3. Call `socket()` to create a socket for communication.Check if the socket creation was successful. If not, print an error message and exit. 55 | 56 | 4. Set up the server address structure (`server_addr`) with the desired port and IP address (`127.0.0.1` in this case). 57 | 58 | 5. Call `connect()` to establish a connection with the server. 59 | 60 | 6. - Prompt the user to enter a message to send to the server. 61 | - Read the input message using `gets()` (Note: `gets()` is deprecated and unsafe, consider using `fgets()` instead). 62 | - Call `send()` to send the message to the server. 63 | - Check for errors in sending. If there's an error, print a message and exit. 64 | 65 | 7. Call `recv()` to receive a response from the server. 66 | 67 | 8. Print the message received from the server. 68 | 69 | 9. Close the socket using `close()`. 70 | 71 | 10. Exit the program with status `0`. 72 | 73 | 74 | 75 | ## Viva Questions: 76 | 1. What is TCP (Transmission Control Protocol), and how does it differ from UDP (User Datagram Protocol)? 77 | 2. Explain the process of establishing a TCP connection between a client and server. 78 | 3. What is the purpose of the `bind()` and `listen()` functions in server socket programming? 79 | 4. Describe the role of the `accept()` function in server socket programming. 80 | 5. How does the client-side socket differ from the server-side socket in a TCP connection? 81 | 6. What is a socket address? Explain the components of a socket address structure. 82 | 7. What are the advantages of using TCP for communication over the Internet? 83 | 8. Discuss the concept of TCP's flow control and congestion control mechanisms. 84 | 9. How does TCP ensure reliable data transmission? 85 | 10. Explain the steps involved in handling multiple client connections by a single server using TCP. 86 | 11. What are the potential issues that may arise during TCP communication, and how can they be addressed? 87 | 12. Compare and contrast blocking and non-blocking socket operations in TCP programming. 88 | 13. How can you handle timeouts and connection failures in TCP communication? 89 | 14. Discuss the significance of error handling in TCP socket programming. 90 | 15. Explain the role of the `send()` and `recv()` functions in sending and receiving data over TCP sockets. 91 | --------------------------------------------------------------------------------