Professional Documents
Culture Documents
CN Lab File
CN Lab File
Objective: Implementation of Stop and Wait Protocol and Sliding Window Protocol.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int k, time, win = 2, i2 = 0, frame = 0, a[20], b[20], i, j, s, r, ack, c, d;
int send(int, int);
int receive();
int checsum(int *);
void main()
{
int i1 = 0, j1 = 0, c1;
printf("Enter the frame size\n");
scanf("%d", &frame);
printf("Enter the window size\n");
scanf("%d", &win);
j1 = win;
for (i = 0; i < frame; i++)
{
a[i] = rand();
}
k = 1;
while (i1 < frame)
{
if ((frame - i1) < win)
j1 = frame - i1;
printf("\n\ntransmit the window no %d\n\n", k);
c1 = send(i1, i1 + j1);
int t1;
for (i = c; i < d; i++)
{
b[i] = a[i];
printf("frame %d is sent\n", i);
}
s = checsum(&a[c]);
return (s);
}
return sum;
}
OUTPUT:
Enter the frame size
10
Enter the window size
2
transmit the window no 1
frame 0 is sent
frame 1 is sent
frame 2 is sent
frame 3 is sent
frame 4 is sent
frame 5 is sent
frame 6 is sent
frame 7 is sent
frame 8 is sent
frame 9 is sent
//Server Program:
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>
#include <cstdio>
int main()
{
int listenfd, connfd, n;
struct sockaddr_in serv_addr;
char buff[100];
int port = 1058;
listen(listenfd, 15);
while (1)
{
connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
cout << "\nConnection Opened!\n";
int child;
if ((child = fork()) < 0)
{
perror("Fork error");
exit(EXIT_FAILURE);
}
else if (child == 0)
{
close(listenfd);
strcpy(buff, "abc");
while (strcmp(buff, "bye") != 0)
{
if (strcmp(buff, "bye") == 0)
break;
n = read(connfd, buff, 100);
buff[n] = '\0';
cout << "\nClient: " << buff;
cout << "Server: ";
cin.getline(buff, 100);
write(connfd, buff, strlen(buff));
}
return 0;
}
// Client Program
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>
#include <cstdio>
if (argc != 3)
{
printf("\nError! Usage: ./a.out <IP ADDRESS> <PORT>\n");
exit(EXIT_FAILURE);
}
{
cout << "\nClient: ";
cin.getline(buff, 100);
write(sockfd, buff, strlen(buff));
cout << "\nServer: ";
n = read(sockfd, buff, 100);
buff[n] = '\0';
cout << buff;
if (n < 0)
{
perror("\nRead error");
exit(EXIT_FAILURE);
}
}
cout << "\n";
return 0;}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
};
return;
}
}
int main() {
// Sample ARP table
struct ARP_Entry arp_table[3] = {
{"192.168.1.10", "00:11:22:33:44:55"},
{"192.168.1.20", "AA:BB:CC:DD:EE:FF"},
{"192.168.1.30", "11:22:33:44:55:66"}
};
int choice;
while (1) {
printf("\n1. ARP\n2. RARP\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
ARP(arp_table, 3);
break;
case 2:
RARP(arp_table, 3);
break;
case 3:
exit(0);
default:
printf("Invalid choice. Please enter 1, 2, or 3.\n");
}
}
return 0;
}
Output
1. ARP
2. RARP
3. Exit
Enter your choice: 1
Enter the IP address to lookup: 192.168.1.20
MAC address for IP 192.168.1.20 is: AA:BB:CC:DD:EE:FF
1. ARP
2. RARP
3. Exit
Enter your choice: 2
Enter the MAC address to lookup: 11:22:33:44:55:66
IP address for MAC 11:22:33:44:55:66 is: 192.168.1.30
1. ARP
2. RARP
3. Exit
Enter your choice: 3
Objective: Write a code simulating PING and TRACEROUTE commands
//Variation 1:
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
int main() {
const char *hostName = [@"stackoverflow.com" cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkConnectionFlags flags = 0;
return 0;
}
//Variation 2:
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
int main() {
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….
if (isAvailable) {
NSLog(@"Host is reachable: %d", flags);
} else {
NSLog(@"Host is unreachable");
}
return 0;
}
OUTPUT
Host is reachable: 3
Objective: Create a socket for HTTP for web page upload and download.
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
if (argc != 3) {
printf("\nUsage: %s <server> <filename>\n", argv[0]);
exit(1);
}
strcpy(hostname, argv[1]);
*cp = '/';
l = strlen(cp);
req = malloc(l + 1);
strcpy(req, cp);
} else {
hostname = argv[1];
req = "/";
}
bzero(&sock, sizeof(sock));
sock.sin_family = AF_INET;
sock.sin_port = htons(80);
con = connect(sd, (struct sockaddr *)&sock, sizeof(sock));
if (con < 0) {
perror("\nConnection failed");
exit(1);
}
do {
nrv = read(sd, buff, size);
if (nrv > 0) {
fwrite(buff, 1, nrv, local);
} else {
break;
}
} while (1);
close(sd);
fclose(local);
return 0;
}
}
}
// Server class
public class RMIServer {
public static void main(String[] args) {
try {
// Create the remote object
Calculator calculator = new CalculatorImpl();
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
// Client class
public class RMIClient {
public static void main(String[] args) {
try {
// Get reference to the remote object from the registry
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
Calculator calculator = (Calculator) registry.lookup("CalculatorService");
Output:
\\ and these bits to get the Network Address. Note, the prefix could be used
\\ instead of subnet mask - they represent the same thing.
//EchoClient.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
String inputLine;
while ((inputLine = userInput.readLine()) != null) {
out.println(inputLine);
System.out.println("Server: " + in.readLine());
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//EchoServer.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Client: " + inputLine);
out.println(inputLine);
}
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT: SERVER
CLIENT:
RESULT: Thus, the program was executed and output is verified successfully
b) Chat Application:
//ChatServer.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket);
new ClientHandler(clientSocket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class ClientHandler extends Thread {
private Socket clientSocket;
private BufferedReader in;
ClientHandler(Socket socket) {
this.clientSocket = socket;
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println("Message from client: " + inputLine);
broadcast(inputLine);
}
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
C. File Transfer
//FileServer.java:
import java.io.*;
import java.net.*;
while (true) {
ClientHandler(Socket socket) {
this.clientSocket = socket;
}
fis.close();
os.close();
clientSocket.close();
System.out.println("File sent successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//FileClient.java:
import java.io.*;
import java.net.*;
try {
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to file server.");
fos.close();
socket.close();
System.out.println("File received successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
socket.send(responsePacket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT: SERVER & CLIENT
1. NS Overview:
NS (Network Simulator) is a widely-used discrete event network simulator written in C++.
It provides a simulation environment for modeling wired and wireless networks, allowing researchers to
study network protocols and algorithms.
2. Simulation Process:
Define Network Topology: Create a network topology by specifying nodes, links, traffic sources, and
destinations.
Configure Protocols: Set up protocols and algorithms to be simulated, such as TCP variants and routing
protocols.
Run Simulation: Execute the simulation and gather data on network performance metrics, like
throughput, delay, and packet loss.
4. Example:
Create a network topology in NS with multiple nodes and links.
Implement TCP Tahoe and TCP Reno congestion control algorithms in C.
Integrate the algorithms into NS by modifying the TCP module.
Run simulations under different network conditions (e.g., bandwidth, delay) and observe algorithm
performance.
Analyze simulation results to compare throughput, delay, and fairness of TCP Tahoe and TCP Reno.