DISTANCE VECTOR ROUTING PROTOCOL
Nandana J
CSE C 3
#include <stdio.h>
#include <stdbool.h>
#define MAX_ROUTERS 100 // Maximum number of routers
#define INF 1000 // Represents infinity
// Router structure with fixed-size arrays
typedef struct {
int distance[MAX_ROUTERS]; // Distance to each router
int next_hop[MAX_ROUTERS]; // Next hop to reach each router
} Router;
int cost_matrix[MAX_ROUTERS][MAX_ROUTERS]; // Global cost matrix
Router routers[MAX_ROUTERS]; // Global router array
// Run distance vector algorithm to find shortest paths
void distance_vector_algorithm(int n) {
bool updated;
// Initialize distance and next_hop arrays
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
routers[i].distance[j] = (i == j) ? 0 :
(cost_matrix[i][j] != -1) ? cost_matrix[i][j] : INF;
routers[i].next_hop[j] = (i == j || cost_matrix[i][j] != -1) ? j : -1;
}
}
// Run algorithm until no updates
do {
updated = false;
for (int i = 0; i < n; i++) { // For each router
for (int j = 0; j < n; j++) { // For each destination
if (i == j) continue;
for (int k = 0; k < n; k++) { // For each neighbor
if (cost_matrix[i][k] == -1) continue;
int new_cost = cost_matrix[i][k] + routers[k].distance[j];
if (new_cost < routers[i].distance[j]) {
routers[i].distance[j] = new_cost;
routers[i].next_hop[j] = k;
updated = true;
}
}
}
}
} while (updated);
}
// Print the shortest path from start to destination
void print_path(int start, int dest, int n) {
if (routers[start].distance[dest] == INF) {
printf("No path exists from %d to %d\n", start, dest);
return;
}
CONGESTION CONTROL USING LEAKY BUCKET ALGORITHM
Nandana J
CSE C 3
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int n,npkts[n],bkt_size, out_rate, dropped, bkt_lvl = 0;
printf("Enter the number of iterations :");
scanf("%d",&n);
printf("Enter the number of packets to be sent in each iteration\n");
for(int i = 0; i < n ; i++){
printf("Iteration %d :",i+1);
scanf("%d",&npkts[i]);
}
printf("Enter the Bucket size : ");
scanf("%d",&bkt_size);
printf("Enter the outgoing rate : ");
scanf("%d",&out_rate);
for(int i = 0; i < n; i++){
printf("[INCOMING] %d packets\n",npkts[i]);
dropped = (npkts[i] + bkt_lvl) - bkt_size;
if (dropped > 0){
printf("[DROPPED] %d packets\n",dropped);
bkt_lvl = (npkts[i] + bkt_lvl) - dropped;
}
else
bkt_lvl = (npkts[i] + bkt_lvl);
printf("Current number of packets in bucket : %d\n",bkt_lvl);
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
while(bkt_lvl > 0){
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
printf("Bucket empty\n");
}
FILE TRANSFER PROTOCOL
Nandana J
CSE C 3
SERVER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 3003
#define BUFFER_SIZE 1024
void handle_client(int client_socket)
{
char filename[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
bzero(filename,1024);
recv(client_socket, filename, sizeof(filename), 0);
printf("File: %s\n", filename);
pid_t pid = getpid();
sprintf(buffer, "Server PID: %d\n", pid);
send(client_socket, buffer, strlen(buffer), 0);
bzero(buffer,1024);
int file = open(filename, O_RDONLY);
if (file < 0) {
strcpy(buffer, "Error: File not found\n");
send(client_socket, buffer, strlen(buffer), 0);
}
else
{
int bytes_read;
while ((bytes_read = read(file, buffer, sizeof(buffer))) > 0)
{
send(client_socket, buffer, bytes_read, 0);
}
close(file);
}
close(client_socket); exit(0);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Binding failed");
exit(EXIT_FAILURE);
WIRESHARK TOOLS
Nandana J
CSE 3
NETWORK COMMANDS
Nandana J
CSE C 3
NS2 SIMULATOR
Nandana J
CSE C 3
#Create a simulator object set ns
[new Simulator]
# Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
# Open the NAM trace file set nf
[open out.nam w]
$ns namtrace-all $nf
# Define a 'finish' procedure proc
finish {} {
global ns nf
$ns flush-trace
# Close the NAM trace file
close $nf
# Execute NAM on the trace file exec
nam out.nam &
exit 0
}
# Create five nodes set n0
[$ns node] set n1 [$ns
node] set n2 [$ns node] set
n3 [$ns node] set n4 [$ns
node]
# Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns duplex-link $n3 $n4 1.7Mb 20ms DropTail
# Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
# Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
# Monitor the queue for link (n2-n3) (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
# Setup a TCP connection set tcp
[new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp set sink
[new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
DISTANCE VECTOR ROUTING PROTOCOL
Vishnuhari V A
CSE C 60
#include <stdio.h>
#include <stdbool.h>
#define MAX_ROUTERS 100 // Maximum number of routers
#define INF 1000 // Represents infinity
// Router structure with fixed-size arrays
typedef struct {
int distance[MAX_ROUTERS]; // Distance to each router
int next_hop[MAX_ROUTERS]; // Next hop to reach each router
} Router;
int cost_matrix[MAX_ROUTERS][MAX_ROUTERS]; // Global cost matrix
Router routers[MAX_ROUTERS]; // Global router array
// Run distance vector algorithm to find shortest paths
void distance_vector_algorithm(int n) {
bool updated;
// Initialize distance and next_hop arrays
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
routers[i].distance[j] = (i == j) ? 0 :
(cost_matrix[i][j] != -1) ? cost_matrix[i][j] : INF;
routers[i].next_hop[j] = (i == j || cost_matrix[i][j] != -1) ? j : -1;
}
}
// Run algorithm until no updates
do {
updated = false;
for (int i = 0; i < n; i++) { // For each router
for (int j = 0; j < n; j++) { // For each destination
if (i == j) continue;
for (int k = 0; k < n; k++) { // For each neighbor
if (cost_matrix[i][k] == -1) continue;
int new_cost = cost_matrix[i][k] + routers[k].distance[j];
if (new_cost < routers[i].distance[j]) {
routers[i].distance[j] = new_cost;
routers[i].next_hop[j] = k;
updated = true;
}
}
}
}
} while (updated);
}
// Print the shortest path from start to destination
void print_path(int start, int dest, int n) {
if (routers[start].distance[dest] == INF) {
printf("No path exists from %d to %d\n", start, dest);
return;
}
CONGESTION CONTROL USING LEAKY BUCKET ALGORITHM
Vishnuhari V A
CSE C 60
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int n,npkts[n],bkt_size, out_rate, dropped, bkt_lvl = 0;
printf("Enter the number of iterations :");
scanf("%d",&n);
printf("Enter the number of packets to be sent in each iteration\n");
for(int i = 0; i < n ; i++){
printf("Iteration %d :",i+1);
scanf("%d",&npkts[i]);
}
printf("Enter the Bucket size : ");
scanf("%d",&bkt_size);
printf("Enter the outgoing rate : ");
scanf("%d",&out_rate);
for(int i = 0; i < n; i++){
printf("[INCOMING] %d packets\n",npkts[i]);
dropped = (npkts[i] + bkt_lvl) - bkt_size;
if (dropped > 0){
printf("[DROPPED] %d packets\n",dropped);
bkt_lvl = (npkts[i] + bkt_lvl) - dropped;
}
else
bkt_lvl = (npkts[i] + bkt_lvl);
printf("Current number of packets in bucket : %d\n",bkt_lvl);
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
while(bkt_lvl > 0){
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
printf("Bucket empty\n");
}
FILE TRANSFER PROTOCOL
Vishnuhari V A
CSE C 60
SERVER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 3003
#define BUFFER_SIZE 1024
void handle_client(int client_socket)
{
char filename[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
bzero(filename,1024);
recv(client_socket, filename, sizeof(filename), 0);
printf("File: %s\n", filename);
pid_t pid = getpid();
sprintf(buffer, "Server PID: %d\n", pid);
send(client_socket, buffer, strlen(buffer), 0);
bzero(buffer,1024);
int file = open(filename, O_RDONLY);
if (file < 0) {
strcpy(buffer, "Error: File not found\n");
send(client_socket, buffer, strlen(buffer), 0);
}
else
{
int bytes_read;
while ((bytes_read = read(file, buffer, sizeof(buffer))) > 0)
{
send(client_socket, buffer, bytes_read, 0);
}
close(file);
}
close(client_socket); exit(0);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Binding failed");
exit(EXIT_FAILURE);
WIRESHARK TOOLS
Vishnuhari V A
CSE 60
NETWORK COMMANDS
Vishnuhari V A
CSE C 60
NS2 SIMULATOR
Vishnuhari V A
CSE C 60
#Create a simulator object set ns
[new Simulator]
# Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
# Open the NAM trace file set nf
[open out.nam w]
$ns namtrace-all $nf
# Define a 'finish' procedure proc
finish {} {
global ns nf
$ns flush-trace
# Close the NAM trace file
close $nf
# Execute NAM on the trace file exec
nam out.nam &
exit 0
}
# Create five nodes set n0
[$ns node] set n1 [$ns
node] set n2 [$ns node] set
n3 [$ns node] set n4 [$ns
node]
# Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns duplex-link $n3 $n4 1.7Mb 20ms DropTail
# Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
# Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
# Monitor the queue for link (n2-n3) (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
# Setup a TCP connection set tcp
[new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp set sink
[new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
DISTANCE VECTOR ROUTING PROTOCOL
Sandra Mariya George
CSE C 32
#include <stdio.h>
#include <stdbool.h>
#define MAX_ROUTERS 100 // Maximum number of routers
#define INF 1000 // Represents infinity
// Router structure with fixed-size arrays
typedef struct {
int distance[MAX_ROUTERS]; // Distance to each router
int next_hop[MAX_ROUTERS]; // Next hop to reach each router
} Router;
int cost_matrix[MAX_ROUTERS][MAX_ROUTERS]; // Global cost matrix
Router routers[MAX_ROUTERS]; // Global router array
// Run distance vector algorithm to find shortest paths
void distance_vector_algorithm(int n) {
bool updated;
// Initialize distance and next_hop arrays
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
routers[i].distance[j] = (i == j) ? 0 :
(cost_matrix[i][j] != -1) ? cost_matrix[i][j] : INF;
routers[i].next_hop[j] = (i == j || cost_matrix[i][j] != -1) ? j : -1;
}
}
// Run algorithm until no updates
do {
updated = false;
for (int i = 0; i < n; i++) { // For each router
for (int j = 0; j < n; j++) { // For each destination
if (i == j) continue;
for (int k = 0; k < n; k++) { // For each neighbor
if (cost_matrix[i][k] == -1) continue;
int new_cost = cost_matrix[i][k] + routers[k].distance[j];
if (new_cost < routers[i].distance[j]) {
routers[i].distance[j] = new_cost;
routers[i].next_hop[j] = k;
updated = true;
}
}
}
}
} while (updated);
}
// Print the shortest path from start to destination
void print_path(int start, int dest, int n) {
if (routers[start].distance[dest] == INF) {
printf("No path exists from %d to %d\n", start, dest);
return;
}
CONGESTION CONTROL USING LEAKY BUCKET ALGORITHM
Sandra Mariya George
CSE C 32
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int n,npkts[n],bkt_size, out_rate, dropped, bkt_lvl = 0;
printf("Enter the number of iterations :");
scanf("%d",&n);
printf("Enter the number of packets to be sent in each iteration\n");
for(int i = 0; i < n ; i++){
printf("Iteration %d :",i+1);
scanf("%d",&npkts[i]);
}
printf("Enter the Bucket size : ");
scanf("%d",&bkt_size);
printf("Enter the outgoing rate : ");
scanf("%d",&out_rate);
for(int i = 0; i < n; i++){
printf("[INCOMING] %d packets\n",npkts[i]);
dropped = (npkts[i] + bkt_lvl) - bkt_size;
if (dropped > 0){
printf("[DROPPED] %d packets\n",dropped);
bkt_lvl = (npkts[i] + bkt_lvl) - dropped;
}
else
bkt_lvl = (npkts[i] + bkt_lvl);
printf("Current number of packets in bucket : %d\n",bkt_lvl);
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
while(bkt_lvl > 0){
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
printf("Bucket empty\n");
}
FILE TRANSFER PROTOCOL
Sandra Mariya George
CSE C 32
SERVER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 3003
#define BUFFER_SIZE 1024
void handle_client(int client_socket)
{
char filename[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
bzero(filename,1024);
recv(client_socket, filename, sizeof(filename), 0);
printf("File: %s\n", filename);
pid_t pid = getpid();
sprintf(buffer, "Server PID: %d\n", pid);
send(client_socket, buffer, strlen(buffer), 0);
bzero(buffer,1024);
int file = open(filename, O_RDONLY);
if (file < 0) {
strcpy(buffer, "Error: File not found\n");
send(client_socket, buffer, strlen(buffer), 0);
}
else
{
int bytes_read;
while ((bytes_read = read(file, buffer, sizeof(buffer))) > 0)
{
send(client_socket, buffer, bytes_read, 0);
}
close(file);
}
close(client_socket); exit(0);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Binding failed");
exit(EXIT_FAILURE);
WIRESHARK TOOLS
Sandra Mariya George
CSE 32
NETWORK COMMANDS
Sandra Mariya George
CSE C 32
NS2 SIMULATOR
Sandra Mariya George
CSE C 32
#Create a simulator object set ns
[new Simulator]
# Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
# Open the NAM trace file set nf
[open out.nam w]
$ns namtrace-all $nf
# Define a 'finish' procedure proc
finish {} {
global ns nf
$ns flush-trace
# Close the NAM trace file
close $nf
# Execute NAM on the trace file exec
nam out.nam &
exit 0
}
# Create five nodes set n0
[$ns node] set n1 [$ns
node] set n2 [$ns node] set
n3 [$ns node] set n4 [$ns
node]
# Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns duplex-link $n3 $n4 1.7Mb 20ms DropTail
# Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
# Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
# Monitor the queue for link (n2-n3) (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
# Setup a TCP connection set tcp
[new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp set sink
[new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink
DISTANCE VECTOR ROUTING PROTOCOL
Stiya Johnson
CSE C 51
#include <stdio.h>
#include <stdbool.h>
#define MAX_ROUTERS 100 // Maximum number of routers
#define INF 1000 // Represents infinity
// Router structure with fixed-size arrays
typedef struct {
int distance[MAX_ROUTERS]; // Distance to each router
int next_hop[MAX_ROUTERS]; // Next hop to reach each router
} Router;
int cost_matrix[MAX_ROUTERS][MAX_ROUTERS]; // Global cost matrix
Router routers[MAX_ROUTERS]; // Global router array
// Run distance vector algorithm to find shortest paths
void distance_vector_algorithm(int n) {
bool updated;
// Initialize distance and next_hop arrays
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
routers[i].distance[j] = (i == j) ? 0 :
(cost_matrix[i][j] != -1) ? cost_matrix[i][j] : INF;
routers[i].next_hop[j] = (i == j || cost_matrix[i][j] != -1) ? j : -1;
}
}
// Run algorithm until no updates
do {
updated = false;
for (int i = 0; i < n; i++) { // For each router
for (int j = 0; j < n; j++) { // For each destination
if (i == j) continue;
for (int k = 0; k < n; k++) { // For each neighbor
if (cost_matrix[i][k] == -1) continue;
int new_cost = cost_matrix[i][k] + routers[k].distance[j];
if (new_cost < routers[i].distance[j]) {
routers[i].distance[j] = new_cost;
routers[i].next_hop[j] = k;
updated = true;
}
}
}
}
} while (updated);
}
// Print the shortest path from start to destination
void print_path(int start, int dest, int n) {
if (routers[start].distance[dest] == INF) {
printf("No path exists from %d to %d\n", start, dest);
return;
}
CONGESTION CONTROL USING LEAKY BUCKET ALGORITHM
Stiya Johnson
CSE C 51
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int n,npkts[n],bkt_size, out_rate, dropped, bkt_lvl = 0;
printf("Enter the number of iterations :");
scanf("%d",&n);
printf("Enter the number of packets to be sent in each iteration\n");
for(int i = 0; i < n ; i++){
printf("Iteration %d :",i+1);
scanf("%d",&npkts[i]);
}
printf("Enter the Bucket size : ");
scanf("%d",&bkt_size);
printf("Enter the outgoing rate : ");
scanf("%d",&out_rate);
for(int i = 0; i < n; i++){
printf("[INCOMING] %d packets\n",npkts[i]);
dropped = (npkts[i] + bkt_lvl) - bkt_size;
if (dropped > 0){
printf("[DROPPED] %d packets\n",dropped);
bkt_lvl = (npkts[i] + bkt_lvl) - dropped;
}
else
bkt_lvl = (npkts[i] + bkt_lvl);
printf("Current number of packets in bucket : %d\n",bkt_lvl);
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
while(bkt_lvl > 0){
if (bkt_lvl - out_rate > 0){
printf("[OUTGOING] %d packets\n",out_rate);
bkt_lvl = bkt_lvl - out_rate;
}
else{
printf("[OUTGOING] %d packets\n",bkt_lvl);
bkt_lvl = 0;
}
printf("Number of packets left in the bucket: %d\n",bkt_lvl);
}
printf("Bucket empty\n");
}
FILE TRANSFER PROTOCOL
Stiya Johnson
CSE C 51
SERVER
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 3003
#define BUFFER_SIZE 1024
void handle_client(int client_socket)
{
char filename[BUFFER_SIZE];
char buffer[BUFFER_SIZE];
bzero(filename,1024);
recv(client_socket, filename, sizeof(filename), 0);
printf("File: %s\n", filename);
pid_t pid = getpid();
sprintf(buffer, "Server PID: %d\n", pid);
send(client_socket, buffer, strlen(buffer), 0);
bzero(buffer,1024);
int file = open(filename, O_RDONLY);
if (file < 0) {
strcpy(buffer, "Error: File not found\n");
send(client_socket, buffer, strlen(buffer), 0);
}
else
{
int bytes_read;
while ((bytes_read = read(file, buffer, sizeof(buffer))) > 0)
{
send(client_socket, buffer, bytes_read, 0);
}
close(file);
}
close(client_socket); exit(0);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
perror("Binding failed");
exit(EXIT_FAILURE);
WIRESHARK TOOLS
Stiya Johnson
CSE 51
NETWORK COMMANDS
Stiya Johnson
CSE C 51
NS2 SIMULATOR
Stiya Johnson
CSE C 51
#Create a simulator object set ns
[new Simulator]
# Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
# Open the NAM trace file set nf
[open out.nam w]
$ns namtrace-all $nf
# Define a 'finish' procedure proc
finish {} {
global ns nf
$ns flush-trace
# Close the NAM trace file
close $nf
# Execute NAM on the trace file exec
nam out.nam &
exit 0
}
# Create five nodes set n0
[$ns node] set n1 [$ns
node] set n2 [$ns node] set
n3 [$ns node] set n4 [$ns
node]
# Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
$ns duplex-link $n3 $n4 1.7Mb 20ms DropTail
# Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
# Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
# Monitor the queue for link (n2-n3) (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
# Setup a TCP connection set tcp
[new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp set sink
[new Agent/TCPSink]
$ns attach-agent $n4 $sink
$ns connect $tcp $sink