Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Advanced C: Intro to Socket Programming

Lesson 51 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Talking to the World

Socket programming allows your C programs to communicate with other computers across a network (like the Internet). It is the basis for Web Servers, Chat Apps, and Online Games.

What is a Socket?

A socket is like a "telephone jack" for your program. Once you plug into it, you can send and receive data.

The Client-Server Model

  1. Server: Waits for a connection.
  2. Client: Reaches out and requests a connection.

Core Functions (Linux/Unix)

  • socket(): Create a new socket.
  • bind(): Assign a local address (IP and Port) to the socket.
  • listen(): Wait for incoming clients.
  • accept(): Pick up the "phone" when a client calls.
  • connect(): (Client side) Call the server.
  • send() / recv(): Exchange data.

The Difficulty of Networking

Networking adds a whole new layer of complexity:

  • Latency: Data takes time to travel across the world.
  • Packet Loss: Sometimes data just disappears.
  • Endianness: Different CPUs store numbers differently (Big Endian vs. Little Endian). You must convert numbers to "Network Byte Order" before sending them.
If you want to build the next Discord or Spotify, you need to understand how sockets work at the low level. It gives you a much better understanding of how the web actually functions!