Networking in C · advanced · ~15 min
Send an HTTP/1.0 request over a TCP connection, then read and parse the response status line.
HTTP/1.0 is a text-based protocol that runs on top of TCP. Because it is text, you can write a request as a plain string and read the reply the same way.
The exchange has three steps:
Connect a TCP socket to the server.
Write the request. A minimal GET request looks like this:
GET /path HTTP/1.0\r\nHost: example\r\n\r\n
Each line ends with \r\n (carriage return + line feed). The blank line at the end (an extra \r\n) tells the server the request is finished.
Read the response from the same socket.
For the C-platform exercises, only target localhost. The test harness starts a local server in a separate thread for you to connect to.
Never point such a client at third-party hosts without explicit permission.
const char *req = "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n";
write(s, req, strlen(req));
char buf[4096]; ssize_t n = read(s, buf, sizeof buf - 1);
buf[n] = 0; puts(buf);
GET /path HTTP/1.0 with \r\n line endings and a trailing blank line, then read the reply.