How to use select() in chat client program in C socket programming?
I'm newbie.
I want to make the client program to receive input from keyboard and data from server. I don't want if while user type something (scanf) and its block to receive data from server.
How to write the code开发者_如何转开发 in C ?
Thank you.
Welcome :)
I suggest grabbing a copy of Advanced Programming in the Unix Environment, 2nd Edition as soon as you can. It has excellent examples on using select()
(and everything else, too).
The source code package on the page above includes an excellent example, calld/loop.c
that shows more or less exactly what you want -- a server loop that accepts connections, adds the connections to the select mask of file descriptors, and handles file descriptors in turn.
If you'd like a friendlier interface, investigate libevent. Libevent can give you higher performance on a wide range of platforms and a nice interface. Great for production code, maybe less great for learning how the kernel works.
精彩评论