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