Tutorial 5: Polling Sockets
Adapted from Spectranet: Tutorial 5.
The earlier examples block while waiting for network data. That is acceptable for simple programs, but not for games, terminal clients, or servers that need to handle several sockets while still processing input and screen updates.
Polling
The Spectranet ROM provides polling routines rather than a full POSIX select interface. The most useful calls are:
pollfd, which checks one socketpollall, which checks all open sockets
Polling reports readiness and state changes. It is not just "INKEY$ for sockets"; it can tell a program when a listening socket needs accept, when an established socket has data to read, or when a remote peer has closed the connection.
Using pollall
The tutorial extends the TCP server pattern from Tutorial 2. After the socket is opened, bound, and placed into listening mode, the program enters a loop and calls pollall.
In the C example the shape is:
#include <sockpoll.h>
struct pollfd p;
polled = pollall(&p);
If pollall returns the listening socket, the program calls accept. If it returns another socket, the program checks p.revents: a hangup means the socket should be closed, otherwise data can be read with recv.
Why this matters
Polling lets a single Spectrum program share time between network activity and other work. It is the basis for more useful clients and servers, including programs that need keyboard input, screen animation, multiple connections, or timeout handling.