Hi, Im coding a chat like program (async stream sockets).
I have a server and there can be multiple clients. When a client writes something, the message is sent to the server, it receives it and then send it to the other clients.
What Im doing to send it to the other clients is (with proper error checking):
Code:
for (i = 0; i < NumOfClients; i++) {
send(server.ClientSockets[i], t,strlen(t),NULL);
}
This seems right to me, what I don't know is how to receive data when having multiple clients. Im doing something like this, tell me if its the right way, or if there is a better one.
Code:
case FD_READ:
for ( int i = 0; i < server.NumOfClients; i++) {
int ret = recv(server.ClientSockets[i], b, sizeof(b),0);
if (ret == SOCKET_ERROR) {
// do nothing
} else {
string tempstring = b;
chatbuffer.push_back(tempstring);
}
// broadcast to other clients
memset(b,0,1024);
}
Thx in advance!