1. TCP Server–Client Program - Server.
java
import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(5000);
[Link]("Server waiting for connection...");
Socket s = [Link]();
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
String msg = [Link]();
[Link]("Received: " + msg);
[Link]();
[Link]();
[Link]();
}
}
1. TCP Server–Client Program - [Link]
import [Link].*;
import [Link].*;
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 5000);
PrintWriter out = new PrintWriter([Link](), true);
[Link]("hello_client");
[Link]();
}
}
2. UDP Echo Server - [Link]
import [Link].*;
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(5000);
byte[] buf = new byte[1024];
[Link]("UDP Server waiting...");
DatagramPacket dp = new DatagramPacket(buf, [Link]);
[Link](dp);
String msg = new String([Link](), 0, [Link]());
[Link]("Received: " + msg);
InetAddress ip = [Link]();
int port = [Link]();
dp = new DatagramPacket([Link](), [Link](), ip, port);
[Link](dp);
[Link]();
}
}
2. UDP Echo Client - [Link]
import [Link].*;
import [Link].*;
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
InetAddress ip = [Link]("localhost");
Scanner sc = new Scanner([Link]);
[Link]("Enter message: ");
String msg = [Link]();
DatagramPacket dp = new DatagramPacket([Link](), [Link](), ip, 5000);
[Link](dp);
byte[] buf = new byte[1024];
dp = new DatagramPacket(buf, [Link]);
[Link](dp);
[Link]("Echo from Server: " + new String([Link](), 0, [Link]
[Link]();
[Link]();
}
}
3. TCP Chat Server - [Link]
import [Link].*;
import [Link].*;
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5000);
Socket s = [Link]();
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
String msg;
while (true) {
msg = [Link]();
if (msg == null || [Link]("bye")) break;
[Link]("Client: " + msg);
[Link]("Server: ");
[Link]([Link]());
}
[Link]();
[Link]();
}
}
3. TCP Chat Client - [Link]
import [Link].*;
import [Link].*;
public class ChatClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 5000);
BufferedReader in = new BufferedReader(new InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
String msg;
while (true) {
[Link]("Client: ");
msg = [Link]();
[Link](msg);
if ([Link]("bye")) break;
[Link]("Server: " + [Link]());
}
[Link]();
}
}
4. DNS Server - [Link]
import [Link].*;
import [Link].*;
public class DNSServer {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(5000);
Map<String, String> dns = new HashMap<>();
[Link]("[Link]", "[Link]");
[Link]("[Link]", "[Link]");
byte[] buf = new byte[1024];
[Link]("DNS Server running...");
while (true) {
DatagramPacket dp = new DatagramPacket(buf, [Link]);
[Link](dp);
String domain = new String([Link](), 0, [Link]()).trim();
String ip = [Link](domain, "Not found");
InetAddress ipaddr = [Link]();
int port = [Link]();
dp = new DatagramPacket([Link](), [Link](), ipaddr, port);
[Link](dp);
}
}
}
4. DNS Client - [Link]
import [Link].*;
import [Link].*;
public class DNSClient {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
InetAddress ip = [Link]("localhost");
Scanner sc = new Scanner([Link]);
[Link]("Enter domain name: ");
String domain = [Link]();
DatagramPacket dp = new DatagramPacket([Link](), [Link](), ip, 500
[Link](dp);
byte[] buf = new byte[1024];
dp = new DatagramPacket(buf, [Link]);
[Link](dp);
[Link]("IP Address: " + new String([Link](), 0, [Link]()));
[Link]();
[Link]();
}
}
5. File Transfer Server - [Link]
import [Link].*;
import [Link].*;
public class FileServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(6000);
Socket s = [Link]();
FileInputStream fin = new FileInputStream("[Link]");
OutputStream out = [Link]();
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = [Link](buffer)) != -1) {
[Link](buffer, 0, bytes);
}
[Link]();
[Link]();
[Link]();
[Link]();
[Link]("File sent successfully.");
}
}
5. File Transfer Client - [Link]
import [Link].*;
import [Link].*;
public class FileClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 6000);
InputStream in = [Link]();
FileOutputStream fout = new FileOutputStream("[Link]");
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = [Link](buffer)) != -1) {
[Link](buffer, 0, bytes);
}
[Link]();
[Link]();
[Link]();
[Link]("File received successfully.");
}
}
6. ARP Protocol Simulation - [Link]
import [Link].*;
public class ARP {
public static void main(String[] args) {
Map<String, String> table = new HashMap<>();
[Link]("[Link]", "AA:BB:CC:DD:EE:01");
[Link]("[Link]", "AA:BB:CC:DD:EE:02");
Scanner sc = new Scanner([Link]);
[Link]("Enter IP address: ");
String ip = [Link]();
String mac = [Link](ip, "Not Found");
[Link]("MAC Address: " + mac);
[Link]();
}
}
7. RARP Server - [Link]
import [Link].*;
import [Link].*;
public class RARPServer {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(6000);
Map<String, String> table = new HashMap<>();
[Link]("AA:BB:CC:DD:EE:01", "[Link]");
[Link]("AA:BB:CC:DD:EE:02", "[Link]");
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, [Link]);
[Link](dp);
String mac = new String([Link](), 0, [Link]()).trim();
String ip = [Link](mac, "Not Found");
dp = new DatagramPacket([Link](), [Link](), [Link](), [Link]())
[Link](dp);
[Link]();
}
}
7. RARP Client - [Link]
import [Link].*;
import [Link].*;
public class RARPClient {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
InetAddress ip = [Link]("localhost");
Scanner sc = new Scanner([Link]);
[Link]("Enter MAC address: ");
String mac = [Link]();
DatagramPacket dp = new DatagramPacket([Link](), [Link](), ip, 6000);
[Link](dp);
byte[] buf = new byte[1024];
dp = new DatagramPacket(buf, [Link]);
[Link](dp);
[Link]("IP Address: " + new String([Link](), 0, [Link]()));
[Link]();
[Link]();
}
}
8. Congestion Control Simulation - [Link]
public class CongestionControl {
public static void main(String[] args) {
int cwnd = 1;
int ssthresh = 8;
for (int i = 1; i <= 12; i++) {
[Link]("Round " + i + ": cwnd = " + cwnd);
if (cwnd < ssthresh) cwnd *= 2; // slow start
else cwnd += 1; // congestion avoidance
if (i == 10) { // simulate congestion
[Link]("Packet loss! Reducing window.");
ssthresh = cwnd / 2;
cwnd = 1;
}
}
}
}
9. Distance Vector Routing - [Link]
import [Link].*;
public class DistanceVector {
public static void main(String[] args) {
int INF = 999;
int[][] cost = {
{0, 2, INF, 1},
{2, 0, 3, 2},
{INF, 3, 0, 4},
{1, 2, 4, 0}
};
int n = 4;
for (int i = 0; i < n; i++) {
[Link]("Router " + i + " table: ");
for (int j = 0; j < n; j++)
[Link](cost[i][j] + " ");
[Link]();
}
}
}
10. Link State Routing - [Link]
import [Link].*;
public class LinkState {
public static void main(String[] args) {
int n = 4;
int[][] graph = {
{0, 1, 3, 0},
{1, 0, 1, 4},
{3, 1, 0, 2},
{0, 4, 2, 0}
};
int start = 0;
int[] dist = new int[n];
boolean[] visited = new boolean[n];
[Link](dist, 999);
dist[start] = 0;
for (int i = 0; i < n - 1; i++) {
int u = -1;
int min = 999;
for (int j = 0; j < n; j++)
if (!visited[j] && dist[j] < min) { min = dist[j]; u = j; }
visited[u] = true;
for (int v = 0; v < n; v++)
if (graph[u][v] != 0 && dist[v] > dist[u] + graph[u][v])
dist[v] = dist[u] + graph[u][v];
}
[Link]("Shortest paths from node 0:");
for (int i = 0; i < n; i++)
[Link]("To " + i + " = " + dist[i]);
}
}
11. CSMA/CD Simulation - [Link]
public class CSMACD {
public static void main(String[] args) {
int stations = 4;
for (int i = 1; i <= stations; i++) {
[Link]("Station " + i + " sensing channel...");
if (i == 2) [Link]("Collision detected! Backoff...");
else [Link]("Transmission successful.");
}
}
}
12. Stop and Wait Protocol - [Link]
import [Link].*;
public class StopAndWait {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of frames: ");
int n = [Link]();
for (int i = 0; i < n; i++) {
[Link]("Frame " + i + " sent.");
[Link]("ACK for frame " + i + " received.");
}
[Link]("All frames sent successfully.");
[Link]();
}
}
13. Four Node Network Simulation - [Link]
public class FourNodeNetwork {
public static void main(String[] args) {
String[] nodes = {"n0", "n1", "n2", "n3"};
[Link]("Links: n0-n2, n1-n2, n2-n3");
[Link]("TCP between n0-n3, UDP between n1-n3");
for (String node : nodes)
[Link]("Packets sent from " + node);
}
}