One pattern which I've found useful on embedded systems is to have an error flag for each stream, but provide that an I/O operation which is attempted when the flag is set will fail immediately. Thus, code can do something like:
pkt_type = tcp_getbyte(my_stream, timeout);
pkt_length = tcp_getbyte(my_stream, timeout);
pkt_length |= tcp_getbyte(my_stream, timeout) << 8;
if (pkt_length < MAX_PACKET_LENGTH)
{
for (i=0; i<pkt_length; i++)
buffer[i] = tcp_getbyte(my_stream, timeout);
}
if (!my_stream->error)
{
/* Do something with packet */
}
If one attempt to get a byte times out, succeeding attempts will fail unconditionally, returning zero. It's not necessary to check every operation for failure; if something goes wrong, the system will end up behaving roughly as though tcp_getbyte() had thrown an exception, just not quite as fast.