POSIX Sockets: How to detect Ctrl-C sent over Telnet?
- by ogott
Short Question
What's the right way to handle a Ctrl-C event sent over Telnet on the server side?
Long Question
After calling recv() on a socket, I'd like to handle some situations appropriately. One of them is to return a certain error code when Ctrl-C was received. What's the correct way to detect this? The following works, but it just doesn't seem right:
size_t recv_count;
static char ctrl_c[5] = {0xff, 0xf4, 0xff, 0xfd, 0x06};
recv_count = recv(socket, buffer, buffer_size, 0);
if (recv_count == sizeof(ctrl_c) &&
memcmp(buffer, ctrl_c, sizeof(ctrl_c) == 0)
{
return CTRL_C_RECEIVED;
}
I found a comment on Ctrl-C in a side-note in this UNIX Socket FAQ:
[...] (by the way, out-of-band is often used for that ctrl-C, too).
As I understand, receiving out-of-band data is done using recv() with a certain flag as the last parameter. But when I'm waiting for data using recv() as I do in the code above, I can't read out-of-band data at the same time. Apart from that, I'm getting something using recv() without that oob-flag.