Sockets Overview
Network communication basics.
Sockets Overview is a free C Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Socket
A socket is an endpoint for network communication. On POSIX systems it is a file descriptor you can read from and write to, letting two programs exchange data across a network or the same machine.
The Client-Server Model
Most network apps use the client-server model:
- The server binds to an address and waits for connections
- The client initiates a connection to the server
Once connected, both sides send and receive data.
TCP vs UDP
Two main transport protocols:
- TCP (SOCK_STREAM): reliable, ordered, connection-based
- UDP (SOCK_DGRAM): fast, connectionless, may drop or reorder packets
Use TCP for correctness, UDP for low-latency streaming.
Creating a Socket
The socket() call returns a file descriptor. Arguments: address family (AF_INET for IPv4), type (SOCK_STREAM for TCP), and protocol (0 for default).
/* Conceptual: sockets need a network environment to run */
#include <sys/socket.h>
#include <netinet/in.h>
int create_tcp_socket(void) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
/* fd < 0 means error; check with perror */
return fd;
}Socket Address Structures
An IPv4 address is described by struct sockaddr_in, holding the family, port, and IP address. Functions take a generic struct sockaddr *, so you cast.
#include <netinet/in.h>
void fill_addr(struct sockaddr_in *addr, unsigned short port) {
addr->sin_family = AF_INET;
addr->sin_port = htons(port); /* host to network short */
addr->sin_addr.s_addr = INADDR_ANY; /* all interfaces */
}Byte Order Matters
Networks use big-endian (network byte order). Convert with:
htons/htonlhost to network (short/long)ntohs/ntohlnetwork to host
Always convert ports and addresses, or you talk to the wrong place.
IP Address Conversion
inet_pton converts a text IP like "127.0.0.1" into the binary form stored in sin_addr, and inet_ntop reverses it for printing.
#include <arpa/inet.h>
#include <netinet/in.h>
int set_ip(struct sockaddr_in *addr, const char *ip) {
return inet_pton(AF_INET, ip, &addr->sin_addr); /* returns 1 on success */
}The Server Call Sequence
A TCP server typically calls, in order:
socket()createbind()attach to an address and portlisten()mark as passiveaccept()get a connection
The Client Call Sequence
A TCP client is simpler:
socket()createconnect()reach out to the server's address
Then it can send and recv on the same descriptor.
Sending and Receiving
Use send() and recv() (or the generic write() and read()). Both may transfer fewer bytes than requested, so real code loops until all data is handled.
Always Check Return Values
Every socket call can fail. A return of -1 sets errno; call perror to report it. A recv of 0 means the peer closed the connection. Robust networking lives and dies by error checking.
Quick Check
Test your understanding of socket basics.
Recap
You learned the foundations of socket programming.
- A socket is a network endpoint and file descriptor
- Client-server model with TCP (reliable) or UDP (fast)
- Use
sockaddr_inwithhtonsandinet_pton - Server: socket, bind, listen, accept. Client: socket, connect
Frequently asked questions
Is the “Sockets Overview” lesson free?
Yes — the full text of “Sockets Overview” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “Sockets Overview”?
Network communication basics. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Sockets Overview” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Sockets Overview
- TCP Server
- TCP Client
- Handling Multiple Clients