Here is a minimal, self-contained C program that
- opens a TCP connection to a Gopher server,
- sends the selector string,
- reads the document,
- decides when to give up by combining
– application-level timeout (no complete reply within N seconds),
– TCP keep-alive probes (optional but shown), and
– the fact that we have un-acked application data (the request itself) until the first byte of the reply arrives.
If the evidence says “peer is dead”, the program closes the socket and starts a brand-new TCP connection (retry once).
No other recovery tricks are attempted; we simply bail out and reconnect.
Compile with:
gcc gopher_timeout.c -o gopher_timeout
/* gopher_timeout.c – public domain / CC0 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#define HOST "gopher.floodgap.com"
#define PORT 70
#define SELECTOR "/"
#define BUF_SIZE 4096
#define APP_TIMEOUT 10 /* seconds we wait for first byte of reply */
#define KEEP_IDLE 5 /* seconds of silence before first keep-alive probe */
#define KEEP_CNT 3 /* number of probes before we declare dead */
#define KEEP_INTVL 2 /* seconds between probes */
static int connect_to(const char *host, uint16_t port)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) { perror("socket"); return -1; }
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (inet_pton(AF_INET, host, &addr.sin_addr) != 1) {
/* fallback: resolve with gethostbyname omitted for brevity */
fprintf(stderr, "inet_pton failed for %s\n", host);
close(fd); return -1;
}
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("connect"); close(fd); return -1;
}
/* Enable TCP keep-alive so the kernel will send probes */
int opt = 1;
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
opt = KEEP_IDLE;
setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &opt, sizeof(opt));
opt = KEEP_INTVL;
setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &opt, sizeof(opt));
opt = KEEP_CNT;
setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &opt, sizeof(opt));
return fd;
}
static int wait_readable(int fd, int timeout_sec)
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
struct timeval tv = { .tv_sec = timeout_sec, .tv_usec = 0 };
return select(fd + 1, &rfds, NULL, NULL, &tv);
}
static int fetch_once(const char *host, uint16_t port, const char *selector)
{
int fd = connect_to(host, port);
if (fd < 0) return -1;
/* 1. Send the selector (application data) */
char req[1024];
int len = snprintf(req, sizeof(req), "%s\r\n", selector);
if (send(fd, req, len, 0) != len) {
perror("send"); close(fd); return -1;
}
/* 2. Wait for first byte of reply with timeout */
int r = wait_readable(fd, APP_TIMEOUT);
if (r == 0) {
fprintf(stderr, "Timeout: no reply within %d s\n", APP_TIMEOUT);
close(fd);
return -1; /* caller will retry with new connection */
}
if (r < 0) {
perror("select"); close(fd); return -1;
}
/* 3. Stream the rest of the document */
char buf[BUF_SIZE];
ssize_t n;
while ((n = recv(fd, buf, sizeof(buf), 0)) > 0) {
fwrite(buf, 1, n, stdout);
}
if (n < 0) perror("recv");
close(fd);
return 0;
}
int main(void)
{
/* Try once; if it times out, reconnect exactly once. */
if (fetch_once(HOST, PORT, SELECTOR) != 0) {
fprintf(stderr, "First attempt failed, reconnecting...\n");
if (fetch_once(HOST, PORT, SELECTOR) != 0) {
fprintf(stderr, "Second attempt failed, giving up.\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}