├── File-Transfer-Protocol ├── demo.txt ├── client.c └── server.c ├── README.md ├── Link-State-Routing ├── sample_graph.png ├── link2.c └── link.c ├── Leaky-Bucket ├── leaky2.c ├── leaky-2-wc.c ├── leaky.c └── leaky-1-wc.c ├── Sliding Window Protocols ├── Stop-N-Wait │ ├── server.c │ └── client.c ├── Selective-Repeat │ ├── client.c │ └── server.c └── Go-Back-N │ ├── client.c │ └── server.c ├── Distance-Vector-Routing └── dvr.c └── Socket Programming ├── TCP ├── client.c └── server.c └── UDP ├── client.c └── server.c /File-Transfer-Protocol/demo.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # S6-Network-Lab 2 | Network Lab Semester 6 3 | -------------------------------------------------------------------------------- /Link-State-Routing/sample_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Althaf-Nazeer/S6-Network-Lab/HEAD/Link-State-Routing/sample_graph.png -------------------------------------------------------------------------------- /Leaky-Bucket/leaky2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int incoming, outgoing, buck_size, n, store = 0; 5 | printf("Enter bucket size, outgoing rate and no of inputs: "); 6 | scanf("%d %d %d", &buck_size, &outgoing, &n); 7 | 8 | while (n != 0) { 9 | printf("Enter the incoming packet size : "); 10 | scanf("%d", &incoming); 11 | printf("Incoming packet size %d\n", incoming); 12 | if (incoming <= (buck_size - store)){ 13 | store += incoming; 14 | printf("Bucket buffer size %d out of %d\n", store, buck_size); 15 | } else { 16 | printf("Dropped %d no of packets\n", incoming - (buck_size - store)); 17 | printf("Bucket buffer size %d out of %d\n", store, buck_size); 18 | store = buck_size; 19 | } 20 | store = store - outgoing; 21 | if(store < 0) 22 | store = 0; 23 | printf("After outgoing %d packets left out of %d in buffer\n", store, buck_size); 24 | n--; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /File-Transfer-Protocol/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define SERV_TCP_PORT 5035 12 | #define MAX 60 13 | int main(int arg,char*argv[]) 14 | { 15 | int sockfd,n; 16 | struct sockaddr_in serv_addr; 17 | struct hostent*server; 18 | char send[MAX],recvline[MAX],s[MAX],name[MAX]; 19 | sockfd=socket(AF_INET,SOCK_STREAM,0); 20 | serv_addr.sin_family=AF_INET; 21 | serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); 22 | serv_addr.sin_port=htons(SERV_TCP_PORT); 23 | connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)); 24 | printf("\nEnter the source file name : \n"); 25 | scanf("%s",send); 26 | write(sockfd,send,MAX); 27 | while((n=read(sockfd,recvline,MAX))!=0) 28 | { 29 | printf("%s",recvline); 30 | } 31 | close(sockfd); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /File-Transfer-Protocol/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #define SERV_TCP_PORT 5035 11 | #define MAX 60 12 | int i, j, tem; 13 | char buff[4096], t; 14 | FILE *f1; 15 | int main(int afg, char *argv) 16 | { 17 | int sockfd, newsockfd, clength; 18 | struct sockaddr_in serv_addr,cli_addr; 19 | char t[MAX], str[MAX]; 20 | strcpy(t,"exit"); 21 | sockfd=socket(AF_INET, SOCK_STREAM,0); 22 | serv_addr.sin_family=AF_INET; 23 | serv_addr.sin_addr.s_addr=INADDR_ANY; 24 | serv_addr.sin_port=htons(SERV_TCP_PORT); 25 | printf("\nBinded"); 26 | bind(sockfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr)); 27 | printf("\nListening..."); 28 | listen(sockfd, 5); 29 | clength=sizeof(cli_addr); 30 | newsockfd=accept(sockfd,(struct sockaddr*) &cli_addr,&clength); 31 | close(sockfd); 32 | read(newsockfd, &str, MAX); 33 | printf("\nClient message\n File Name : %s\n", str); 34 | f1=fopen(str, "r"); 35 | while(fgets(buff, 4096, f1)!=NULL) 36 | { 37 | write(newsockfd, buff,MAX); 38 | printf("\n"); 39 | } 40 | fclose(f1); 41 | printf("\nFile Transferred\n"); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Leaky-Bucket/leaky-2-wc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int incoming, outgoing, buck_size, n, store = 0; 5 | //store is used to store the number of packets that are removed from the buffer 6 | //buck_size is the size of the buffer in bytes 7 | //n is the number of packets that are to be transmitted 8 | //incoming is the size of the incoming packet in bytes 9 | //outgoing is the size of the outgoing packet in bytes 10 | printf("Enter bucket size, outgoing rate and no of inputs: "); 11 | scanf("%d %d %d", &buck_size, &outgoing, &n); 12 | 13 | while (n != 0) { 14 | printf("Enter the incoming packet size : "); 15 | scanf("%d", &incoming); 16 | printf("Incoming packet size %d\n", incoming); 17 | //if the incoming packet size is greater than the bucket size 18 | if (incoming <= (buck_size - store)){ 19 | store += incoming; //add the incoming packet size to the buffer 20 | printf("Bucket buffer size %d out of %d\n", store, buck_size); 21 | } else { 22 | printf("Dropped %d no of packets\n", incoming - (buck_size - store)); 23 | printf("Bucket buffer size %d out of %d\n", store, buck_size); 24 | //if the incoming packet size is greater than the bucket size 25 | //then the packets are dropped 26 | store = buck_size; 27 | } 28 | store = store - outgoing; //subtract the outgoing packet size from the buffer 29 | if(store < 0) 30 | store = 0; //if the buffer size is less than 0 then set it to 0 31 | printf("After outgoing %d packets left out of %d in buffer\n", store, buck_size); 32 | n--; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Sliding Window Protocols/Stop-N-Wait/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | 12 | typedef struct packet{ 13 | char data[1024]; 14 | }Packet; 15 | 16 | typedef struct frame{ 17 | int frame_kind; //ACK:0, SEQ:1 FIN:2 18 | int sq_no; 19 | int ack; 20 | Packet packet; 21 | }Frame; 22 | 23 | int main(int argc, char** argv){ 24 | 25 | if (argc != 2){ 26 | printf("Usage: %s ", argv[0]); 27 | exit(0); 28 | } 29 | 30 | int port = atoi(argv[1]); 31 | int sockfd; 32 | struct sockaddr_in serverAddr, newAddr; 33 | char buffer[1024]; 34 | socklen_t addr_size; 35 | 36 | int frame_id=0; 37 | Frame frame_recv; 38 | Frame frame_send; 39 | 40 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); 41 | 42 | memset(&serverAddr, '\0', sizeof(serverAddr)); 43 | serverAddr.sin_family = AF_INET; 44 | serverAddr.sin_port = htons(port); 45 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 46 | 47 | bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr)); 48 | addr_size = sizeof(newAddr); 49 | 50 | while(1){ 51 | int f_recv_size = recvfrom(sockfd, &frame_recv, sizeof(Frame), 0, (struct sockaddr*)&newAddr, &addr_size); 52 | if (f_recv_size > 0 && frame_recv.frame_kind == 1 && frame_recv.sq_no == frame_id){ 53 | printf("[+]Frame Received: %s\n", frame_recv.packet.data); 54 | 55 | frame_send.sq_no = 0; 56 | frame_send.frame_kind = 0; 57 | frame_send.ack = frame_recv.sq_no + 1; 58 | sendto(sockfd, &frame_send, sizeof(frame_send), 0, (struct sockaddr*)&newAddr, addr_size); 59 | printf("[+]Ack Send\n"); 60 | }else{ 61 | printf("[+]Frame Not Received\n"); 62 | } 63 | frame_id++; 64 | } 65 | 66 | close(sockfd); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Sliding Window Protocols/Stop-N-Wait/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | typedef struct packet{ 9 | char data[1024]; 10 | }Packet; 11 | 12 | typedef struct frame{ 13 | int frame_kind; //ACK:0, SEQ:1 FIN:2 14 | int sq_no; 15 | int ack; 16 | Packet packet; 17 | }Frame; 18 | 19 | int main(int argc, char *argv[]){ 20 | if (argc != 2){ 21 | printf("Usage: %s ", argv[0]); 22 | exit(0); 23 | } 24 | 25 | int port = atoi(argv[1]); 26 | int sockfd; 27 | struct sockaddr_in serverAddr; 28 | char buffer[1024]; 29 | socklen_t addr_size; 30 | 31 | int frame_id = 0; 32 | Frame frame_send; 33 | Frame frame_recv; 34 | int ack_recv = 1; 35 | 36 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); 37 | 38 | memset(&serverAddr, '\0', sizeof(serverAddr)); 39 | serverAddr.sin_family = AF_INET; 40 | serverAddr.sin_port = htons(port); 41 | serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 42 | 43 | while(1){ 44 | 45 | if(ack_recv == 1){ 46 | frame_send.sq_no = frame_id; 47 | frame_send.frame_kind = 1; 48 | frame_send.ack = 0; 49 | 50 | printf("Enter Data: "); 51 | scanf("%s", buffer); 52 | strcpy(frame_send.packet.data, buffer); 53 | sendto(sockfd, &frame_send, sizeof(Frame), 0, (struct sockaddr*)&serverAddr, sizeof(serverAddr)); 54 | printf("[+]Frame Send\n"); 55 | } 56 | int addr_size = sizeof(serverAddr); 57 | int f_recv_size = recvfrom(sockfd, &frame_recv, sizeof(frame_recv), 0 ,(struct sockaddr*)&serverAddr, &addr_size); 58 | 59 | if( f_recv_size > 0 && frame_recv.sq_no == 0 && frame_recv.ack == frame_id+1){ 60 | printf("[+]Ack Received\n"); 61 | ack_recv = 1; 62 | }else{ 63 | printf("[-]Ack Not Received\n"); 64 | ack_recv = 0; 65 | } 66 | 67 | frame_id++; 68 | } 69 | 70 | close(sockfd); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Link-State-Routing/link2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int count, src_router, i, j, k, w, v, min; 6 | int cost_matrix[100][100], dist[100], last[100]; 7 | int flag[100]; 8 | 9 | printf("\n Enter the no of routers"); 10 | scanf("%d", &count); 11 | printf("\n Enter the cost matrix values:"); 12 | 13 | for (i = 0; i < count; i++) 14 | { 15 | for (j = 0; j < count; j++) 16 | { 17 | printf("\n%d->%d:", i, j); 18 | scanf("%d", &cost_matrix[i][j]); 19 | if (cost_matrix[i][j] < 0) 20 | cost_matrix[i][j] = 1000; 21 | } 22 | } 23 | 24 | printf("\n Enter the source router:"); 25 | scanf("%d", &src_router); 26 | 27 | for (v = 0; v < count; v++) 28 | { 29 | flag[v] = 0; 30 | last[v] = src_router; 31 | dist[v] = cost_matrix[src_router][v]; 32 | } 33 | flag[src_router] = 1; 34 | 35 | for (i = 0; i < count; i++) 36 | { 37 | min = 1000; 38 | for (w = 0; w < count; w++) 39 | { 40 | if (!flag[w]) 41 | if (dist[w] < min) 42 | { 43 | v = w; 44 | min = dist[w]; 45 | } 46 | } 47 | flag[v] = 1; 48 | for (w = 0; w < count; w++) 49 | { 50 | if (!flag[w]) 51 | if (min + cost_matrix[v][w] < dist[w]) 52 | { 53 | dist[w] = min + cost_matrix[v][w]; 54 | last[w] = v; 55 | } 56 | } 57 | } 58 | 59 | for (i = 0; i < count; i++) 60 | { 61 | printf("\n%d==>%d:Path taken:%d", src_router, i, i); 62 | w = i; 63 | while (w != src_router) 64 | { 65 | 66 | printf("<--%d", last[w]); 67 | w = last[w]; 68 | } 69 | printf("\n Shortest path cost:%d", dist[i]); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Sliding Window Protocols/Selective-Repeat/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | int isfaulty(){ //simulating corruption of message 14 | 15 | int d=rand()%4; 16 | //printf("%d\n",d); 17 | return (d>2); 18 | 19 | } 20 | int main() { 21 | srand(time(0)); 22 | int c_sock; 23 | c_sock = socket(AF_INET, SOCK_STREAM, 0); 24 | struct sockaddr_in client; 25 | memset(&client, 0, sizeof(client)); 26 | client.sin_family = AF_INET; 27 | client.sin_port = htons(9009); 28 | client.sin_addr.s_addr = inet_addr("127.0.0.1"); 29 | 30 | 31 | if(connect(c_sock, (struct sockaddr*)&client, sizeof(client)) == -1) { 32 | printf("Connection failed"); 33 | return 0; 34 | } 35 | printf("\n\tClient -with individual acknowledgement scheme\n\n"); 36 | char msg1[50]="akwnowledgementof-"; 37 | char msg3[50]="negative akwn-"; 38 | char msg2[50]; 39 | char buff[100]; 40 | int count=-1,flag=1; 41 | while(count<8){ 42 | bzero(buff,sizeof(buff)); 43 | bzero(msg2,sizeof(msg2)); 44 | if(count==7&&flag==1){ 45 | //sleep(3); 46 | printf("here\n"); //simulate loss 47 | //i--; 48 | flag=0; 49 | read(c_sock,buff,sizeof(buff)); 50 | //printf("aa %s \n",buff); 51 | continue; 52 | } 53 | int n = read(c_sock, buff, sizeof(buff)); 54 | char i=buff[strlen(buff)-1]; 55 | printf("Message received from server : %s \n",buff); 56 | int isfault=isfaulty(); 57 | printf("correption status : %d \n",isfault); 58 | printf("Response/akwn sent for message \n"); 59 | if(isfault) 60 | strcpy(msg2,msg3); 61 | else{ 62 | strcpy(msg2,msg1); 63 | count++;} 64 | msg2[strlen(msg2)]=i; 65 | write(c_sock,msg2, sizeof(msg2)); 66 | } 67 | 68 | close(c_sock); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Distance-Vector-Routing/dvr.c: -------------------------------------------------------------------------------- 1 | #include 2 | int costMatrix[20][20], n; 3 | 4 | struct routers 5 | { 6 | int distance[20]; 7 | int adjNodes[20]; 8 | } node[20]; 9 | 10 | // function to read the cost matrix 11 | void readCostMatrix() 12 | { 13 | int i, j; 14 | printf("\nEnter cost matrix\n"); 15 | for (i = 0; i < n; ++i) 16 | { 17 | for (j = 0; j < n; ++j) 18 | { 19 | scanf("%d", &costMatrix[i][j]); 20 | // distance from X to X is 0 21 | costMatrix[i][i] = 0; 22 | node[i].distance[j] = costMatrix[i][j]; 23 | node[i].adjNodes[j] = j; 24 | } 25 | } 26 | } 27 | 28 | void calcRoutingTable() 29 | { 30 | int i, j, k; 31 | for (i = 0; i < n; ++i) 32 | { 33 | for (j = 0; j < n; ++j) 34 | { 35 | for (k = 0; k < n; ++k) 36 | { 37 | // if the cost of the path from X to Y is less than the cost of the path from X to Z 38 | if (node[i].distance[j] > costMatrix[i][k] + node[k].distance[j]) 39 | { 40 | // substitute with minimum distance 41 | node[i].distance[j] = node[i].distance[k] + node[k].distance[j]; 42 | // substitute with minimum path 43 | node[i].adjNodes[j] = k; 44 | } 45 | } 46 | } 47 | } 48 | } 49 | 50 | void displayRoutes() 51 | { 52 | int i, j; 53 | for (i = 0; i < n; ++i) 54 | { 55 | printf("\nRouter %d\n", i + 1); 56 | for (j = 0; j < n; ++j) 57 | { 58 | printf("Node %d via %d : Distance %d\n", j + 1, node[i].adjNodes[j] + 1, node[i].distance[j]); 59 | } 60 | printf("\n"); 61 | } 62 | } 63 | 64 | int main() 65 | { 66 | int i, j; 67 | printf("Number of nodes: "); 68 | scanf("%d", &n); 69 | readCostMatrix(); 70 | calcRoutingTable(); 71 | displayRoutes(); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Leaky-Bucket/leaky.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define NOF_PACKETS 10 6 | 7 | int randm(int a) 8 | { 9 | int rn = (random() % 10) % a; 10 | return rn == 0 ? 1 : rn; 11 | } 12 | 13 | int main() 14 | { 15 | int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm = 0, p_sz, p_time, op; 16 | 17 | for (i = 0; i < NOF_PACKETS; ++i) 18 | packet_sz[i] = randm(6) * 10; 19 | 20 | for (i = 0; i < NOF_PACKETS; ++i) 21 | printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]); 22 | 23 | 24 | printf("\nEnter the Output rate:"); 25 | scanf("%d", &o_rate); 26 | printf("Enter the Bucket Size:"); 27 | scanf("%d", &b_size); 28 | 29 | for (i = 0; i < NOF_PACKETS; ++i) 30 | { 31 | if ((packet_sz[i] + p_sz_rm) > b_size) 32 | if (packet_sz[i] > b_size) /*compare the packet siz with bucket size*/ 33 | printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-PACKET REJECTED", packet_sz[i], b_size); 34 | else 35 | printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!"); 36 | else 37 | { 38 | p_sz_rm += packet_sz[i]; 39 | printf("\n\nIncoming Packet size: %d", packet_sz[i]); 40 | printf("\nBytes remaining to Transmit: %d", p_sz_rm); 41 | p_time = randm(4) * 10; 42 | printf("\nTime left for transmission: %d units", p_time); 43 | for (clk = 10; clk <= p_time; clk += 10) 44 | { 45 | sleep(1); 46 | if (p_sz_rm) 47 | { 48 | if (p_sz_rm <= o_rate) /*packet size remaining comparing with output rate*/ 49 | op = p_sz_rm, p_sz_rm = 0; 50 | else 51 | op = o_rate, p_sz_rm -= o_rate; 52 | printf("\nPacket of size %d Transmitted", op); 53 | printf("----Bytes Remaining to Transmit: %d", p_sz_rm); 54 | } 55 | else 56 | { 57 | printf("\nTime left for transmission: %d units", p_time - clk); 58 | printf("\nNo packets to transmit!!"); 59 | } 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Socket Programming/TCP/client.c: -------------------------------------------------------------------------------- 1 | /* 2 | First run server.c by gcc server.c -o server 3 | then run server by ./server 4 | After that start client by gcc client.c -o client 5 | then run client by ./client 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | int main() 14 | { 15 | int clientsocket,port; //clientsocket is the socket descriptor , port is the port number 16 | struct sockaddr_in serveraddr; //creating a structure of type sockaddr_in for server 17 | socklen_t len; //creating a variable to store the length of the server address 18 | char message[50]; 19 | 20 | 21 | clientsocket=socket(AF_INET,SOCK_STREAM,0);//creating a socket 22 | bzero((char*)&serveraddr,sizeof(serveraddr));//initializing the server address to zero 23 | len=sizeof(serveraddr); //storing the length of the server address in len 24 | serveraddr.sin_family=AF_INET; //setting the family of the server address to AF_INET 25 | 26 | printf("Enter the port number "); 27 | scanf("%d",&port); 28 | serveraddr.sin_port=htons(port);//setting the port number of the server address to port 29 | printf("\nTrying to connect to the server.\n"); 30 | 31 | 32 | connect(clientsocket,(struct sockaddr*)&serveraddr,sizeof(serveraddr));//connecting to the server 33 | printf("\nConnected to the server.\n"); 34 | printf("\nSending message for server connection"); 35 | 36 | send(clientsocket,"HI,IAM CLIENT...",sizeof("HI,IAM CLIENT..."),0);//sending the message to the server 37 | printf("\nReceiving message from server.\n"); 38 | recv(clientsocket,message,sizeof(message),0);//receiving the message from the server 39 | printf("\nMessage received.\t%s\n",message); 40 | close(clientsocket);//closing the socket 41 | } 42 | 43 | /* 44 | 45 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ gcc client.c -o client 46 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ ./client 47 | Enter the port number 6000 48 | 49 | Trying to connect to the server. 50 | 51 | Connected to the server. 52 | 53 | Sending message for server connection 54 | Receiving message from server. 55 | 56 | Message received. YOUR MESSAGE RECEIVED. 57 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ 58 | 59 | */ 60 | -------------------------------------------------------------------------------- /Sliding Window Protocols/Go-Back-N/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | int main() 12 | { 13 | int c_sock; 14 | c_sock = socket(AF_INET, SOCK_STREAM, 0); 15 | struct sockaddr_in client; 16 | memset(&client, 0, sizeof(client)); 17 | client.sin_family = AF_INET; 18 | client.sin_port = htons(9009); 19 | client.sin_addr.s_addr = inet_addr("127.0.0.1"); 20 | 21 | if (connect(c_sock, (struct sockaddr *)&client, sizeof(client)) == -1) 22 | { 23 | printf("Connection failed"); 24 | return 0; 25 | } 26 | printf("\n\tClient -with individual acknowledgement scheme\n\n"); 27 | char msg1[50] = "akwnowledgementof-"; 28 | char msg2[50]; 29 | // char msg3[50]="Final cumulative aknowledgement"; 30 | char buff[100]; 31 | int flag = 1, flg = 1; 32 | for (int i = 0; i <= 9; i++) 33 | { 34 | flg = 1; 35 | bzero(buff, sizeof(buff)); 36 | bzero(msg2, sizeof(msg2)); 37 | if (i == 8 && flag == 1) 38 | { 39 | printf("here\n"); // simulating loss 40 | // i--; 41 | flag = 0; 42 | read(c_sock, buff, sizeof(buff)); 43 | } 44 | int n = read(c_sock, buff, sizeof(buff)); 45 | if (buff[strlen(buff) - 1] != i + '0') 46 | { // outoforder 47 | printf("Discarded as out of order \n"); 48 | // printf("%c, %c, -- %s\n",buff[strlen(buff)-1],'0'+i,buff); 49 | // flg=0; 50 | i--; 51 | } 52 | else 53 | { 54 | printf("Message received from server : %s \n", buff); 55 | printf("Aknowledgement sent for message \n"); 56 | strcpy(msg2, msg1); 57 | msg2[strlen(msg2)] = i + '0'; 58 | // if(i==8) 59 | // continue; //we will send cumulative for last 2 60 | // if(i==9){ 61 | // write(c_sock,msg3,sizeof(msg3)); 62 | // printf("- %s",msg3);} 63 | // else 64 | write(c_sock, msg2, sizeof(msg2)); 65 | // printf("- %s",msg2); 66 | } 67 | } 68 | 69 | close(c_sock); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Socket Programming/UDP/client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | int clientsocket,port; //clientsocket is the socket descriptor , port is the port number 11 | struct sockaddr_in serveraddr; //creating a structure of type sockaddr_in for server 12 | socklen_t len; //creating a variable to store the length of the server address 13 | struct hostent *server; //creating a structure of type hostent for server 14 | char message[50]; //creating a char array to store the message 15 | 16 | clientsocket=socket(AF_INET,SOCK_DGRAM,0);//creating a socket 17 | //steps involved in the server address creation. 18 | bzero((char*)&serveraddr,sizeof(serveraddr));//initializing the server address to zero 19 | len=sizeof(serveraddr);//storing the length of the server address in len 20 | serveraddr.sin_family=AF_INET;//setting the family of the server address to AF_INET 21 | 22 | printf("Enter the port number "); 23 | scanf("%d",&port); 24 | serveraddr.sin_port=htons(port);//setting the port number of the server address to port , htons is used to convert the port number to network byte order 25 | fgets(message,2,stdin);//fgets is used to read the message from the user and storing it in message 2 is the size of the message 26 | printf("\nSending message for server connection\n"); 27 | //sending message. 28 | sendto(clientsocket,"HI I AM CLIENT...",sizeof("HI I AM CLIENT...."),0,(struct sockaddr*)&serveraddr,sizeof(serveraddr)); //sendto is used to send the message to the server 29 | printf("\nReceiving message from server.\n"); 30 | //receiving messages. 31 | recvfrom(clientsocket,message,sizeof(message),0,(struct sockaddr*)&serveraddr,&len);//recvfrom is used to receive the message from the server 32 | printf("\nMessage received:\t%s\n",message); 33 | close(clientsocket);//closing the socket 34 | } 35 | 36 | /* 37 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/UDP$ gcc client.c -o client 38 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/UDP$ ./client 39 | Enter the port number 6000 40 | 41 | Sending message for server connection 42 | 43 | Receiving message from server. 44 | 45 | Message received: YOUR MESSAGE RECEIVED. 46 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/UDP$ 47 | */ 48 | -------------------------------------------------------------------------------- /Socket Programming/UDP/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | int serversocket,port; //serversocket is the socket descriptor , port is the port number 11 | struct sockaddr_in serveraddr,clientaddr; //creating a structure of type sockaddr_in for server and client 12 | socklen_t len; //creating a variable to store the length of the server address 13 | char message[50];//creating a char array to store the message 14 | //socket creation. 15 | serversocket=socket(AF_INET,SOCK_DGRAM,0);//creating a socket 16 | //steps involved in defining the serveraddress. 17 | bzero((char*)&serveraddr,sizeof(serveraddr)); //initializing the server address to zero 18 | serveraddr.sin_family=AF_INET;//setting the family of the server address to AF_INET 19 | 20 | printf("Enter the port number "); 21 | scanf("%d",&port); 22 | serveraddr.sin_port=htons(port);//setting the port number of the server address to port , htons is used to convert the port number to network byte order 23 | serveraddr.sin_addr.s_addr=INADDR_ANY; //setting the address of the server address to INADDR_ANY , INADDR_ANY is used to bind the socket to all the interfaces of the machine 24 | //binding the socket to the operating system. 25 | bind(serversocket,(struct sockaddr*)&serveraddr,sizeof(serveraddr));//bind is used to bind the socket to the operating system 26 | printf("\nWaiting for the client connection\n"); 27 | bzero((char*)&clientaddr,sizeof(clientaddr));//initializing the client address to zero 28 | len=sizeof(clientaddr);//storing the length of the client address in len 29 | 30 | //receiving message from the client. 31 | recvfrom(serversocket,message,sizeof(message),0,(struct sockaddr*)&clientaddr,&len);//recvfrom is used to receive the message from the client 32 | printf("\nConnection received from client.\n"); 33 | printf("\nThe client has send:\t%s\n",message); 34 | printf("\nSending message to the client.\n"); 35 | //sending message to the client. 36 | sendto(serversocket,"YOUR MESSAGE RECEIVED.",sizeof("YOUR MESSAGERECEIVED."),0,( struct sockaddr*)&clientaddr,sizeof(clientaddr));//sendto is used to send the message to the client 37 | close(serversocket); 38 | } 39 | 40 | 41 | 42 | /* 43 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/UDP$ gcc server.c -o server 44 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/UDP$ ./server 45 | Enter the port number 6000 46 | 47 | Waiting for the client connection 48 | 49 | Connection received from client. 50 | 51 | The client has send: HI I AM CLIENT... 52 | 53 | Sending message to the client. 54 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/UDP$ 55 | */ 56 | -------------------------------------------------------------------------------- /Socket Programming/TCP/server.c: -------------------------------------------------------------------------------- 1 | /* 2 | First run server.c by gcc server.c -o server 3 | then run server by ./server 4 | After that start client by gcc client.c -o client 5 | then run client by ./client 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int main() 15 | { 16 | int serversocket,clientsocket,port; //clientsocket is the socket descriptor , port is the port number 17 | struct sockaddr_in serveraddr,clientaddr; //creating a structure of type sockaddr_in for server 18 | socklen_t len; //creating a variable to store the length of the server address 19 | char message[50]; // 20 | struct serveraddr; //creating a structure of type sockaddr_in for server 21 | 22 | 23 | serversocket=socket(AF_INET,SOCK_STREAM,0); //creating a socket 24 | bzero((char*)&serveraddr,sizeof(serveraddr));//initializing the server address to zero 25 | serveraddr.sin_family=AF_INET;//setting the family of the server address to AF_INET 26 | 27 | printf("Enter the port number "); 28 | scanf("%d",&port); 29 | 30 | serveraddr.sin_port=htons(port); //setting the port number of the server address to port 31 | serveraddr.sin_addr.s_addr=INADDR_ANY; //setting the IP address of the server address to INADDR_ANY 32 | bind(serversocket,(struct sockaddr*)&serveraddr,sizeof(serveraddr)); //binding the server address to the socket 33 | bzero((char*)&clientaddr,sizeof(clientaddr)); //initializing the client address to zero 34 | len=sizeof(clientaddr); //storing the length of the client address in len 35 | listen(serversocket,5); //listening to the socket, 5 is the number of clients that can connect to the server 36 | 37 | printf("\nWaiting for client connection\n"); 38 | printf("\nhai:"); 39 | clientsocket=accept(serversocket,(struct sockaddr*)&clientaddr,&len);//accepting the client connection 40 | 41 | printf("\nClient connectivity received.\n"); 42 | printf("\nReading message from the client.\n"); 43 | read(clientsocket,message,sizeof(message));//reading the message from the client 44 | 45 | printf("\nThe client has sent.%s",message); 46 | printf("\nSending message to the client.\n"); 47 | write(clientsocket,"YOUR MESSAGE RECEIVED.",sizeof("YOUR MESSAGE RECEIVED."));//sending the message to the client 48 | 49 | close(clientsocket);//closing the client socket 50 | close(serversocket);//closing the server socket 51 | 52 | 53 | } 54 | 55 | /* 56 | OUTPUT 57 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ gcc server.c -o server 58 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ ./server 59 | Enter the port number 6000 60 | 61 | Waiting for client connection 62 | 63 | hai: 64 | Client connectivity received. 65 | 66 | Reading message from the client. 67 | 68 | The client has sent.HI,IAM CLIENT... 69 | Sending message to the client. 70 | s6d2@user-HP-280-G3-MT:~/Networking-Lab-S6/Socket-Programming/TCP$ ^C 71 | */ 72 | -------------------------------------------------------------------------------- /Sliding Window Protocols/Selective-Repeat/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | void rsendd(int ch, int c_sock) 13 | { 14 | char buff2[60]; 15 | bzero(buff2, sizeof(buff2)); 16 | strcpy(buff2, "reserver message :"); 17 | buff2[strlen(buff2)] = (ch) + '0'; 18 | buff2[strlen(buff2)] = '\0'; 19 | printf("Resending Message to client :%s \n", buff2); 20 | write(c_sock, buff2, sizeof(buff2)); 21 | usleep(1000); 22 | } 23 | int main() 24 | { 25 | int s_sock, c_sock; 26 | s_sock = socket(AF_INET, SOCK_STREAM, 0); 27 | struct sockaddr_in server, other; 28 | memset(&server, 0, sizeof(server)); 29 | memset(&other, 0, sizeof(other)); 30 | server.sin_family = AF_INET; 31 | server.sin_port = htons(9009); 32 | server.sin_addr.s_addr = INADDR_ANY; 33 | socklen_t add; 34 | 35 | if (bind(s_sock, (struct sockaddr *)&server, sizeof(server)) == -1) 36 | { 37 | printf("Binding failed\n"); 38 | return 0; 39 | } 40 | printf("\tServer Up\n Selective repeat scheme\n\n"); 41 | listen(s_sock, 10); 42 | add = sizeof(other); 43 | c_sock = accept(s_sock, (struct sockaddr *)&other, &add); 44 | time_t t1, t2; 45 | char msg[50] = "server message :"; 46 | char buff[50]; 47 | int flag = 0; 48 | 49 | fd_set set1, set2, set3; 50 | struct timeval timeout1, timeout2, timeout3; 51 | int rv1, rv2, rv3; 52 | 53 | int tot = 0; 54 | int ok[20]; 55 | memset(ok, 0, sizeof(ok)); 56 | 57 | while (tot < 9) 58 | { 59 | int toti = tot; 60 | for (int j = (0 + toti); j < (3 + toti); j++) 61 | { 62 | // printf("%d %d %d \n",tot,toti,j); 63 | bzero(buff, sizeof(buff)); 64 | char buff2[60]; 65 | bzero(buff2, sizeof(buff2)); 66 | strcpy(buff2, "server message :"); 67 | buff2[strlen(buff2)] = (j) + '0'; 68 | buff2[strlen(buff2)] = '\0'; 69 | printf("Message sent to client :%s \n", buff2); 70 | write(c_sock, buff2, sizeof(buff2)); 71 | usleep(1000); 72 | } 73 | for (int k = 0 + toti; k < (toti + 3); k++) 74 | { 75 | qq: 76 | FD_ZERO(&set1); 77 | FD_SET(c_sock, &set1); 78 | timeout1.tv_sec = 2; 79 | timeout1.tv_usec = 0; 80 | 81 | rv1 = select(c_sock + 1, &set1, NULL, NULL, &timeout1); 82 | if (rv1 == -1) 83 | perror("select error "); 84 | else if (rv1 == 0) 85 | { 86 | printf("Timeout for message :%d \n", k); 87 | rsendd(k, c_sock); 88 | goto qq; 89 | } // a timeout occured 90 | else 91 | { 92 | read(c_sock, buff, sizeof(buff)); 93 | printf("Message from Client: %s\n", buff); 94 | if (buff[0] == 'n') 95 | { 96 | printf(" corrupt message awk (msg %d) \n", buff[strlen(buff) - 1] - '0'); 97 | rsendd((buff[strlen(buff) - 1] - '0'), c_sock); 98 | goto qq; 99 | } 100 | else 101 | tot++; 102 | // printf("%d %d %d \n",tot,toti,k); 103 | } 104 | } 105 | } 106 | 107 | close(c_sock); 108 | close(s_sock); 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /Leaky-Bucket/leaky-1-wc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define NOF_PACKETS 10 6 | 7 | // function to generate random number between 0 and 100 8 | int randm(int a) 9 | { 10 | int rn = (random() % 10) % a; 11 | return rn == 0 ? 1 : rn; 12 | } 13 | 14 | int main() 15 | { 16 | int packet_sz[NOF_PACKETS], i, clk, b_size, o_rate, p_sz_rm = 0, p_sz, p_time, op; 17 | // b_size = bandwidth in bytes/sec 18 | // o_rate = output rate in packets/sec 19 | // p_sz = packet size in bytes 20 | // p_time = packet time in sec 21 | // op = operation to be performed on the packet 22 | // clk = clock in sec 23 | // p_sz_rm = packet size in bytes removed from the buffer 24 | // p_sz = packet size in bytes added to the buffer 25 | 26 | for (i = 0; i < NOF_PACKETS; ++i) 27 | packet_sz[i] = randm(6) * 10; // generate random packet size between 0 and 60 bytes 28 | 29 | for (i = 0; i < NOF_PACKETS; ++i) 30 | printf("\npacket[%d]:%d bytes\t", i, packet_sz[i]); // print the packet size 31 | 32 | 33 | printf("\nEnter the Output rate:"); 34 | scanf("%d", &o_rate); 35 | printf("Enter the Bucket Size:"); 36 | scanf("%d", &b_size); 37 | 38 | for (i = 0; i < NOF_PACKETS; ++i) 39 | { 40 | // calculate the packet time 41 | if ((packet_sz[i] + p_sz_rm) > b_size) 42 | //if packet size is greater than the bucket size 43 | if (packet_sz[i] > b_size) /*compare the packet siz with bucket size*/ 44 | printf("\n\nIncoming packet size (%dbytes) is Greater than bucket capacity (%dbytes)-PACKET REJECTED", packet_sz[i], b_size); 45 | else 46 | printf("\n\nBucket capacity exceeded-PACKETS REJECTED!!"); 47 | else 48 | { 49 | p_sz_rm += packet_sz[i]; 50 | printf("\n\nIncoming Packet size: %d", packet_sz[i]); 51 | printf("\nBytes remaining to Transmit: %d", p_sz_rm); 52 | p_time = randm(4) * 10; // generate random packet time between 0 and 40 secs 53 | printf("\nTime left for transmission: %d units", p_time); 54 | for (clk = 10; clk <= p_time; clk += 10) 55 | { 56 | sleep(1); 57 | if (p_sz_rm) 58 | { 59 | // if the packet size is greater than the bucket size 60 | if (p_sz_rm <= o_rate) /*packet size remaining comparing with output rate*/ 61 | op = p_sz_rm, p_sz_rm = 0; // if the packet size is less than the output rate, then remove the packet size from the buffer 62 | else 63 | op = o_rate, p_sz_rm -= o_rate; // else remove the output rate from the buffer 64 | printf("\nPacket of size %d Transmitted", op); 65 | printf("----Bytes Remaining to Transmit: %d", p_sz_rm); 66 | } 67 | else 68 | { 69 | printf("\nTime left for transmission: %d units", p_time - clk); 70 | printf("\nNo packets to transmit!!"); 71 | } 72 | } 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Sliding Window Protocols/Go-Back-N/server.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | int main() 12 | { 13 | int s_sock, c_sock; 14 | s_sock = socket(AF_INET, SOCK_STREAM, 0); 15 | struct sockaddr_in server, other; 16 | memset(&server, 0, sizeof(server)); 17 | memset(&other, 0, sizeof(other)); 18 | server.sin_family = AF_INET; 19 | server.sin_port = htons(9009); 20 | server.sin_addr.s_addr = INADDR_ANY; 21 | socklen_t add; 22 | 23 | if (bind(s_sock, (struct sockaddr *)&server, sizeof(server)) == -1) 24 | { 25 | printf("Binding failed\n"); 26 | return 0; 27 | } 28 | printf("\tServer Up\n Go back n (n=3) used to send 10 messages \n\n"); 29 | listen(s_sock, 10); 30 | add = sizeof(other); 31 | c_sock = accept(s_sock, (struct sockaddr *)&other, &add); 32 | time_t t1, t2; 33 | char msg[50] = "server message :"; 34 | char buff[50]; 35 | int flag = 0; 36 | 37 | fd_set set1, set2, set3; 38 | struct timeval timeout1, timeout2, timeout3; 39 | int rv1, rv2, rv3; 40 | 41 | int i = -1; 42 | qq: 43 | i = i + 1; 44 | bzero(buff, sizeof(buff)); 45 | char buff2[60]; 46 | bzero(buff2, sizeof(buff2)); 47 | strcpy(buff2, "server message :"); 48 | buff2[strlen(buff2)] = i + '0'; 49 | buff2[strlen(buff2)] = '\0'; 50 | printf("Message sent to client :%s \n", buff2); 51 | write(c_sock, buff2, sizeof(buff2)); 52 | usleep(1000); 53 | i = i + 1; 54 | bzero(buff2, sizeof(buff2)); 55 | strcpy(buff2, msg); 56 | buff2[strlen(msg)] = i + '0'; 57 | printf("Message sent to client :%s \n", buff2); 58 | write(c_sock, buff2, sizeof(buff2)); 59 | i = i + 1; 60 | usleep(1000); 61 | qqq: 62 | bzero(buff2, sizeof(buff2)); 63 | strcpy(buff2, msg); 64 | buff2[strlen(msg)] = i + '0'; 65 | printf("Message sent to client :%s \n", buff2); 66 | write(c_sock, buff2, sizeof(buff2)); 67 | FD_ZERO(&set1); 68 | FD_SET(c_sock, &set1); 69 | timeout1.tv_sec = 2; 70 | timeout1.tv_usec = 0; 71 | 72 | rv1 = select(c_sock + 1, &set1, NULL, NULL, &timeout1); 73 | if (rv1 == -1) 74 | perror("select error "); 75 | else if (rv1 == 0) 76 | { 77 | printf("Going back from %d:timeout \n", i); 78 | i = i - 3; 79 | goto qq; 80 | } 81 | else 82 | { 83 | read(c_sock, buff, sizeof(buff)); 84 | printf("Message from Client: %s\n", buff); 85 | i++; 86 | if (i <= 9) 87 | goto qqq; 88 | } 89 | qq2: 90 | FD_ZERO(&set2); 91 | FD_SET(c_sock, &set2); 92 | timeout2.tv_sec = 3; 93 | timeout2.tv_usec = 0; 94 | rv2 = select(c_sock + 1, &set2, NULL, NULL, &timeout2); 95 | if (rv2 == -1) 96 | perror("select error "); // an error accured 97 | else if (rv2 == 0) 98 | { 99 | printf("Going back from %d:timeout on last 2\n", i - 1); 100 | i = i - 2; 101 | bzero(buff2, sizeof(buff2)); 102 | strcpy(buff2, msg); 103 | buff2[strlen(buff2)] = i + '0'; 104 | write(c_sock, buff2, sizeof(buff2)); 105 | usleep(1000); 106 | bzero(buff2, sizeof(buff2)); 107 | i++; 108 | strcpy(buff2, msg); 109 | buff2[strlen(buff2)] = i + '0'; 110 | write(c_sock, buff2, sizeof(buff2)); 111 | goto qq2; 112 | } // a timeout occured 113 | else 114 | { 115 | read(c_sock, buff, sizeof(buff)); 116 | printf("Message from Client: %s\n", buff); 117 | bzero(buff, sizeof(buff)); 118 | read(c_sock, buff, sizeof(buff)); 119 | printf("Message from Client: %s\n", buff); 120 | } 121 | 122 | //} 123 | 124 | close(c_sock); 125 | close(s_sock); 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /Link-State-Routing/link.c: -------------------------------------------------------------------------------- 1 | #include 2 | int cost[10][10]; 3 | int dist[10]; 4 | int arr[20]; 5 | void djikstra(int); 6 | int search(int); 7 | int length_of(int *); 8 | void print_route(int, int); 9 | int prev[20]; 10 | int order_arr[20]; 11 | int src; 12 | int main() 13 | { 14 | int num_nodes, i, j, d; 15 | printf("Enter number of nodes:"); 16 | scanf("%d", &num_nodes); 17 | printf("Enter the source node:"); 18 | scanf("%d", &src); 19 | printf("Enter the cost matrix, For infinity enter 999\n"); 20 | for (i = 0; i < num_nodes; i++) 21 | for (j = 0; j < num_nodes; j++) 22 | scanf("%d", &cost[i][j]); 23 | djikstra(num_nodes); 24 | 25 | return 0; 26 | } 27 | void djikstra(int _num_nodes) 28 | { 29 | int i, min_val, l; 30 | int k = 0; 31 | int m = 0; 32 | int min = 999; 33 | int last; 34 | int neighbour[20]; 35 | 36 | for (i = 0; i < 20; i++) 37 | { 38 | arr[i] = -1; 39 | neighbour[i] = -1; 40 | order_arr[i] = -1; 41 | prev[i] = -1; 42 | } 43 | arr[0] = src; 44 | last = 0; 45 | for (i = 0; i < _num_nodes; i++) 46 | { 47 | if (i != src) 48 | { 49 | if (cost[src][i] < 999) 50 | { 51 | dist[i] = cost[src][i]; 52 | prev[i] = src; 53 | } 54 | else 55 | dist[i] = 999; 56 | } 57 | else 58 | { 59 | dist[i] = 0; 60 | } 61 | } 62 | do 63 | { 64 | for (i = 0; i < _num_nodes; i++) 65 | { 66 | if (search(i) == 0) 67 | { 68 | if (dist[i] < min) 69 | { 70 | min = dist[i]; 71 | min_val = i; 72 | } 73 | } 74 | } 75 | last++; 76 | arr[last] = min_val; 77 | for (i = 0; i < _num_nodes; i++) 78 | { 79 | if (search(i) == 0) 80 | { 81 | if (cost[min_val][i] < 999) 82 | { 83 | neighbour[m] = i; 84 | m++; 85 | } 86 | } 87 | } 88 | m = 0; 89 | while (neighbour[m] != -1) 90 | { 91 | if (dist[min_val] + cost[min_val][neighbour[m]] < dist[neighbour[m]]) 92 | { 93 | dist[neighbour[m]] = dist[min_val] + cost[min_val][neighbour[m]]; 94 | prev[neighbour[m]] = min_val; 95 | } 96 | m++; 97 | } 98 | m = 0; 99 | for (i = 0; i < _num_nodes; i++) 100 | neighbour[i] = -1; 101 | min = 999; 102 | min_val = -1; 103 | } while (length_of(arr) != _num_nodes); 104 | i = 1; 105 | l = 1; 106 | while (i < _num_nodes) 107 | { 108 | print_route(i, l); 109 | printf("[ distance = %d]", dist[i]); 110 | printf("\n"); 111 | i++; 112 | l++; 113 | for (k = 0; k < 20; k++) 114 | order_arr[k] = -1; 115 | } 116 | } 117 | 118 | void print_route(int _i, int _l) 119 | { 120 | int begin; 121 | int *ptr; 122 | int h, len, temp; 123 | static int inc[20]; 124 | if (_i == src) 125 | { 126 | ptr = order_arr; 127 | while (*ptr != -1) 128 | ptr++; 129 | len = ptr - order_arr; 130 | for (h = 0; h < len / 2; h++) 131 | { 132 | temp = order_arr[h]; 133 | order_arr[h] = order_arr[len - h - 1]; 134 | order_arr[len - h - 1] = temp; 135 | } 136 | ptr = order_arr; 137 | printf("%d", src); 138 | while (*ptr != -1) 139 | { 140 | printf("->%d ", *ptr); 141 | ptr++; 142 | } 143 | return; 144 | } 145 | else 146 | { 147 | order_arr[inc[_l]] = _i; 148 | inc[_l]++; 149 | print_route(prev[_i], _l); 150 | } 151 | } 152 | 153 | 154 | int search(int _i) 155 | { 156 | int i = 0; 157 | while (arr[i] != -1) 158 | { 159 | if (_i == arr[i]) 160 | break; 161 | else 162 | i++; 163 | } 164 | if (arr[i] == -1) 165 | return 0; 166 | else 167 | return 1; 168 | } 169 | 170 | 171 | int length_of(int _arr[]) 172 | { 173 | int i = 0; 174 | while (_arr[i] != -1) 175 | i++; 176 | return i; 177 | } 178 | 179 | 180 | 181 | // Sample Output 182 | // aromal@NitroAN51555:~/cnlab/Networking-Lab-S6/Link-State-Routing$ ./a.out 183 | // Enter number of nodes:4 184 | // Enter the source node:0 185 | // Enter the cost matrix, For infinity enter 999 186 | // 0 5 8 999 187 | // 5 0 9 2 188 | // 8 9 0 6 189 | // 999 2 6 0 190 | // 0->1 [ distance = 5] 191 | // 0->2 [ distance = 8] 192 | // 0->1 ->3 [ distance = 7] 193 | // image for input graph https://www.freecodecamp.org/news/content/images/2020/06/image-126.png 194 | --------------------------------------------------------------------------------