Lesson 10 of 15
TCP Client Simulation
TCP Client Simulation
A TCP client initiates a connection to a server, sends requests, and processes responses. This lesson simulates the client-side behavior of TCP communication including connection state management and data exchange.
Client Lifecycle
1. connect(host, port) — Initiate connection (three-way handshake)
2. send(data) — Send data to server
3. receive() — Read response from server
4. close() — Terminate connection (four-way teardown)
TCP Connection States (Client)
CLOSED -> SYN_SENT -> ESTABLISHED -> FIN_WAIT -> CLOSED
Simulated Network
We will simulate a network where the client connects to a server and exchanges messages. The server echoes back messages with a prefix.
Your Task
Implement a TCPClient class:
connect(host, port)— transitions to ESTABLISHED, returns handshake logsend(data)— queues data for sending, returns bytes queuedreceive(response)— stores a server response in the receive buffergetReceived()— returns all received data as an arrayclose()— transitions to CLOSED, returns teardown loggetState()— returns current connection state
Also implement simulateEcho(client, messages) that sends messages and simulates the server echoing them back with "ECHO:" prefix.
Node.js loading...
Loading...
Click "Run" to execute your code.