Skip to content

Network Programming Fundamentals

Network programming is the technology that enables programs on different hosts to communicate with each other over a network. Understanding the underlying TCP/IP protocol and the Socket abstraction layer is the foundation for writing network programs.

Architecture Patterns

  • B/S (Browser/Server): The client is a browser that communicates with the server over HTTP/HTTPS. No client installation is required, but functionality is limited by the browser.
  • C/S (Client/Server): A dedicated client communicates with the server. Protocols can be customized for richer functionality, but a client must be installed.

The OSI Seven-Layer Model

The OSI (Open Systems Interconnection) reference model divides network communication into seven abstract layers, each with a clearly defined responsibility:

LayerNameProtocol / Function ExamplesData Unit
7ApplicationHTTP, FTP, SMTP, DNSData
6PresentationEncryption, compression, encoding conversionData
5SessionSession establishment and managementData
4TransportTCP, UDPSegment
3NetworkIP, ARP, ICMPPacket
2Data LinkEthernet, MAC addressFrame
1PhysicalCables, fiber optics, wireless signalsBit
  • When sending, data is encapsulated top-down through each layer (headers are added at each layer).
  • When receiving, data is de-encapsulated bottom-up through each layer.

The TCP/IP Protocol Suite

The actual Internet uses the TCP/IP protocol suite (a four-layer model):

TCP/IP LayerCorresponding OSI LayersKey Protocols
ApplicationLayers 5–7HTTP, HTTPS, FTP, DNS, SMTP
TransportLayer 4TCP, UDP
NetworkLayer 3IP, ARP, ICMP
Network InterfaceLayers 1–2Ethernet, Wi-Fi

IP Addresses and Ports

  • IP Address: A 32-bit binary number (IPv4) that identifies the location of a host on the network. Format: 192.168.1.100.
    • 127.0.0.1: The loopback address, used for local self-testing.
    • 0.0.0.0: Listen on all network interfaces.
    • 255.255.255.255: Broadcast address.
  • Port Number: 0–65535, identifies a specific process or service on a host.
    • Well-known ports (0–1023): HTTP 80, HTTPS 443, SSH 22, FTP 21, SMTP 25.
    • Dynamic ports (49152–65535): Randomly assigned by the client.

TCP vs UDP

FeatureTCPUDP
ConnectionConnection-oriented (three-way handshake)Connectionless
ReliabilityGuaranteed order, no lossNot guaranteed
SpeedSlower (acknowledgement mechanism)Faster
Use CasesWeb, file transfer, emailVideo streaming, DNS, gaming

TCP Three-Way Handshake and Four-Way Termination

Three-Way Handshake (Establishing a Connection)

Client                        Server
   |   ---SYN(seq=x)--->      |    Step 1: Client sends SYN, enters SYN_SENT
   |  <--SYN+ACK(seq=y,ack=x+1)--  |   Step 2: Server sends SYN+ACK, enters SYN_RECV
   |   ---ACK(ack=y+1)--->    |    Step 3: Client sends ACK, both enter ESTABLISHED

Why three steps? Two handshakes cannot confirm the client’s receive capability; three handshakes allow both sides to verify that communication is working.

Four-Way Termination (Closing a Connection)

Active Closer                  Passive Closer
   |   ---FIN--->              |    Step 1: Sends FIN, enters FIN_WAIT_1
   |   <--ACK---               |    Step 2: Replies ACK, enters CLOSE_WAIT
   |   <--FIN---               |    Step 3: Sends FIN, enters LAST_ACK
   |   ---ACK--->              |    Step 4: Replies ACK, waits 2MSL then closes

Why four steps? TCP is full-duplex; closing each direction requires its own FIN+ACK exchange.

The Socket Abstraction Layer

A Socket is the network programming interface provided by the operating system, hiding the low-level TCP/IP details from applications.

  • AF_INET: IPv4 network socket (most common)

  • AF_INET6: IPv6 network socket

  • AF_UNIX: Unix domain socket, used for inter-process communication on the same machine

  • SOCK_STREAM: TCP socket (connection-oriented, reliable)

  • SOCK_DGRAM: UDP socket (connectionless, fast)

Quick Port Reference

import socket

# Look up port by service name
print(socket.getservbyname("http"))    # 80
print(socket.getservbyname("https"))   # 443
print(socket.getservbyname("ssh"))     # 22

# Look up service name by port
print(socket.getservbyport(80))        # "http"

# Get local machine IP
print(socket.gethostbyname(socket.gethostname()))

The next section, Socket Programming, introduces how to implement TCP/UDP communication using Python’s socket module.

Last updated on