Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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);

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

ack = receive(i1, i1 + j1, c1);


if (ack != 0)
{
printf("\n\n1.Selective window\n");
printf("2.Go back N\n");
scanf("%d", &ack);
switch (ack)
{
case 1:
printf("\n\n\t Selective window \t\nEnter the faulty frame no\n");
scanf("%d", &i2);
printf("\n\n Retransmit the frame %d \n", i2);
send(i2, i2 + 1);
break;
case 2:
printf("\n\n\t Go back n\t\n\n");
printf("\nRetransmit the frames from %d to %d\n", i1, i1 + j1);
send(i1, i1 + j1);
break;
}
}
i1 = i1 + win;
k++;
}
}
int send(c, d)
{

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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);
}

int receive(c, d, c2)


int c2;
{
r = checsum(&b[c]);
if (c2 == r)
{
return (0);
}
else
return (1);
}

int checsum(int *c)


{
int sum = 0;
for (i = 0; i < win; i++)
sum = sum ^ (*c);

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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

transmit the window no 2

frame 2 is sent
frame 3 is sent

transmit the window no 3

frame 4 is sent
frame 5 is sent

transmit the window no 4

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

frame 6 is sent
frame 7 is sent

transmit the window no 5

frame 8 is sent
frame 9 is sent

Objective: Study of Socket Programming and Client – Server model

//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>

using namespace std;

int main()
{
int listenfd, connfd, n;
struct sockaddr_in serv_addr;

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

char buff[100];
int port = 1058;

listenfd = socket(AF_INET, SOCK_STREAM, 0);


if (listenfd < 0)
{
perror("Error creating socket");
exit(EXIT_FAILURE);
}

bzero((char *)&serv_addr, sizeof(serv_addr));


serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);

if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)


{
perror("Binding failed");
exit(EXIT_FAILURE);
}

listen(listenfd, 15);

while (1)
{
connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);
cout << "\nConnection Opened!\n";

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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));
}

cout << "\n\nConnection closed!";


close(connfd);
exit(0);
}
close(connfd);

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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>

using namespace std;

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


{
int sockfd, n;
char buff[100];
struct sockaddr_in serv_addr;

if (argc != 3)
{
printf("\nError! Usage: ./a.out <IP ADDRESS> <PORT>\n");

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

exit(EXIT_FAILURE);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)


{
perror("Error creating socket");
exit(EXIT_FAILURE);
}

bzero((char *)&serv_addr, sizeof(serv_addr));


serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[2]));

if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0)


{
perror("Error converting IP address from string to numeric");
exit(EXIT_FAILURE);
}

if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)


{
perror("Error! Connection cannot be established");
exit(EXIT_FAILURE);
}
printf("\nConnected..\n");
strcpy(buff, "abc");
while (strcmp(buff, "bye") != 0)

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

{
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;}

Objective: Write a code simulating ARP /RARP protocols.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Structure to represent an ARP table entry


struct ARP_Entry {
char ip[16];
char mac[18];
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

};

// Function to simulate ARP protocol


void ARP(struct ARP_Entry arp_table[], int table_size) {
char target_ip[16];
printf("Enter the IP address to lookup: ");
scanf("%s", target_ip);

for (int i = 0; i < table_size; i++) {


if (strcmp(arp_table[i].ip, target_ip) == 0) {
printf("MAC address for IP %s is: %s\n", target_ip, arp_table[i].mac);
return;
}
}

printf("IP address not found in ARP table.\n");


}

// Function to simulate RARP protocol


void RARP(struct ARP_Entry arp_table[], int table_size) {
char target_mac[18];
printf("Enter the MAC address to lookup: ");
scanf("%s", target_mac);

for (int i = 0; i < table_size; i++) {


if (strcmp(arp_table[i].mac, target_mac) == 0) {
printf("IP address for MAC %s is: %s\n", target_mac, arp_table[i].ip);

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

return;
}
}

printf("MAC address not found in ARP table.\n");


}

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;

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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;

if (SCNetworkCheckReachabilityByName(hostName, &flags) && flags > 0) {


NSLog(@"Host is reachable: %d", flags);
} else {
NSLog(@"Host is unreachable");
}

return 0;
}

//Variation 2:

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>

int main() {
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

bool success = false;


const char *host_name = [@"stackoverflow.com" cStringUsingEncoding:NSASCIIStringEncoding];

SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,


host_name);
SCNetworkReachabilityFlags flags;

success = SCNetworkReachabilityGetFlags(reachability, &flags);


bool isAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags &
kSCNetworkFlagsConnectionRequired);

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……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

#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>

#define size 100

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


struct sockaddr_in sock;
char *req, *hostname, *cp;
FILE *local;
char buff[size];
int con, l, nrv, sd;

if (argc != 3) {
printf("\nUsage: %s <server> <filename>\n", argv[0]);
exit(1);
}

if (cp = strchr(argv[1], '/')) {


*cp = '\0';
l = strlen(argv[1]);
hostname = malloc(l + 1);
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

strcpy(hostname, argv[1]);
*cp = '/';
l = strlen(cp);
req = malloc(l + 1);
strcpy(req, cp);
} else {
hostname = argv[1];
req = "/";
}

printf("\nHost = %s\nReq = %s\n", hostname, req);

sd = socket(AF_INET, SOCK_STREAM, 0);


if (sd < 0) {
perror("\nCannot open socket");
exit(1);
}

bzero(&sock, sizeof(sock));
sock.sin_family = AF_INET;

if (inet_pton(AF_INET, hostname, &sock.sin_addr) <= 0) {


struct hostent *hp;
if ((hp = gethostbyname(hostname)) == NULL) {
perror("gethostbyname");
exit(1);
}

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

bcopy(hp->h_addr_list[0], (char *)&sock.sin_addr, hp->h_length);


}

sock.sin_port = htons(80);
con = connect(sd, (struct sockaddr *)&sock, sizeof(sock));

if (con < 0) {
perror("\nConnection failed");
exit(1);
}

sprintf(buff, "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", req, hostname);


printf("Buff = %s\n", buff);
l = strlen(buff);

local = fopen(argv[2], "w");


if (!local) {
perror("fopen");
exit(1);
}

write(sd, buff, l);

do {
nrv = read(sd, buff, size);
if (nrv > 0) {
fwrite(buff, 1, nrv, local);

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

} else {
break;
}
} while (1);

close(sd);
fclose(local);
return 0;
}

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

Objective: Write a program to implement RPC (Remote Procedure Call)

//Server (RPC Server):


import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

// Define the remote interface


interface Calculator extends Remote {
int add(int a, int b) throws RemoteException;
}

// Implement the remote interface


class CalculatorImpl extends UnicastRemoteObject implements Calculator {
public CalculatorImpl() throws RemoteException {
super();
}

public int add(int a, int b) throws RemoteException {


return a + b;
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

}
}

// Server class
public class RMIServer {
public static void main(String[] args) {
try {
// Create the remote object
Calculator calculator = new CalculatorImpl();

// Bind the remote object to the registry


Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("CalculatorService", calculator);

System.out.println("RPC Server running...");


} catch (Exception e) {
System.err.println("RPC Server exception: " + e.toString());
e.printStackTrace();
}
}
}

//Client (RPC Client):

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

// 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");

// Invoke the remote method


int result = calculator.add(10, 20);
System.out.println("Result received from server: " + result);
} catch (Exception e) {
System.err.println("RPC Client exception: " + e.toString());
e.printStackTrace();
}
}
}

Output:

RPC Server running...


Result received from server: 30

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

Objective: Implementation of Subnetting.

dot-decimal address binary representation

Full IP Address 192.168.5.10 11000000.10101000.00000101.00001010


Subnet Mask 255.255.255.0 11111111.11111111.11111111.00000000

\\ and these bits to get the Network Address. Note, the prefix could be used
\\ instead of subnet mask - they represent the same thing.

Network Address 192.168.5.0 11000000.10101000.00000101.00000000

\\ Host and prefix


Host Portion 0.0.0.10 00000000.00000000.00000000.00001010
Prefix /24 11111111.11111111.11111111.00000000

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

Objective: Applications using TCP Sockets like

a) Echo Client and Echo Server:

//EchoClient.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class EchoClient {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));


BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

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;

public class EchoServer {

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

public static void main(String[] args) {


try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server started. Waiting for client...");

Socket clientSocket = serverSocket.accept();


System.out.println("Client connected.");

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);


BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));

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

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

CLIENT:

RESULT: Thus, the program was executed and output is verified successfully

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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;

public class ChatServer {


private static List<PrintWriter> clients = new ArrayList<>();

public static void main(String[] args) {


try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Chat Server started. Waiting for clients...");

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket);

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);


clients.add(out);

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

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) {

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

e.printStackTrace();
}
}

private void broadcast(String message) {


for (PrintWriter client : clients) {
client.println(message);
}
}
}
}
//ChatClient.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class ChatClient {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to chat server.");

BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));


BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

new Thread(() -> {


String inputLine;
try {
while ((inputLine = in.readLine()) != null) {
System.out.println("Server: " + inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
String inputLine;
while ((inputLine = userInput.readLine()) != null) {
out.println(inputLine);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT: SERVER & CLIENT

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

C. File Transfer

//FileServer.java:

import java.io.*;
import java.net.*;

public class FileServer {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("File Server started. Waiting for clients...");

while (true) {

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

Socket clientSocket = serverSocket.accept();


System.out.println("Client connected: " + clientSocket);

// Start a new thread to handle each client


new ClientHandler(clientSocket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

static class ClientHandler extends Thread {


private Socket clientSocket;

ClientHandler(Socket socket) {
this.clientSocket = socket;
}

public void run() {


try {
// Receive the file name from the client
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String fileName = in.readLine();
System.out.println("File requested: " + fileName);

// Read the file and send it to the client


File file = new File(fileName);
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

FileInputStream fis = new FileInputStream(file);


OutputStream os = clientSocket.getOutputStream();

byte[] buffer = new byte[4096];


int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}

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.*;

public class FileClient {


public static void main(String[] args) {

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

try {
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to file server.");

// Get the file name from the user


BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter file name: ");
String fileName = userInput.readLine();

// Send the file name to the server


PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(fileName);

// Receive the file from the server


InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("received_" + fileName);

byte[] buffer = new byte[4096];


int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}

fos.close();
socket.close();
System.out.println("File received successfully.");
} catch (IOException e) {

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

e.printStackTrace();
}
}
}

Objective: Applications using TCP and UDP Sockets like

d) DNS (UDP Socket):


//DNSClient.java:
import java.io.*;
import java.net.*;

public class DNSClient {


Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

public static void main(String[] args) {


try {
DatagramSocket socket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("8.8.8.8");
int serverPort = 53;
// Prepare DNS query message (example query for "example.com")
byte[] query = {
0x08, 0xbb, // Transaction ID
0x01, 0x00, // Flags
0x00, 0x01, // Questions
0x00, 0x00, // Answer RRs
0x00, 0x00, // Authority RRs
0x00, 0x00, // Additional RRs
(byte) 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
(byte) 0x03, 'c', 'o', 'm',
0x00, // Type
0x00, 0x01 // Class
};

// Send DNS query


DatagramPacket requestPacket = new DatagramPacket(query, query.length, serverAddress,
serverPort);
socket.send(requestPacket);
// Receive DNS response
byte[] responseBuffer = new byte[1024];
DatagramPacket responsePacket = new DatagramPacket(responseBuffer, responseBuffer.length);
socket.receive(responsePacket);
// Print DNS response
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

String response = new String(responsePacket.getData(), 0, responsePacket.getLength());


System.out.println("DNS Response:\n" + response);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//DNS Server (UDPServer.java):
import java.io.*;
import java.net.*;

public class UDPServer {


public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket(53); // DNS uses port 53
byte[] buffer = new byte[1024];
System.out.println("DNS Server started. Waiting for requests...");
while (true) {
DatagramPacket requestPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(requestPacket);
// Process DNS request
String request = new String(requestPacket.getData(), 0, requestPacket.getLength());
System.out.println("Received DNS request:\n" + request);
// Prepare DNS response (echo back the request for simplicity)
DatagramPacket responsePacket = new DatagramPacket(requestPacket.getData(),
requestPacket.getLength(),
requestPacket.getAddress(), requestPacket.getPort());
Faculty Name: sign with date
Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

socket.send(responsePacket);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
OUTPUT: SERVER & CLIENT

Objective: Study of Network simulator (NS).and Simulation of Congestion Control Algorithms


using NS

1. NS Overview:
 NS (Network Simulator) is a widely-used discrete event network simulator written in C++.

Faculty Name: sign with date


Experiment No…………………………………………. Date……………………………………………….

Name……………………………………………………….. Roll No………………………………………………

 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.

3. Simulation of Congestion Control Algorithms:


 Choose Algorithms: Select congestion control algorithms (e.g., TCP Tahoe, TCP Reno) for simulation.
 Implementation: Implement chosen algorithms in C, ensuring they interact correctly with the simulation
environment.
 Integration: Integrate the implemented algorithms into the NS simulation by modifying protocol
modules or creating custom modules in C.
 Evaluation and Analysis: Run simulations with different network conditions to evaluate algorithm
performance. Analyze simulation results to understand behavior under various scenarios.

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.

Faculty Name: sign with date

You might also like