Fundamentals 25 min read

Unlock Linux Networking: From Basics to Socket Programming

This article introduces Linux networking fundamentals, covering LAN and WAN concepts, the purpose and structure of network protocols, OSI and TCP/IP layered models, MAC and IP addressing, and provides a practical guide to socket programming with ports, address families, and common API calls.

Raymond Ops
Raymond Ops
Raymond Ops
Unlock Linux Networking: From Basics to Socket Programming

Linux Networking Basics: From Fundamentals to Socket Programming

1. Computer Network Background

1-1 Network Evolution

Network interconnection: multiple computers connect to share data. LAN (Local Area Network): many computers linked via switches and routers. WAN (Wide Area Network): computers far apart linked together.

LAN and WAN are relative concepts; a large WAN can be seen as a big LAN.

Computers are tools for people; collaboration requires networks.

2. Introduction to Protocols

"Protocol" is an agreement.

Phone call example: agree on number of rings.

Computer communication uses light or electrical signals; frequency and amplitude represent bits, requiring a data format agreement.

Thought: Is agreeing on a protocol enough for two hosts to communicate? Even if the protocol is the same, different representations (e.g., language) can prevent communication. Therefore, a complete protocol must define detailed rules that all participants follow. Many manufacturers, OSes, and hardware require a common standard – the network protocol.

Core functions of network protocols: Standardization: enable devices from different vendors to interoperate. Specification: define behavior of data during transmission. Reliability: ensure correct delivery via checksums and retransmission.

Examples: HTTP: web browser ↔ server communication. TCP: reliable, ordered data delivery.

2-1 OSI Seven-Layer Model

The OSI model (ISO) defines seven layers from physical transmission to application services, providing a theoretical framework for protocol design.

Layer

Main Function

Typical Protocols/Devices

Application

Provides network services to users (file transfer, email, remote login).

HTTP, FTP, SMTP

Presentation

Data format conversion, encryption, compression.

JPEG, GIF, TLS

Session

Manages session establishment, maintenance, termination.

NetBIOS, RPC

Transport

Reliable end‑to‑end data transfer and error detection.

TCP, UDP

Network

Routing and logical addressing (IP).

IP, ICMP, IGRP

Data Link

Frame assembly, transmission, error detection on LAN.

Ethernet, PPP, Wi‑Fi

Physical

Bit‑stream transmission over physical media (cables, fiber, wireless).

Fiber, Ethernet cable, radio waves

In practice, the session and presentation layers are often omitted, resulting in a five‑layer TCP/IP model.

3. TCP/IP Five‑Layer (or Four‑Layer) Model

TCP/IP is a suite of protocols that implements a four‑layer (sometimes five‑layer) architecture.

Physical layer: transmission of optical/electrical signals (e.g., Ethernet cable, fiber, Wi‑Fi).

Data link layer: frame transmission, MAC address handling, collision detection.

Network layer: IP addressing and routing.

Transport layer: TCP (connection‑oriented, reliable) and UDP (connectionless, best‑effort).

Application layer: protocols such as HTTP, FTP, SMTP.

3-1 Why TCP/IP Exists?

Even a single machine uses internal protocols (e.g., SATA, IDE). When hosts are far apart, new problems arise, requiring a new set of protocols – TCP/IP – to handle long‑distance communication.

3-2 What Is TCP/IP?

TCP/IP is a solution that works because the problems it solves can be layered.

3-3 Relationship Between TCP/IP and the Operating System

TCP/IP operates in the kernel; applications invoke system calls provided by the transport layer.

4. Basic Network Transmission Process

4-1 LAN (Ethernet) Communication Principle

Hosts in the same LAN can communicate directly using unique MAC addresses.

Only one host may transmit at a time; simultaneous transmission causes collisions.

Switches create separate collision domains; without a switch, the whole LAN is one collision domain.

Each Ethernet frame includes a header (protocol‑specific fields) and a payload (actual data). The complete packet is built by encapsulating payload with headers at each layer and later decapsulated in reverse order.

4-1-1 Understanding MAC Addresses

MAC address identifies a node at the data‑link layer (48 bits, usually shown as six hexadecimal bytes).

Assigned by the NIC manufacturer; generally immutable.

4-1-2 Packet Encapsulation and Decapsulation

Encapsulation adds a header at each layer (e.g., segment → datagram → frame). Decapsulation removes headers in reverse order, delivering the payload to the appropriate upper‑layer protocol.

4-2 Cross‑Network Transmission

4-2-1 IP Addressing

IPv4 uses a 32‑bit address written in dotted decimal (e.g., 192.168.0.1). IPv6 uses 128‑bit addresses.

IP addresses remain constant across the entire routing path, while MAC addresses change at each hop.

5. Socket Programming Preparation

5-1 Source and Destination IP Addresses

IP identifies a host; a port identifies a specific process on that host. Together they form a unique endpoint.

5-2 Ports

16‑bit integer used by the transport layer.

Well‑known ports (0‑1023) are reserved for standard services (HTTP, SSH, etc.).

Ephemeral ports (1024‑65535) are assigned dynamically to client processes.

5-2-1 Port Range Division

0‑1023: well‑known services.

1024‑65535: dynamically allocated client ports.

5-2-2 Port vs. Process ID

Ports map network traffic to processes; a process may bind multiple ports, but a port can belong to only one process at a time.

5-2-3 Source and Destination Ports

Transport‑layer segments contain source and destination ports, indicating who sent the data and who should receive it.

5-3 Understanding Sockets

A socket is the combination of an IP address and a port, uniquely identifying a network endpoint.

5-4 Typical Transport‑Layer Protocols

TCP: connection‑oriented, reliable, byte‑stream protocol.

UDP: connectionless, unreliable, datagram protocol.

5-5 Network Byte Order

Network data uses big‑endian order (high‑order byte first). Functions such as htonl, htons, ntohl, and ntohs convert between host and network byte order.

5-6 Socket Programming API

int socket(int domain, int type, int protocol);
int bind(int socket, const struct sockaddr *address, socklen_t address_len);
int listen(int socket, int backlog);
int accept(int socket, struct sockaddr *address, socklen_t *address_len);
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);

5-6-1 sockaddr Structures

The generic struct sockaddr pointer is cast to specific address families such as struct sockaddr_in for IPv4.

image
image
sockaddr_in

contains:

Address family ( AF_INET).

Port number (16 bits, network byte order).

IPv4 address (32 bits, network byte order).

image
image

The in_addr structure holds a 32‑bit IPv4 address.

image
image

6. Conclusion

This guide provides a comprehensive overview of Linux networking fundamentals, protocol layering, addressing, and socket programming, equipping readers with the knowledge needed to develop networked applications on Linux.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxProtocolsNetworkingsocket programmingOSI model
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.