Server Client Messaging Program
Im trying to make a program that allows 1 server to have multiple clients connected to it.
So far I have the server set up quite well, it allows many clients to connect at once and the clients can all send data to the server.
Im having 1 little problem on each side, the server cant send data and the client side cant receive data, I have no idea how to go about fixing this since this is the first time I ever multithread.
For the server program, to send data to the client (assuming you have like 10 clients connected), how can you pick out 1 specific client and actually send the data to him?
Heres the code I use for the server side to send data:
Code:
bool SendtoClient(char *Buffer)
{
unsigned long messageSize = strlen(Buffer);
messageSize = htonl(messageSize);
if (send(Client, (char*)&messageSize, sizeof(messageSize), 0) == SOCKET_ERROR)
return false;
messageSize = ntohl(messageSize);
if (send(Client, Buffer, messageSize, 0) == SOCKET_ERROR)
return false;
}
Client is a SOCKET, i guess it should point to the actual client socket?
This function is called when a user selects the Clients SOCKET address from a list, and clicks send. I think this function should work but im not really sure if your supposed to put the Server's SOCKET or the Client's SOCKET in the first parameter of the send function.
For the Client side i have similar code but instead of using the Server's SOCKET, i use the Client's SOCKET and it works..
Code:
bool SendtoServer(char *Buffer)
{
unsigned long messageSize = strlen(Buffer);
messageSize = htonl(messageSize);
if (send(Client, (char*)&messageSize, sizeof(messageSize), 0) == SOCKET_ERROR)
return false;
messageSize = ntohl(messageSize);
if (send(Client, Buffer, messageSize, 0) == SOCKET_ERROR)
return false;
}