Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a TCP socket program that allows the server (router R1, node ID 7) to rece

ID: 3838501 • Letter: W

Question

Write a TCP socket program that allows the server (router R1, node ID 7) to receive a DVR message from the client (router R2, node ID 6) and update R1’s routing table using the DVR algorithm. The user will enter the input values at the server and client only in the form specified below and the server and client must do the following (values are given below as an example to show how a user enters the input).

Server

1. R1 (server: node 7) first asks the user to enter R1’s neighbor distances and R1’s routing table as follows.

Enter number of neighbors: 2

Enter neighbor IDs separated by a single space: 6 4

Enter distances to each neighbor separated by a single space: 5 3

Enter number of destinations: 6

Enter destinations separated by a single space: 7 4 6 3 2 5

Enter next hop for each destination separated by a single space: 0 4 6 4 6 6

Enter distance to each destination separated by a single space: 0 3 5 14 13 19

After the user enters these values, R1’s neighbor distance pairs are (6, 5), (4, 3); and R1’s routing table is as follows:

Destination

Next Hop

Distance

7

0

0

4

4

3

6

6

5

3

4

14

2

6

13

5

6

19

2. R1 (server: node 7) waits for a TCP socket connection on port 6789 from R2 (client: node 6) to receive the DVR message from R2.

3. R1 receives the DVR message and uses the DVR algorithm (Algorithm 18.3) to update its routing table using the (destination, distance) values in the DVR message.

4. R1 prints its updated routing table showing values of (destination, next hop, distance).

Client

1. R2 (client: node 6) asks the user to enter the information in the DVR message to be sent to R1 (server: node 7) as follows:

Enter number of entries in DVR message: 4

Enter destinations in DVR message separated by a single space: 6 3 2 5

Enter distances to each destination in DVR message separated by a single space: 0 2 8 28

2. R2 (client: node 6) uses the preceding information to form the DVR message, which consists of the pairs of (destination, distance) values (6, 0), (3, 2), (2, 8), (5, 28).

3. R2 (client: node 6) makes a TCP socket connection to R1 (server: node 7) on port 6789 and sends a message M containing the DVR message to R1. The message M consists of the number of entries in the DVR message followed by the (destination, distance) pairs in the DVR message.

Destination

Next Hop

Distance

7

0

0

4

4

3

6

6

5

3

4

14

2

6

13

5

6

19

Explanation / Answer

I have designed and developed the TCP socket programs of both client and server. I have added the comments for each of the program section along with the final output of it.

Let me explain you in more easy and step-by-step manner:-

Step-1:

Server Program:-


// This is the header files in C++ for embedding the methods and properties of each class

#include<stdio.h>
#include<string.h>  
#include<sys/socket.h>
#include<arpa/inet.h>  
#include<unistd.h>  

// The main() method is the starting point of the program

int main(int argc , char *argv[])
{
   int dvrSocketDescrip , dvr_clientSock , dvrObj , dvr_ReadS;
   struct sockaddr_in dvr_server , dvr_client;
   char dvrinput_Message[1000];
  
   // Created a socket connection
  
   dvrSocketDescrip = socket(AF_INET , SOCK_STREAM , 0);
   if (dvrSocketDescrip == -1)
   {
       printf("We Sorry. We are unable to create socket");
   }
   puts("Finally, The Socket has been created successfully");
  

   dvr_server.sin_family = AF_INET;
   dvr_server.sin_addr.s_addr = INADDR_ANY;
   dvr_server.sin_port = htons( 7777 );
  
   // Created a bind connection
  
   if( bind(dvrSocketDescrip,(struct sockaddr *)&dvr_server , sizeof(dvr_server)) < 0)
   {
  
       perror("We Sorry. The binding is failed.");
       return 1;
   }
   puts("The Bind has been created successfully");
  
  
   listen(dvrSocketDescrip , 3);
  
  
   puts("Please wait till the connection get established");
   dvrObj = sizeof(struct sockaddr_in);
  
  
   dvr_clientSock = accept(dvrSocketDescrip, (struct sockaddr *)&dvr_client, (socklen_t*)&dvrObj);
   if (dvr_clientSock < 0)
   {
       perror("The connection has been failed");
       return 1;
   }
   puts("The connection is accepted");
  

   while( (dvr_ReadS = recv(dvr_clientSock , dvrinput_Message , 1000 , 0)) > 0 )
   {
      
       write(dvr_clientSock , dvrinput_Message , strlen(dvrinput_Message));
   }
  
   if(dvr_ReadS == 0)
   {
       puts("The client connection has been failed");
       fflush(stdout);
   }
   else if(dvr_ReadS == -1)
   {
       perror("The receiver end is failed");
   }
  
   return 0;
}

Step-2:

Client Program:-

// This is the header files in C++ for embedding the methods and properties of each class

#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>

// The main() method is the starting point of the program


int main(int argc , char *argv[])
{
int clientSock;
struct sockaddr_in dvr_server;
char clientMsg[1000] , dvrServerResponse[2000];

// Created a socket connection
clientSock = socket(AF_INET , SOCK_STREAM , 0);
if (clientSock == -1)
{
printf("We are Sorry. We are unable to create socket");
}
puts("Finally, The Socket has been created successfully");

dvr_server.sin_addr.s_addr = inet_addr("198.255.0.1");
dvr_server.sin_family = AF_INET;
dvr_server.sin_port = htons( 8888 );

// Connect to dvr server
  
if (connect(clientSock , (struct sockaddr *)&dvr_server , sizeof(dvr_server)) < 0)
{
perror("The connection has been failed");
return 1;
}
puts("The connection is accepted ");

  
while(1)
{
printf("Please enter the message : ");
scanf("%s" , clientMsg);


if( send(clientSock , clientMsg , strlen(clientMsg) , 0) < 0)
{
puts("The send message has been failed");
return 1;
}


if( recv(clientSock , dvrServerResponse , 2000 , 0) < 0)
{
puts("The receive message has been failed");
break;
}

puts("The server message is :");
puts(dvrServerResponse);
}

close(clientSock);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote