King Tokio
08-26-2009, 10:35 PM
So, with help from a few people here I managed to get my very basic client and server programs working. Many thanks!
I'm taking it up a step now by trying to send any message received by the server, back to all clients (excluding the sender).
My server send/recv code looks like this:
// Check all client sockets for activity
for (i = 0; i < SOCKETNUM; i++)
{
if (SDLNet_SocketReady(client[i].socket))
{
// There is an incoming message from the client
if (client[i].nameset == false)
{
SDLNet_TCP_Recv(client[i].socket, client[i].name,
sizeof(client[i].name));
cout << client[i].name << " connected.\n";
client[i].nameset = true;
}
else if (recvAll(client[i].socket, msg) == -1)
{
cout << client[i].name << " disconnected\n";
client[i].socket = NULL;
}
else
{
cout << client[i].name << ": " << msg << "\n";
len = (short)(strlen(msg)+1);
for (r = 0; r < SOCKETNUM; r++)
{
if (client[r].socket != NULL && r != i)
{
sendAll(client[r].socket, msg, len);
cout << "Sent: " << msg << "\n";
}
}
}
}
}
As you can see, the first message sent by the client is the username. All subsequent messages are printed and then sent back to all clients. I can't see anything wrong with this code, but when I run it and send some messages from the client, the server tells me it has sent a message back but no client receives anything.
This leads me to beleive it may be a client side error. Here is my client send/recv code:
// Get the message to send
cin.getline(msg, sizeof(msg));
len = (short)(strlen(msg)+1);
sendAll(sock, msg, len);
cout << "Sent: " << msg << "\nBytes: " << len << "\nEnter Message: ";
// Check for incoming message
if (SDLNet_SocketReady(sock))
{
if (recvAll(sock, msg) == -1)
{
cout << "You have been disconnected\n";
sock = NULL;
}
else
{
cout << "\nServer: " << msg << "\n";
}
}
Again I can see no coding error :( Oddly enough, the message IS sent back to all the clients, but only once in say, every 20 messages sent.
Anyone got any idea as to what might be wrong?
EDIT: None of the code within if(SDLNet_SocketReady(sock)){} is being executed which means the function call is probably returning false. But since the data IS being sent from the server, I don't know how this is possible.
I'm taking it up a step now by trying to send any message received by the server, back to all clients (excluding the sender).
My server send/recv code looks like this:
// Check all client sockets for activity
for (i = 0; i < SOCKETNUM; i++)
{
if (SDLNet_SocketReady(client[i].socket))
{
// There is an incoming message from the client
if (client[i].nameset == false)
{
SDLNet_TCP_Recv(client[i].socket, client[i].name,
sizeof(client[i].name));
cout << client[i].name << " connected.\n";
client[i].nameset = true;
}
else if (recvAll(client[i].socket, msg) == -1)
{
cout << client[i].name << " disconnected\n";
client[i].socket = NULL;
}
else
{
cout << client[i].name << ": " << msg << "\n";
len = (short)(strlen(msg)+1);
for (r = 0; r < SOCKETNUM; r++)
{
if (client[r].socket != NULL && r != i)
{
sendAll(client[r].socket, msg, len);
cout << "Sent: " << msg << "\n";
}
}
}
}
}
As you can see, the first message sent by the client is the username. All subsequent messages are printed and then sent back to all clients. I can't see anything wrong with this code, but when I run it and send some messages from the client, the server tells me it has sent a message back but no client receives anything.
This leads me to beleive it may be a client side error. Here is my client send/recv code:
// Get the message to send
cin.getline(msg, sizeof(msg));
len = (short)(strlen(msg)+1);
sendAll(sock, msg, len);
cout << "Sent: " << msg << "\nBytes: " << len << "\nEnter Message: ";
// Check for incoming message
if (SDLNet_SocketReady(sock))
{
if (recvAll(sock, msg) == -1)
{
cout << "You have been disconnected\n";
sock = NULL;
}
else
{
cout << "\nServer: " << msg << "\n";
}
}
Again I can see no coding error :( Oddly enough, the message IS sent back to all the clients, but only once in say, every 20 messages sent.
Anyone got any idea as to what might be wrong?
EDIT: None of the code within if(SDLNet_SocketReady(sock)){} is being executed which means the function call is probably returning false. But since the data IS being sent from the server, I don't know how this is possible.