modify the following code so it connects to a tcp port

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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 3 steps

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-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education