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

Java Networking Protocols Overview

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)
17 views7 pages

Java Networking Protocols Overview

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

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 < [Link];) {
[Link]("Sending frame " + i + ": " + data[i]);
[Link](500);
if ([Link]() < 0.9) {
[Link]("ACK received for frame " + i);
i++;
} else {
[Link]("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 < [Link]) {
while (nextSeqNum < base + windowSize && nextSeqNum < [Link]) {
[Link]("Sending frame " + nextSeqNum + ": " + data[nextSeqNum]);
nextSeqNum++;
}
[Link](1000);
if ([Link]() < 0.8) {
[Link]("ACK received for frame " + base);
base++;
} else {
[Link]("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 [Link].*;
import [Link].*;

public class Server {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5000);
Socket socket = [Link]();
DataInputStream in = new DataInputStream([Link]());
String msg = [Link]();
[Link]("Client says: " + msg);
[Link]();
}
}

// Simple TCP Client


import [Link].*;
import [Link].*;

public class Client {


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

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 [Link];
public class ARP {
public static void main(String[] args) {
HashMap<String, String> arpTable = new HashMap<>();
[Link]("[Link]", "00:0a:95:9d:68:16");
String ip = "[Link]";
[Link]("MAC Address for IP " + ip + " is " + [Link](ip));
}
}

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

// Simulate PING and Traceroute using Java


import [Link].*;
public class Ping {
public static void main(String[] args) throws IOException {
Process p = [Link]().exec("ping -c 4 [Link]");
BufferedReader input = new BufferedReader(new InputStreamReader([Link]()));
String line;
while ((line = [Link]()) != null) [Link](line);
[Link]();
}
}

public class Traceroute {


public static void main(String[] args) throws IOException {
Process p = [Link]().exec("traceroute [Link]");
BufferedReader input = new BufferedReader(new InputStreamReader([Link]()));
String line;
while ((line = [Link]()) != null) [Link](line);
[Link]();
}
}

Executed Output:
PING [Link] ...
64 bytes from [Link]: icmp_seq=1 ttl=118 time=18.3 ms
64 bytes from [Link]: icmp_seq=2 ttl=118 time=18.2 ms

Traceroute:
1 [Link] 2.345 ms
2 [Link] 3.567 ms
3 ***
4 [Link] 18.2 ms
Practical 5: HTTP Socket for Web Page Upload/Download

// HTTP GET request using Socket in Java


import [Link].*;
import [Link].*;

public class HttpGet {


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

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 = [Link](5, 3);
[Link]("Result from remote procedure: " + result);
}
}

Executed Output:
Result from remote procedure: 8

Common questions

Powered by AI

Using Java to simulate protocols like ARP/RARP and PING offers several advantages in educational or experimental environments. Java's platform independence allows simulations to run easily on various systems without modification. Its extensive libraries provide tools for network operations and data handling, simplifying the implementation of complex protocols. Moreover, Java's object-oriented nature aids in organizing and managing protocol logic effectively. These features make Java a suitable choice for creating accurate simulations that are accessible for learning and experimentation .

Correct implementation of data streams in a TCP server is critical to ensure accurate communication and data integrity between the server and clients. Streams manage data flow, allowing data to be read from or written to a network socket efficiently. Improper handling of these streams could result in incomplete data transfers, data corruption, or difficulties in synchronizing message boundaries. This could lead to faulty applications, decreased performance, and security vulnerabilities, ultimately affecting user experience and operational efficiency .

Executing an HTTP GET request using Java sockets involves creating a Socket object connected to the web server on port 80, building an HTTP GET request string, and then writing this request to the output stream associated with the socket. The server response is read from the input stream. This approach demonstrates fundamental HTTP concepts such as establishing a connection, sending a request method, receiving a response, and managing socket connections, embodying key network concepts like client-server communication and protocol adherence .

The Simple TCP Client initiates a connection to a server by creating a Socket object specifying the server's hostname and port. Once connected, it uses output streams to send data, such as a text message, to the server. This process is crucial for TCP/IP communication as it establishes a reliable, ordered, and error-checked delivery of a byte stream between applications running on hosts communicating over an IP network, ensuring both message integrity and data coherence .

RPC enables programs to call functions or procedures on remote servers as if they were local calls, abstracting the complexities of network communication. This benefits distributed computing by facilitating the integration of distributed resources, allowing developers to build scalable systems without managing explicit message exchanges. Compared to traditional local method calls, RPC requires handling potential network issues, but it significantly simplifies remote interactions and resource sharing, enhancing modular software development for distributed environments .

In the Stop-and-Wait protocol, the sender transmits a data frame and then waits for an acknowledgment (ACK) from the receiver before sending the next frame. If an ACK is not received, the sender assumes there was an error or a timeout and resends the frame. This method ensures reliability but can lead to inefficiency, particularly if network latency is high, as the sender spends a significant amount of time waiting, leading to a lower utilization of the available bandwidth .

PING is used to verify the reachability of a host and measure the round-trip time for messages sent from the originating host to a destination computer. It relies on ICMP Echo Request and Echo Reply messages. Traceroute, on the other hand, determines the path packets take to reach a network host, displaying each hop along the way and the time taken for each hop. In Java simulation, PING sends ICMP echo requests and receives echo replies, outputting the response times, whereas Traceroute sends sequences of packets with incrementally increasing TTL (Time to Live) values to identify each route hop, displaying the IP address and time of each hop .

The primary difference is that the Stop-and-Wait protocol sends one frame at a time and waits for an acknowledgment before proceeding, which can lead to inefficiencies in high-latency networks. In contrast, the Sliding Window (Go-Back-N) protocol can send multiple frames up to a window size without receiving an ACK, thus increasing throughput. When an error is detected, the Sliding Window protocol will resend from the last acknowledged frame, potentially resending multiple frames, whereas Stop-and-Wait only resends a single frame .

The ARP protocol resolves IPv4 addresses into MAC addresses, facilitating network communication. Analogous to a Java HashMap, ARP uses the IP address as a key to find the associated MAC address, the value in the mapping. When a device wants to communicate, it sends a broadcast message in the network to request the MAC address corresponding to a specific IP. The device with the matching IP replies with its MAC address, which can then be used for direct communication. This process ensures efficient address resolution similar to retrieving a value using a key in a HashMap .

In TCP socket programming, a server creates a ServerSocket object which listens for incoming connection requests on a specified port. Upon receiving a request, the server accepts the connection using the accept() method, creating a new Socket object for the communication. The server then reads data sent by the client using input streams and responds accordingly. Once the message exchange is completed, both the client and server close their respective sockets to terminate the connection .

You might also like