Overview
TheServerTcpProtocl class implements the server-side Selective Repeat ARQ protocol over UDP. It handles multiple concurrent client connections, performs three-way handshakes, receives HTTP requests reliably, and sends HTTP responses back to clients.
Constructor
ServerTcpProtocl()
Initializes the server and starts listening for client connections.Port number to listen on for incoming connections
Root directory for serving files
Enable verbose logging output
The constructor automatically calls
handShake() to begin accepting client connections.Connection Handling
handShake()
Listens for and handles three-way handshake connections from clients.Handshake Process
Handshake Process
- Server receives SYN packet (type=2) from client
- Creates or retrieves a dedicated handler socket for the client
- Sends SYN-ACK (type=3) with the handler’s port number
- Waits for ACK (type=1) with 20ms timeout
- Spawns a new thread running
clientHandlerfor this client
Client Handler
clientHandler (Inner Class)
Handles communication with a single client in a dedicated thread.Dedicated socket for this client
Client’s IP address
Request Processing Flow
Request Processing Flow
- Wait for initial packet containing total packet count
- Send ACK for the count packet
- Receive all data packets using Selective Repeat
- Parse HTTP request (GET or POST)
- Generate HTTP response using
HttpcLib - Send response back using Selective Repeat
- Remove client from socket mapping
Data Transmission
requestresponse()
Sends the HTTP response to the client using reliable transmission.Socket for sending data
Client’s address and port
Router address for packet forwarding
HTTP response data to send
Data is automatically chunked into 1013-byte packets and transmitted using a sliding window protocol.
sendreq()
Implements the sliding window transmission with selective repeat.Total number of packets to send
Destination client address
Router address
Socket for transmission
Data chunks to send
Window Management
Window Management
- Initial window size: 100 packets
- Sends window-sized batch initially
- On receiving ACK, slides window forward
- On receiving NACK (type=4), retransmits specific packet
- Each packet has its own timeout timer thread
isAcked()
Retransmission handler invoked on packet timeout.Sequence number to check
Socket for retransmission
Current retry count
Data Reception
getRequestFromPacket()
Receives and assembles all packets from the client using Selective Repeat.Socket to receive from
Expected number of packets
Complete assembled HTTP request
Reception Strategy
Reception Strategy
- Maintains a sliding window (initial size: 100)
- Buffers out-of-order packets in a HashMap
- Sends cumulative ACKs for in-order packets
- Sends NACKs for detected gaps
- Sends duplicate ACKs for already-received packets
Protocol Variables
HTTP Request Handling
The server integrates withHttpcLib to process HTTP requests:
Verbose Logging
Whenverbose is enabled, the server logs:
- Waiting for connection request
- Client handler creation
- Handler waiting for packets
- Request received notification
- Response creation and sending progress
- ACK reception status
Example Usage
Error Handling
Socket Timeout
Socket Timeout
30ms timeout on ACK reception. On timeout, retransmits the size packet or continues waiting.
Connection Closure
Connection Closure
When receiving a packet with type=6, the handler terminates and removes the client from the mapping.
Handler Errors
Handler Errors
Runtime exceptions in handlers are caught and logged, and the client is removed from the mapping.
Thread Safety
Performance Characteristics
See Also
ReliableClientProtocol
Client-side protocol implementation
Packet Format
Packet structure and builder pattern