csocketsnonblockingposix-select

How to use select() in chat client program in C socket programming?


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 it blocks to receive data from server.

How to write the code in C?


Solution

  • 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.