C PROGRAMMING C90 modify the following code so it connects to a tcp port
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
#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;
}
Step by step
Solved in 2 steps with 2 images