Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Socket Programming  ›  Lesson

The BSD Socket API

BSD Socket 12 minPrograms reach TCP and UDP through the socket calls that began in BSD Unix, socket, bind, listen, accept, connect, send and receive, and Windows offers the same calls through Winsock with a few differences.

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_INET for IPv4, AF_INET6 for IPv6) and a type (SOCK_DGRAM for UDP, SOCK_STREAM for 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".

Two servers, side by sideStep 1 of 4
  1. Create

    Match server: socket(AF_INET, SOCK_DGRAM, 0). Lobby: socket(AF_INET, SOCK_STREAM, 0).

Drills

Answer fast — trust the rule.

quick drill

Which call does a UDP server never need?

quick drill

What does accept return?

quick drill

On a TCP client, which call triggers the three-way handshake?

quick drill

On Windows, what must a program call before any other socket function?

quick drill

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, accept hands out one socket per player.
  • Trap: on Windows, forgetting WSAStartup, or closing with close instead of closesocket.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe calls each server makes

Explain the sequence of socket calls the Manja Arena lobby (TCP) and the match server (UDP) each make, from start-up to talking with players, and why the sequences differ.