C PROGRAMMING C90 modify the following code so it connects to a tcp port

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

C PROGRAMMING C90

modify the following code so it connects to a tcp port

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#include "graph.h" // Include the graph library
#include "dijkstra.h" // Include the Dijkstra's algorithm implementation

#define MAX_LINE_LENGTH 512 // Maximum length of a line of text
#define MAX_NETWORKS 255 // Maximum number of networks
#define MAX_CONNECTIONS ((MAX_NETWORKS * (MAX_NETWORKS - 1)) / 2) // Maximum number of connections between networks
#define TIMEOUT_SECS 10 // Timeout in seconds for waiting for greeting and QUIT response

// Struct to represent a graph
struct graph {
  int num_vertices; // Number of vertices in the graph
  int num_edges; // Number of edges in the graph
  int *vertices; // Array of vertices
  int *edges; // Array of edges
};

// Function to create a graph
struct graph *create_graph(int num_vertices, int num_edges) {
  struct graph *g = malloc(sizeof(struct graph)); // Allocate memory for the graph
  if (g == NULL) {
    return NULL; // Return NULL if the allocation failed
  }

  // Initialize the fields of the graph
  g->num_vertices = num_vertices;
  g->num_edges = num_edges;
  g->vertices = malloc(num_vertices * sizeof(int)); // Allocate memory for the vertices array
  g->edges = malloc(num_edges * sizeof(int)); // Allocate memory for the edges array

  if (g->vertices == NULL || g->edges == NULL) {
    // Free the allocated memory if the allocation of the vertices or edges array failed
    free(g->vertices);
    free(g->edges);
    free(g);
    return NULL;
  }

  // Initialize the vertices and edges array to 0
  memset(g->vertices, 0, num_vertices * sizeof(int));
  memset(g->edges, 0, num_edges * sizeof(int));

  return g; // Return the created graph
}

typedef struct {
  int src; // Source network
  int dest; // Destination network
  int weight; // Weight of connection
} connection_t;

int main(int argc, char *argv[]) {
  printf("S: +OK 2022 Programming Portfolio Route Server\n");

  struct graph *g = create_graph(MAX_NETWORKS, MAX_CONNECTIONS);

  int num_networks = 0;

  char input[MAX_LINE_LENGTH];
  while (1) {
    printf("C: ");
    fgets(input, MAX_LINE_LENGTH, stdin);
    input[strcspn(input, "\r\n")] = 0;

    if (strcmp(input, "QUIT") == 0) {
      printf("S: +OK\n");
      break;
    } else if (strcmp(input, "NET-ADD") == 0) {
      if (num_networks == MAX_NETWORKS) {
        printf("S: -ERR Too many networks\n");
      } else {
        printf("S: +OK Added %d\n", num_networks + 1);
        num_networks++;
      }
    } else if (strncmp(input, "ROUTE-ADD", strlen("ROUTE-ADD")) == 0) {
      int src, dest, weight;
      if (sscanf(input, "ROUTE-ADD %d %d %d", &src, &dest, &weight) == 3) {
        if (src > num_networks || dest > num_networks) {
          printf("S: -ERR Invalid network number\n");
        } else {
          printf("S: +OK Route Added\n");
        }
      } else {
        printf("S: -ERR Invalid command format\n");
      }
    } else if (strncmp(input, "ROUTE-TABLE", strlen("ROUTE-TABLE")) == 0) {
      int src;
      if (sscanf(input, "ROUTE-TABLE %d", &src) == 1) {
        if (src > num_networks) {
          printf("S: -ERR Invalid network number\n");
        } else {
          printf("S: +OK 4\n");
          printf("S: 3 -> 1, next-hop 1, weight 1\n");
          printf("S: 3 -> 2, next-hop 4, weight 4\n");
          printf("S: 3 -> 4, next-hop 4, weight 2\n");
          printf("S: 3 -> 5, next-hop 1, weight 3\n");
        }
      } else {
        printf("S: -ERR Invalid command format\n");
      }
    } else if (strncmp(input, "ROUTE-SHOW", strlen("ROUTE-SHOW")) == 0) {
      int dest;
      if (sscanf(input, "ROUTE-SHOW %d", &dest) == 1) {
        if (dest > num_networks) {
          printf("S: -ERR Invalid network number\n");
        } else {
          printf("S: +OK 1\n");
          printf("S: 1\n");
        }
      } else {
        printf("S: -ERR Invalid command format\n");
      }
    } else {
      printf("S: -ERR Not Implemented\n");
    }
  }

  return 0;
}

Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Types of Protocols
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY