Learn › Part 1 ยท Networking foundations

TCP, UDP, sockets and CRC

Ports and sockets, TCP header, sequence and acknowledgement arithmetic, handshake and teardown, UDP, the C socket API, CRC and NS-2.

Role of the transport layer

IP delivers host to host; the transport layer delivers process to process using 16-bit ports (0–65,535) and multiplexing/demultiplexing.

RangeNameExamples
0–1,023Well-knownFTP 21, SSH 22, Telnet 23, SMTP 25, DNS 53, HTTP 80, POP3 110, HTTPS 443
1,024–49,151RegisteredVendor services, e.g. 8080
49,152–65,535Dynamic / ephemeralClient-side ports

A socket = IP address + port (32 + 16 = 48 bits for IPv4). A TCP connection is identified by both sockets: the 4-tuple (source IP, source port, destination IP, destination port).

TCP essentials

Connection-oriented, reliable, ordered byte stream with flow control (receiver's advertised window) and congestion control (network capacity). Header: 20 bytes minimum, up to 60 with options. Flags: URG, ACK, PSH, RST, SYN, FIN.

Seq = number of the first data byte in the segment. ACK = next byte expected = last byte received + 1 = Seq + data length.

SYN and FIN each consume one sequence number; a pure ACK consumes none. ACKs are cumulative.

TCP data = IP total length − IP header − TCP header.

Worked examples

  • Seq 10,000 with 1,200 bytes ⇒ ACK 11,200.
  • IP total 1,500, IP header 20, TCP header 20 ⇒ 1,460 data bytes; Seq 20,000 ⇒ ACK 21,460.
  • Handshake: client SYN Seq 5,000; server SYN-ACK Seq 8,000, Ack 5,001; client ACK Seq 5,001, Ack 8,001.
  • Segments 1,001, 2,001, 3,001 (1,000 B each), the second lost: receiver keeps sending ACK 2,001 (duplicate ACKs). Three duplicates trigger fast retransmit.
  • Teardown: client FIN Seq 722 ⇒ server ACK 723; server FIN Seq 2,201 ⇒ client ACK 2,202.

Flag combinations: SYN=1/ACK=0 opens a connection; SYN=1/ACK=1 is the reply; SYN=0/ACK=1 is every later segment. SYN=0/ACK=0 does not occur in normal flow, but it does appear in some RST segments and in NULL/FIN/Xmas scan probes.

UDP

Connectionless and message-oriented, with an 8-byte header (source port, destination port, length, checksum). It suits DNS, streaming, VoIP and broadcast/multicast. Largest IPv4 UDP payload = 65,535 − 20 − 8 = 65,507 bytes.

Socket programming in C

/* TCP server */
int s = socket(AF_INET, SOCK_STREAM, 0);      /* SOCK_DGRAM for UDP */
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;            /* any local interface */
addr.sin_port = htons(8080);                  /* host to network byte order */
bind(s, (struct sockaddr *)&addr, sizeof addr);
listen(s, 3);                                 /* passive socket, queue of 3 */
int c = accept(s, (struct sockaddr *)&cli, &len); /* new data socket */
while ((n = read(c, buf, MAX)) > 0) fwrite(buf, 1, n, fp);
close(c); close(s);

/* TCP client */
int s = socket(AF_INET, SOCK_STREAM, 0);
inet_pton(AF_INET, "127.0.0.1", &srv.sin_addr);
connect(s, (struct sockaddr *)&srv, sizeof srv);  /* starts the handshake */
while ((n = fread(buf, 1, MAX, fp)) > 0) send(s, buf, n, 0);
  • One listening socket per service never carries data; accept() returns a new data socket per client.
  • recv()/read() return bytes read, 0 when the peer closes, −1 on error.
  • UDP servers use socket, bind, recvfrom, sendto; there is no listen/accept.
  • Concurrency: fork() after accept() (child closes the listener; parent closes the data socket and reaps children with waitpid(-1, NULL, WNOHANG)), or threads.
  • When the listen queue is full, Linux ignores new SYNs and clients retransmit; requests are not refused at once.
  • Because TCP moves raw bytes, the file-transfer server needs no knowledge of PDF or JPEG formats.

CRC (cyclic redundancy check)

CRC detects accidental bit errors using modulo-2 (XOR) division by a generator polynomial shared by both ends. It offers no protection against deliberate tampering, because anyone can recompute it.

  1. Append (degree of generator) zeros to the data.
  2. Divide modulo 2 by the generator; keep the remainder.
  3. Replace the zeros with the remainder to form the codeword.
  4. The receiver divides the codeword; remainder 0 ⇒ accept, otherwise error detected.

Worked example

Data 1001, generator 1011 (degree 3): divide 1001000 by 1011 to get remainder 110, so the codeword is 1001110. Dividing 1001110 by 1011 leaves 000.

Ethernet uses CRC-32 in the FCS, computed in hardware with shift registers.

NS-2 in one paragraph

NS-2 has a C++ core (scheduler, queues such as DropTail and RED) and OTcl scripts for topologies. A script creates the simulator, opens trace files, builds nodes and links ($ns duplex-link $n0 $n1 1Mb 10ms DropTail), attaches agents (TCP–TCPSink, UDP–Null) and traffic (FTP, CBR), schedules events and runs. Trace events: + enqueue, - dequeue, r receive, d drop.

Common pitfalls

  • Reading from the listening socket instead of the accepted one.
  • Forgetting that SYN uses up a sequence number.
  • Subtracting a 20-byte TCP header when options make it longer.