[go: up one dir, main page]

0% found this document useful (0 votes)
20 views15 pages

CN Assignment

The document contains lab assignments for a Computer Networks course at Swami Vivekananda University, detailing various C programming tasks related to socket programming, TCP client-server connections, and routing algorithms. It includes code implementations for a client and server using sockets, as well as algorithms for Distance Vector and Link State Routing. Each section is accompanied by output examples demonstrating the functionality of the implemented programs.

Uploaded by

parijatdas823
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views15 pages

CN Assignment

The document contains lab assignments for a Computer Networks course at Swami Vivekananda University, detailing various C programming tasks related to socket programming, TCP client-server connections, and routing algorithms. It includes code implementations for a client and server using sockets, as well as algorithms for Distance Vector and Link State Routing. Each section is accompanied by output examples demonstrating the functionality of the implemented programs.

Uploaded by

parijatdas823
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

SWAMI VIVEKANANDA UNIVERSITY (SVU)

STUDENT NAME: Parijat Das


ROLL NO: 002-BCS-2021-046
REGISTRATION NO: 002-103-2021-046
SUBJECT: Computer Networks
PAPER NAME: Computer Networks Lab
PAPER CODE: BTCSC692
SESSION: 2021 – 2025
INDEX

Page
Problem Statement Date Signature
No.
Write a C program to implement socket programming
09.05.2024 1-2
for Client.
Write a C program to implement socket programming
09.05.2024 3-4
for Server.
Write a C program to implement TCP Client-Server
09.05.2024 5-8
socket connection.
Write a C program to implement Distance Vector
10.05.2024 9 - 11
Routing algorithm.
Write a C program to implement Link State Routing
10.05.2024 12 - 13
algorithm.
09.05.2024

Write a C program to implement socket programming for client.

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

int main(int argc, char const* argv[]) {


int sock = 0, valread, client_fd;
struct sockaddr_in serv_addr;
char* hello = "Hello _ client";
char buffer[1024] = {0};

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {


printf("\n Socket creation error \n");
return -1;
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {

1
09.05.2024

printf("\nInvalid address/ Address not supported \n");


return -1;
}

if ((client_fd = connect(sock, (struct sockaddr*)&serv_addr,


sizeof(serv_addr))) < 0) {
printf("\nConnection Failed \n");
return -1;
}

send(sock, hello, strlen(hello), 0);


printf("Hello message sent\n");
valread = read(sock, buffer, 1024);
printf("%s\n", buffer);

// closing the connected socket

close(client_fd);
return 0;
}

OUTPUT

2
09.05.2024

Write a C program to implement socket programming for server.


// server-side socket connection.
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080

int main(int argc, char const* argv[]) {


int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char* hello = "Hello from server";

// Creating socket file descriptor


if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}

3
09.05.2024

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

// Forcefully attaching socket to port 8080


if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr*)&address,
(socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");

// closing the connected socket


close(new_socket);
// closing the listening socket
shutdown(server_fd, SHUT_RDWR);
return 0;
}

4
09.05.2024

Write a C program to implement TCP Client-Server socket


connection.

#include <arpa/inet.h> // inet_addr()


#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> // bzero()
#include <sys/socket.h>
#include <unistd.h> // read(), write(), close()
#define MAX 80
#define PORT 8080
#define SA struct sockaddr

5
09.05.2024

void func(int sockfd) {


char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n') {
write(sockfd, buff, sizeof(buff));
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
}
}
}

int main() {
int sockfd, connfd;
struct sockaddr_in servaddr, cli;

// socket create and verification


sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(0);

6
09.05.2024

} else {
printf("Socket successfully created..\n");
}
bzero(&servaddr, sizeof(servaddr));

// assign IP, PORT


servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);

// connect the client socket to server socket


if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
printf("connection with the server failed...\n");
exit(0);
} else
printf("connected to the server..\n");

// function for chat


func(sockfd);

// close the socket


close(sockfd);
}

OUTPUT

Server side:
Socket successfully created..
Socket successfully binded..
Server listening..

7
09.05.2024

server accept the client...


From client: hi
To client : hello
From client: exit
To client : exit
Server Exit...
Client side:
Socket successfully created..
connected to the server..
Enter the string : hi
From Server : hello
Enter the string : exit
From Server : exit
Client Exit...

8
10.05.2024

Write a C program to implement Distance Vector Routing Algorithm.


// DISTANCE VECTOR ROUTING ALGORITHM …

#include <stdio.h>
#include <stdlib.h>
#define MAX_NODES 10

typedef struct Node { // Structure to represent a node in the network


int id;
int routing_table[MAX_NODES][3]; // [destination, next_hop, cost]
int num_neighbors;
struct Node* neighbors[MAX_NODES];
} Node;

void initialize_node(Node* node, int id) { // Function to initialize a node


int i, j;
node->id = id;
node->num_neighbors = 0;
for (i = 0; i < MAX_NODES; i++) {
for (j = 0; j < 3; j++)
node->routing_table[i][j] = -1;
}
}

void add_neighbor(Node* node, Node* neighbor) { // Function to add a neighbor to a node


if (node->num_neighbors < MAX_NODES) {
node->neighbors[node->num_neighbors] = neighbor;
node->num_neighbors++;
}
}

void update_routing_table(Node* node) { // Function to update the routing table of a node


int i, j;
for (i = 0; i < node->num_neighbors; i++) {
Node* neighbor = node->neighbors[i];
for (j = 0; j < MAX_NODES; j++) {
if (neighbor->routing_table[j][0] != -1) {
int destination = neighbor->routing_table[j][0];
int cost = neighbor->routing_table[j][2];
int total_cost = cost + 1; // Assuming a cost of 1 for each hop

if (node->routing_table[destination][0] == -1 ||
total_cost < node->routing_table[destination][2]) {
node->routing_table[destination][0] = destination;
node->routing_table[destination][1] = neighbor->id;
node->routing_table[destination][2] = total_cost;

9
10.05.2024

}
}
}
}
}

// Function to print the routing table of a node


void print_routing_table(Node* node) {
int i;
printf("Routing table of Node %d:\n", node->id);
printf("| Destination | Next Hop | Cost |\n");
for (i = 0; i < MAX_NODES; i++) {
if (node->routing_table[i][0] != -1) {
printf("| %2d | %2d | %2d |\n",
node->routing_table[i][0], node->routing_table[i][1],
node->routing_table[i][2]);
}
}
}

int main() {
int i, j;
Node nodes[MAX_NODES]; // Create nodes
for (i = 0; i < MAX_NODES; i++) {
initialize_node(&nodes[i], i);
}
add_neighbor(&nodes[0], &nodes[1]); // Add neighbors
add_neighbor(&nodes[0], &nodes[2]);
add_neighbor(&nodes[1], &nodes[0]);
add_neighbor(&nodes[1], &nodes[3]);
add_neighbor(&nodes[2], &nodes[0]);
add_neighbor(&nodes[2], &nodes[3]);
add_neighbor(&nodes[3], &nodes[1]);
add_neighbor(&nodes[3], &nodes[2]);

int convergence = 0; // Run distance vector routing algorithm


int iteration = 0;
while (!convergence) {
convergence = 1;
printf("Iteration %d\n", iteration);
for (i = 0; i < MAX_NODES; i++)
update_routing_table(&nodes[i]);

for (i = 0; i < MAX_NODES; i++) { // Check for convergence


for (j = 0; j < MAX_NODES; j++) {
if (nodes[i].routing_table[j][0] != -1 &&
nodes[i].routing_table[j][2] !=

10
10.05.2024

nodes[i].routing_table[j][2]) {
convergence = 0;
break;
}
}
if (!convergence)
break;
}

for (i = 0; i < MAX_NODES; i++) {


print_routing_table(&nodes[i]); // Print routing tables
}

iteration++;
printf("\n");
}
return 0;
}

OUTPUT

Iteration 0
Routing table of Node 0:
| Destination | Next Hop | Cost |
Routing table of Node 1:
| Destination | Next Hop | Cost |
Routing table of Node 2:
| Destination | Next Hop | Cost |
Routing table of Node 3:
| Destination | Next Hop | Cost |
Routing table of Node 4:
| Destination | Next Hop | Cost |
Routing table of Node 5:
| Destination | Next Hop | Cost |
Routing table of Node 6:
| Destination | Next Hop | Cost |
Routing table of Node 7:
| Destination | Next Hop | Cost |
Routing table of Node 8:
| Destination | Next Hop | Cost |
Routing table of Node 9:
| Destination | Next Hop | Cost |

--------------------------------
Process exited after 0.03921 seconds with return value 0
Press any key to continue . . .

11
10.05.2024

Write a C program to implement Link State Routing algorithm in


network.
// WAC to implement Link state routing algorithm in networking...
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define MAX_NODES 5 // Maximum number of nodes in the network

// Function to find the node with the minimum distance.


int minDistance(int dist[], bool sptSet[], int n) {
int min = INT_MAX;
int min_index, v;
for (v = 0; v < n; v++)
if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}

// Function to print the shortest path from the source to each node.
void printShortestPath(int dist[], int n) {
int i;
printf("Node Shortest Distance from Source\n");
for (i = 0; i < n; i++)
printf("%d\t\t%d\n", i, dist[i]);
}

// Function to implement the Link State Routing Algorithm


void linkStateRouting(int graph[MAX_NODES][MAX_NODES], int src, int n) {
int dist[MAX_NODES], i, count, v;
// To store the shortest distance from the source node to each node
bool sptSet[MAX_NODES]; // To keep track of nodes included in the shortest path tree

for (i = 0; i < n; i++) { // Initialize all distances as INFINITE and sptSet[] as false
dist[i] = INT_MAX;
sptSet[i] = false;
}
dist[src] = 0; // Distance of source node from itself is always 0

// Find shortest path for all nodes


for (count = 0; count < n - 1; count++) {
int u = minDistance(dist, sptSet, n);
// Mark the selected node as processed
sptSet[u] = true;

12
10.05.2024

// Update dist[] value of the adjacent nodes of the selected node


for (v = 0; v < n; v++) {
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX &&
dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
// Print the shortest path from the source node
printShortestPath(dist, n);
}

int main() {
int n = MAX_NODES; // Number of nodes in the network
int graph[MAX_NODES][MAX_NODES] =
{{0, 2, 4, 0, 0},
{2, 0, 3, 7, 0},
{4, 3, 0, 1, 0},
{0, 7, 1, 0, 3},
{0, 0, 0, 3, 0}};
int src = 0; // Source node
printf("Link State Routing Algorithm\n");
linkStateRouting(graph, src, n);
return 0;
}

OUTPUT

Link State Routing Algorithm


Node Shortest Distance from Source
0 0
1 2
2 4
3 5
4 8

--------------------------------
Process exited after 0.05536 seconds with return value 0
Press any key to continue . . .

13

You might also like