I have an assignment that implements a Client-server chat room in C. I've got most of it down, but have got stuck on the client-side with regard to select()
.
When a client connects to the server, he doesn't send anything unless the user actually inputs something to stdin
. However, he should still receive all the messages that are either broadcast to all users or aimed specifically at him.
Ex.:
- C1, C2, C3 are connected and talking
- C4 connects, but says nothing. C1/2/3 still talking.
- C1/2/3 see nothing with regard to C4 (except that he connected), but C4 sees all the messages that C1/2/3 have been sending since he connected.
So in essence, the client should block upon reading but still receive messages (quite contradictory!). The only way I can think of right now would be to make the client multi-threaded with one thread listening for receive, and the other for sending; or using fork()
.
My question is the following: Is there an easier way to do the above with select()
? Would select still do the trick if, say, I add stdin
to my write_fds
and both stdin
the socket that is used to connect to the server to my read_fds
(since I would need to read from both) and pass those to select()
? Since TCP is full-duplex, there should be no issues when/if a read and a write happen at the same time, correct?
I guess essentially, what I want to implement is a simple telnet session for all of my clients. Any suggestions to that end would be quite welcome.
EDIT:
Something I neglected to mention: I am well aware that I can use FD_ISSET
to both check if the server sent something and if the client sent something, but I see no way to make those behave the way I want it to. That is, I see no way to still receive something while "blocking" on send - perhaps adding a timer to the select()
and periodically polling both the socket and stdin? I will attempt that and edit this post further, but any suggestions are quite welcome in the meantime.