Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: TCP sending random characters  (Read 2999 times)

0 Members and 1 Guest are viewing this topic.

larztheloser

  • Newbie
  • *
  • Posts: 4
    • View Profile
TCP sending random characters
« 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:

Code: [Select]
... (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]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
TCP sending random characters
« Reply #1 on: January 27, 2011, 07:41:53 am »
Quote
Code: [Select]
  TCP.Receive(Buff, sizeof(Buff), Received);
   cout << "Message from server: " << Buff << endl;

You forgot to close your string:
Code: [Select]
Buff[Received] = 0;

Quote
Instead, sometimes I cannot type anything at all

What does this mean exactly? Is the console frozen?
Laurent Gomila - SFML developer

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TCP sending random characters
« Reply #2 on: January 27, 2011, 10:47:27 am »
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)

larztheloser

  • Newbie
  • *
  • Posts: 4
    • View Profile
TCP sending random characters
« Reply #3 on: January 28, 2011, 12:47:06 am »
Thanks, closing the string properly (duh!) solved most of the problem.

Quote
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.

Quote
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?

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TCP sending random characters
« Reply #4 on: January 28, 2011, 04:53:02 am »
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.

larztheloser

  • Newbie
  • *
  • Posts: 4
    • View Profile
TCP sending random characters
« Reply #5 on: January 28, 2011, 05:11:25 am »
Quote
I had the same problem.


Mind sharing how you fixed it?

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
TCP sending random characters
« Reply #6 on: January 28, 2011, 05:16:27 am »
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.
Code: [Select]

int i = 0;
while (buf[i] != '\0")
{
  cout << buf[i];
  i++;
}
cout << endl;

Something like that.

larztheloser

  • Newbie
  • *
  • Posts: 4
    • View Profile
TCP sending random characters
« Reply #7 on: January 28, 2011, 05:56:43 am »
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?)

 

anything