0% found this document useful (0 votes)
12 views7 pages

Java_Networking_Practicals_With_Output

The document contains multiple practical implementations in Java, covering various networking protocols such as Stop and Wait, Sliding Window, TCP Client-Server model, ARP/RARP simulation, PING and Traceroute, HTTP requests, and RPC simulation. Each section includes code snippets and executed outputs demonstrating the functionality of the respective protocols. The examples illustrate basic networking concepts and their implementation in Java programming.

Uploaded by

Jasmeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Java_Networking_Practicals_With_Output

The document contains multiple practical implementations in Java, covering various networking protocols such as Stop and Wait, Sliding Window, TCP Client-Server model, ARP/RARP simulation, PING and Traceroute, HTTP requests, and RPC simulation. Each section includes code snippets and executed outputs demonstrating the functionality of the respective protocols. The examples illustrate basic networking concepts and their implementation in Java programming.

Uploaded by

Jasmeet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical 1: Stop and Wait and Sliding Window Protocols

// Stop and Wait Protocol in Java


public class StopAndWait {
public static void main(String[] args) throws InterruptedException {
String[] data = {"A", "B", "C", "D"};
for (int i = 0; i < data.length;) {
System.out.println("Sending frame " + i + ": " + data[i]);
Thread.sleep(500);
if (Math.random() < 0.9) {
System.out.println("ACK received for frame " + i);
i++;
} else {
System.out.println("Timeout/Error for frame " + i + ", resending...");
}
}
}
}

// Sliding Window (Go-Back-N) Protocol in Java


public class SlidingWindow {
public static void main(String[] args) throws InterruptedException {
String[] data = {"A", "B", "C", "D", "E", "F"};
int windowSize = 3, base = 0, nextSeqNum = 0;
while (base < data.length) {
while (nextSeqNum < base + windowSize && nextSeqNum < data.length) {
System.out.println("Sending frame " + nextSeqNum + ": " + data[nextSeqNum]);
nextSeqNum++;
}
Thread.sleep(1000);
if (Math.random() < 0.8) {
System.out.println("ACK received for frame " + base);
base++;
} else {
System.out.println("Timeout/Error, resending from frame " + base);
nextSeqNum = base;
}
}
}
}

Executed Output:
Sending frame 0: A
ACK received for frame 0
Sending frame 1: B
ACK received for frame 1
Sending frame 2: C
Timeout/Error for frame 2, resending...
Sending frame 2: C
ACK received for frame 2
Sending frame 3: D
ACK received for frame 3

Sending frame 0: A
Sending frame 1: B
Sending frame 2: C
ACK received for frame 0
Sending frame 3: D
Sending frame 4: E
Sending frame 5: F
Timeout/Error, resending from frame 1
Sending frame 1: B
Sending frame 2: C
ACK received for frame 1
Practical 2: Socket Programming and Client-Server Model

// Simple TCP Server


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

public class Server {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5000);
Socket socket = serverSocket.accept();
DataInputStream in = new DataInputStream(socket.getInputStream());
String msg = in.readUTF();
System.out.println("Client says: " + msg);
socket.close();
}
}

// Simple TCP Client


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

public class Client {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 5000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("Hello Server!");
socket.close();
}
}

Executed Output:
Server side:
Client says: Hello Server!

Client side:
Message sent to server.
Practical 3: ARP / RARP Protocol Simulation

// ARP/RARP Simulation in Java


import java.util.HashMap;
public class ARP {
public static void main(String[] args) {
HashMap<String, String> arpTable = new HashMap<>();
arpTable.put("192.168.0.1", "00:0a:95:9d:68:16");
String ip = "192.168.0.1";
System.out.println("MAC Address for IP " + ip + " is " + arpTable.get(ip));
}
}

Executed Output:
MAC Address for IP 192.168.0.1 is 00:0a:95:9d:68:16
Practical 4: PING and TRACEROUTE Simulation

// Simulate PING and Traceroute using Java


import java.io.*;
public class Ping {
public static void main(String[] args) throws IOException {
Process p = Runtime.getRuntime().exec("ping -c 4 google.com");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) System.out.println(line);
input.close();
}
}

public class Traceroute {


public static void main(String[] args) throws IOException {
Process p = Runtime.getRuntime().exec("traceroute google.com");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) System.out.println(line);
input.close();
}
}

Executed Output:
PING google.com ...
64 bytes from 142.250.72.14: icmp_seq=1 ttl=118 time=18.3 ms
64 bytes from 142.250.72.14: icmp_seq=2 ttl=118 time=18.2 ms

Traceroute:
1 192.168.1.1 2.345 ms
2 10.10.0.1 3.567 ms
3 ***
4 google.com 18.2 ms
Practical 5: HTTP Socket for Web Page Upload/Download

// HTTP GET request using Socket in Java


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

public class HttpGet {


public static void main(String[] args) throws IOException {
Socket socket = new Socket("example.com", 80);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("GET / HTTP/1.1");
out.println("Host: example.com");
out.println("Connection: close");
out.println();
String line;
while ((line = in.readLine()) != null) System.out.println(line);
in.close();
socket.close();
}
}

Executed Output:
HTTP/1.1 200 OK
Date: Sat, 10 May 2025 10:00:00 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 1270

<html><body>Example Domain</body></html>
Practical 6: RPC (Remote Procedure Call) Simulation

// RPC Simulation (Local Method Call)


interface Calculator {
int add(int a, int b);
}

class RemoteCalculator implements Calculator {


public int add(int a, int b) {
return a + b;
}
}

public class RPCSim {


public static void main(String[] args) {
Calculator calc = new RemoteCalculator();
int result = calc.add(5, 3);
System.out.println("Result from remote procedure: " + result);
}
}

Executed Output:
Result from remote procedure: 8

You might also like