The BSD Socket API
Everything so far, from handshakes to datagrams to ports, reaches your program through one small set of functions. They first appeared in BSD Unix in the early 1980s, and nearly every operating system and language still copies them. Learn the order of the calls once, and you can read network code in C, Python, Go or C#.
The rule
UDP: socket, bind, then recvfrom and sendto. TCP adds listen and accept.
Here is the heart of the Manja Arena match server, in C. Error checks are left out to keep it short; real code checks every return value.
int fd = socket(AF_INET, SOCK_DGRAM, 0); /* an IPv4 UDP socket */
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons(7777); /* port, in network byte order */
addr.sin_addr.s_addr = htonl(INADDR_ANY); /* every local address */
bind(fd, (struct sockaddr *)&addr, sizeof addr);
char buf[1500];
struct sockaddr_in from;
socklen_t fromlen = sizeof from;
ssize_t n = recvfrom(fd, buf, sizeof buf, 0, (struct sockaddr *)&from, &fromlen);
sendto(fd, buf, n, 0, (struct sockaddr *)&from, fromlen); /* reply to the sender */- socket creates an endpoint, given an address family (
AF_INETfor IPv4,AF_INET6for IPv6) and a type (SOCK_DGRAMfor UDP,SOCK_STREAMfor TCP). - bind attaches it to a local address and port, so players know where to send.
- recvfrom and sendto read and write one datagram at a time, with the other side's address each time.
A TCP server such as the lobby needs two more steps. listen marks the socket as ready to take connections. accept waits for a completed handshake and returns a new socket for that one player, while the listening socket goes back to waiting for the next. A TCP client calls connect, which performs the handshake, and then uses send and recv. Clients rarely call bind: the operating system picks an ephemeral port for them.
On Windows the same calls live in Winsock, with a few differences. A program must call WSAStartup before any other socket function, and WSACleanup when it is done. Sockets have the type SOCKET rather than int, they are closed with closesocket rather than close, and error codes come from WSAGetLastError() rather than errno.
The shortcut
Picture a server's life as one sentence: "Make a socket, give it an address, then serve." For TCP, "serve" means "listen, then accept one new socket per player".
Create
Match server:
socket(AF_INET, SOCK_DGRAM, 0). Lobby:socket(AF_INET, SOCK_STREAM, 0).
Drills
Answer fast — trust the rule.
Which call does a UDP server never need?
What does accept return?
On a TCP client, which call triggers the three-way handshake?
On Windows, what must a program call before any other socket function?
Why does the match server bind to port 7777 while game clients usually do not call bind at all?
Remember
- UDP: socket, bind, then recvfrom and sendto. TCP adds listen and accept.
- One UDP socket serves every player; for TCP,
accepthands out one socket per player. - Trap: on Windows, forgetting
WSAStartup, or closing withcloseinstead ofclosesocket.
