Firstly, if you are trying to write socket functions, do not use fgets()
or anything else that uses buffered IO, otherwise known as a FILE *
. Instead use file descriptors (fd). Generally, every libc
function beginning with 'f' is to be avoided. You want read
and write
.
Secondly, you want to read up on asynchronous I/O with select()
, rather than work out how to 'break out' of fgets()
.
Thirdly, I could give you a tutorial here, or I could tell you to google, or look at http://en.wikipedia.org/wiki/Asynchronous_I/O but really what you want to find is a copy of Stephens (from memory "Advanced Programming in the Unix Environment" is what you want but really you should buy all of them and tape them to your body whilst you sleep in the hope of learning by osmosis).
Fourthly, I know you said you wanted to do this with threads. You can kill a thread with pthread_cancel()
if you really want to do that, and restart it. Don't. Do it properly. You don't need threads.