Socket Programming Basics·Lesson 1 of 9

Socket Programming Basics

01

Talking to the Network From Code

Socket programming is how you write a program that sends and receives data over a network — for example, a chat app that delivers your message to a friend's computer. The key tool is a : a kind of doorway your program opens to push data out to the network and pull data in. Learn sockets and you understand what's really happening underneath every app that talks to the internet.

The one-line idea

A socket is your program's plug into the network. Socket programming is just opening that plug and reading and writing data through it.

☎️

Like a telephone handset

A phone handset is the thing you speak into and listen from — it connects you to the person on the other end. A socket is that handset for your program: one endpoint of a conversation between two computers.

02

Why It's Worth Learning

Almost everything that goes over a network sits on top of sockets: web browsers, video calls, online games, email, databases. Most of the time you use a higher-level library that hides the details, but those libraries are built on sockets underneath. Knowing how sockets work makes it far easier to reason about, debug, and design anything that talks over a network.

03

Two Flavors of Socket

There are two main kinds of socket, and they differ in how carefully they deliver your data. A TCP (Transmission Control Protocol) socket is reliable and ordered — like a phone call. A UDP (User Datagram Protocol) socket is fast and lightweight but makes no promises — like dropping postcards in the mail. Here's the contrast at a glance:

TCP socketUDP socket
ConnectionSet up first, then talkNone — just send
ReliabilityGuaranteed delivery, in orderBest effort; may be lost or reordered
SpeedSlightly slower (more bookkeeping)Faster, less overhead
Good forWeb pages, email, file transferLive video, games, voice calls

Each kind gets its own lesson: the reliable stream in TCP Socket and the lightweight datagram in UDP Socket.

04

The Client and the Server

In almost every socket program, one side waits and the other reaches out. The side that waits for connections is the server; the side that starts the conversation is the client. This split is the , and it shapes how you write the two ends — the server binds to a known address and listens, while the client connects to it.

05

What This Topic Covers

The rest of this topic builds up socket programming one piece at a time — first the socket itself and how it's addressed, then the two socket types, then the client and server roles, and finally the full flow and where it all shows up in the real world.

Quick Revision Cheat Sheet

Socket: A program's endpoint for sending/receiving over a network

Socket programming: Writing code that reads and writes through sockets

TCP socket: Reliable, ordered, connection-based — like a call

UDP socket: Fast, connectionless, best-effort — like postcards

Two roles: Server waits and listens; client reaches out