I'm writing a C network server that will pair two clients together and allow them to send messages to each other.
At the moment, each client has their own thread in the server and in the thread I have a loop that is basically while((numBytesRead = read(fd, buffer, 1024)) > 0)
. This works fine and I am able to receive messages and then echo them back to the client.
My problem is though that I'm not sure the best way of transferring a message from one client to another through the server.
I think my biggest issue is that read()
blocks, so I won't be able to send a message to a client until the client sends some text to the server so that read stops blocking.
Is there any way around this? My initial thought was to make one thread for reading from the client and one for writing to the client but if read is blocking in one thread and then I try to write to the same file descriptor then won't this cause issues?
Appreciate any help!! :)