Skip to main content

Overview

The ReliableClientProtocol class provides client-side implementation of the Selective Repeat ARQ protocol over UDP. It handles connection establishment via three-way handshake, reliable data transmission with windowing, and automatic retransmission of lost packets.

Connection Establishment

connection()

Establishes a three-way handshake connection with the server.
socket
DatagramSocket
required
The UDP socket to use for communication
serverAddr
InetSocketAddress
required
The server’s address and port number
port
int
The server’s data port assigned for this connection
DatagramSocket socket = new DatagramSocket();
InetSocketAddress serverAddress = new InetSocketAddress("127.0.0.1", 8000);
int dataPort = ReliableClientProtocol.connection(socket, serverAddress);
The connection process implements a three-way handshake:
  1. Client sends SYN packet (type=2)
  2. Server responds with SYN-ACK (type=3)
  3. Client sends ACK (type=1)

Data Transmission

requestresponse()

Sends data reliably to the server and waits for a response using the Selective Repeat protocol.
socket
DatagramSocket
required
The UDP socket for communication
serverAddr
InetSocketAddress
required
The server’s data port address
data
String
required
The data payload to send to the server
response
String
The complete response received from the server
String httpRequest = "GET /index.html HTTP/1.0\r\n\r\n";
String response = ReliableClientProtocol.requestresponse(
    socket, 
    new InetSocketAddress("127.0.0.1", dataPort), 
    httpRequest
);
The method automatically:
  • Fragments data into 1013-byte chunks
  • Sends the total packet count first
  • Transmits packets using a sliding window (size: 100)
  • Handles ACKs and retransmits on timeout or NACK
  • Receives the server’s response using the same protocol

Internal Methods

sendreq()

Handles the sliding window transmission logic with selective repeat.
nofpackets
int
required
Total number of data packets to send
serverAddr
InetSocketAddress
required
Destination server address
socket
DatagramSocket
required
Socket for transmission
chunks
List<byte[]>
required
List of data chunks to send
This is an internal method called by requestresponse(). Direct use is not recommended.

isAcked()

Timeout handler that checks if a packet has been acknowledged and retransmits if necessary.
seq
int
required
Sequence number of the packet to check
socket
DatagramSocket
required
Socket for retransmission
retries
int
required
Current retry attempt count
Packets are retransmitted up to 10 times before being abandoned.

response()

Waits for and receives the complete response from the server.
response
String
The assembled response data from all received packets

getRequestFromPacket()

Receives and assembles packets using the Selective Repeat protocol.
socket
DatagramSocket
required
Socket to receive packets from
totalPackets
int
required
Expected number of packets to receive
request
String
The complete assembled data from all packets

Protocol Constants

static int lastack = 0;           // Last acknowledged packet sequence number
static int window = 100;          // Sliding window size
static InetSocketAddress routerAddr = 
    new InetSocketAddress("127.0.0.1", 3000);  // Router address

Error Handling

When waiting for ACKs, the socket uses a 50ms timeout. On timeout, the size packet is retransmitted.
The protocol handles packet loss through:
  • Cumulative ACKs for in-order packets
  • NACKs (type=4) for out-of-order packets
  • Timeout-based retransmission with exponential backoff
When a packet with type=6 is received, the connection is closed gracefully.

Example Usage

import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import client.ReliableClientProtocol;

public class Example {
    public static void main(String[] args) {
        try {
            // Create socket
            DatagramSocket socket = new DatagramSocket();
            
            // Establish connection
            InetSocketAddress serverAddress = 
                new InetSocketAddress("192.168.1.100", 8000);
            int dataPort = ReliableClientProtocol.connection(
                socket, serverAddress
            );
            
            // Send request and get response
            String request = "GET /data HTTP/1.0\r\n\r\n";
            String response = ReliableClientProtocol.requestresponse(
                socket,
                new InetSocketAddress("192.168.1.100", dataPort),
                request
            );
            
            System.out.println("Response: " + response);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

See Also

ServerTcpProtocl

Server-side protocol implementation

Packet Format

Packet structure and builder pattern

Build docs developers (and LLMs) love