SFML community forums
Help => Network => Topic started by: larztheloser on January 27, 2011, 05:29:22 am
-
Hey,
I'm having a problem creating a simple SFML network app. My client code looks like this:
... (standard variables, includes, using sf namespace etc) ...
SocketTCP TCP;
string msg;
void Thread_Recieve(void* UserData)
{
char Buff[128];
size_t Received;
TCP.Receive(Buff, sizeof(Buff), Received);
cout << "Message from server: " << Buff << endl;
}
...(start of int main, open connection) ...
TCP.SetBlocking(false);
Thread reciever(&Thread_Recieve);
reciever.Launch();
while(true) {
msg="";
cin >> msg;
TCP.Send(msg.c_str(),msg.length());
if(msg=="QUIT") break;
}
TCP.Close();
...
My server code looks quite similar, obviously using a separate TCP for the client and so on.
What I expect this to do is, when I type something in either my client or my server window, it will come up exactly as I typed it on the other end.
Instead, sometimes I cannot type anything at all, and when I am able, it sends the string followed by a bunch of random characters (numbers, punctuation, letters, musical symbols etc).
Anyone have any idea why?[/code]
-
TCP.Receive(Buff, sizeof(Buff), Received);
cout << "Message from server: " << Buff << endl;
You forgot to close your string:
Buff[Received] = 0;
Instead, sometimes I cannot type anything at all
What does this mean exactly? Is the console frozen?
-
At the moment everything in the buffer is being pronted out, even after the null-terminator at the end of the desired string (the '\0' character)
-
Thanks, closing the string properly (duh!) solved most of the problem.
Instead, sometimes I cannot type anything at all
For some reason, adding the null terminating character changes this problem. It used to refuse to let me type anything. Now it inputs, but it won't send the message. I've noticed that both the client and the server can send a message only once. After that I can still type, but hitting the enter key has no effect. However, I know that the send function is still running because it correctly identifies when we have disconnected.
The server has also started to send an apparently blank message when it connects to the client.
At the moment everything in the buffer is being pronted out, even after the null-terminator at the end of the desired string
Seeing as I am re-creating the character array every time that Thread_Receive runs (presumably making the string blank), why should there be any characters after the null terminator?
-
I don't know what happens behind the scenes when re-initialising the array, but it definitely leaves "garbage" behind. I had the same problem.
-
I had the same problem.
Mind sharing how you fixed it?
-
As in, the same receiving garbage characters problem. I wrote a function to loop through the buffer, stopping at the first '\0' character it saw.
int i = 0;
while (buf[i] != '\0")
{
cout << buf[i];
i++;
}
cout << endl;
Something like that.
-
Just did some testing and Laurent's solution works better. Thanks though.
The biggest problem is still that it won't send more then one message (or maybe it wont receive more than one?)