codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🌐

Computer Networks

26 chapters

1Network Topologies & LAN/WAN2Network Devices (Hub, Switch, Router)3OSI Reference Model4OSI Model & TCP/IP Suite5TCP/IP Protocol Suite6Switching Techniques (Circuit, Packet)7Data Link: Framing & Error Detection8Error Correction (Hamming Code)9Flow Control (Stop-and-Wait, Sliding Window)10MAC: CSMA/CD & CSMA/CA11Network Layer & Routing12IP Addressing (IPv4, IPv6)13Subnetting & CIDR14Routing Algorithms (Distance Vector, Link State)15ARP, ICMP, and NAT16DHCP Protocol17Transport Layer Services18Transport Layer: UDP19Transport Layer: TCP & 3-Way Handshake20TCP Congestion Control21Application Layer: DNS & HTTP22Application Layer: SMTP & FTP23Socket Programming Basics24Wireless Networks & Wi-Fi Standards25VLANs & Spanning Tree Protocol26Network Security & Cryptography
SubjectsComputer Networks

Socket Programming Basics

Updated 2026-04-30
2 min read

Socket Programming Basics

A Socket is an endpoint for sending and receiving data across a computer network. It is the fundamental abstraction that allows application-level programs (web servers, chat clients, games) to communicate over TCP/IP.

1. What is a Socket?

A socket is uniquely identified by: (IP Address, Port Number, Protocol). When a web server runs on 192.168.1.10:80 using TCP, that specific combination is a socket.

2. TCP Socket Communication (Connection-Oriented)

Server Side:

  1. socket(): Create a socket object.
  2. bind(): Bind the socket to a specific IP address and port number.
  3. listen(): Put the socket in listening mode, ready to accept connections.
  4. accept(): Block and wait. When a client connects, accept returns a NEW socket dedicated to that client.
  5. recv() / send(): Exchange data with the client.
  6. close(): Close the connection.

Client Side:

  1. socket(): Create a socket.
  2. connect(): Connect to the server's IP and port (triggers the TCP 3-way handshake).
  3. send() / recv(): Exchange data.
  4. close(): Close the connection.

3. UDP Socket Communication (Connectionless)

No connection is established. The client simply sends datagrams to a destination address.

  • Server uses recvfrom() and sendto().
  • Client uses sendto() and recvfrom().
  • No connect(), listen(), or accept() needed.

4. Port Numbers

  • Well-Known Ports (0-1023): Reserved for standard services. HTTP = 80, HTTPS = 443, SSH = 22, FTP = 21, DNS = 53, SMTP = 25.
  • Registered Ports (1024-49151): For user applications. MySQL = 3306, PostgreSQL = 5432.
  • Dynamic/Ephemeral Ports (49152-65535): Temporarily assigned by the OS to client sockets.

5. Concurrency in Servers

A simple single-threaded server can only handle one client at a time. To handle thousands of concurrent clients:

  • Multi-threaded: Spawn a new thread for each accepted client connection.
  • Event-driven (Non-blocking I/O): Use select(), poll(), or epoll() to monitor multiple sockets on a single thread. Node.js uses this model.
  • Asynchronous I/O: The OS notifies the application when I/O operations complete.


PreviousApplication Layer: SMTP & FTPNextWireless Networks & Wi-Fi Standards

Recommended Gear

Application Layer: SMTP & FTPWireless Networks & Wi-Fi Standards