i try to do a kind-of chat over TCP sockets. The server side is implemented in php and (should) work fine.
No i’ve encountered a problem on the client side (implemented in c [c99]):
i’d like to listen (read) for “new bytes” from the server and i also would like to send (write) “some bytes” to the server whenever the user did enter new data.
Now the question:
-> how do i listen for incoming traffic (read) and still be able to call every 1 seconds a callback where i can check for new bytes to send to the server?
I know that i have to use select (system call) but i do not have a idea how to handle the timeout etc. select is also blocking for a certain time.
Thanks for any example.
There is a function called
selectthat can be used for this. It can check if there is new input on a socket to read, and it has a timeout so it can be used for your timer as well.It exists on one form or other on all major operating systems. Just do a Google search for
socket select <your operating system> exampleand you will get a boat-load of results.The last argument to
selectis used for the timeout. It’s a pointer to astruct timeval, a structure which contains fields to set the timeout.If this pointer is passed as
NULLthen there is no timeout, andselectcan block indefinitely.To set a timeout, you need to set the
tv_secfield of thetimevalstructure to the number of seconds, and thetv_usecfield to the number of microseconds (must be less than one million). You can have a timeout of zero, i.e. just a quick poll, by setting these fields to zero.If
selectreturns zero, then there was a timeout.Example, with a 1.5 second timeout:
If
selectreturns before the timeout, thetimevalstructure is modified to contain the time remaining of the timeout.