Computer Network Practical Manual BCS653
Computer Network Practical Manual BCS653
import java.util.Scanner;
import java.util.Random;
if (ackReceived) {
System.out.println("ACK for Frame " + i + " received.\n");
} else {
System.out.println("ACK for Frame " + i + " lost. Resending Frame...\n");
i--; // resend same frame
}
}
int sent = 0;
System.out.println("\nSending frames from " + sent + " to " + (sent + framesInWindow - 1));
if (!ackReceived) {
System.out.println("ACK for Frame " + (sent + i) + " lost. Resending window...");
allAcked = false;
break;
} else {
System.out.println("ACK for Frame " + (sent + i) + " received.");
}
}
if (allAcked) {
sent += framesInWindow; // move window forward
}
// else: window remains at same place, resend
}
OUTPUT :-
3. Study of Socket Programming and Client – Server model
In short:
Socket = Door.
Socket Programming = Knocking, opening, sending, and receiving through that door.
In Client-Server, one side is called Server, and the other side is called Client.
SERVER CLIENT
Example:
socket.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client (connects to server)
import java.io.*;
import java.net.*;
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
How to run?
1. First run the Server.java program:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
switch (choice) {
case 1:
System.out.print("Enter IP Address: ");
String ip = scanner.nextLine();
arp(ip);
break;
case 2:
System.out.print("Enter MAC Address: ");
String mac = scanner.nextLine();
rarp(mac);
break;
case 3:
System.out.println("Exiting Simulation.");
break;
default:
System.out.println("Invalid choice. Try again.");
}
} while (choice != 3);
scanner.close();
}
}
OUTPUT :-
5. Write a code simulating PING commands
import java.net.*;
import java.io.*;
try {
// Ping the host
InetAddress inet = InetAddress.getByName(host);
if (reachable) {
System.out.println(host + " is reachable.");
} else {
System.out.println(host + " is not reachable.");
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
OUTPUT :-
6. Write a code simulating TRACEROUTE commands
import java.io.BufferedReader;
import java.io.InputStreamReader;
try {
System.out.println("Tracing route to " + host + "...\n");
OUTPUT :-
7. Applications using TCP Sockets like – Chat App
System.out.print("You: ");
msgToClient = userInput.readLine();
out.println(msgToClient);
}
socket.close();
serverSocket.close();
}
}
if (msgToServer.equalsIgnoreCase("exit")) {
System.out.println("Disconnected from server.");
break;
}
msgFromServer = in.readLine();
System.out.println("Server: " + msgFromServer);
}
socket.close();
}
}
OUTPUT :-
Server: Client:
8. Applications using TCP and UDP Sockets – DNS(Domain Name System)
import java.net.*;
import java.nio.ByteBuffer;
} catch (SocketTimeoutException e) {
System.out.println("DNS Server did not respond (Timeout)");
}
socket.close();
}
// Transaction ID
buffer.putShort((short) 0x1234);
OUTPUT :-