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

Author Topic: [SOLVED] [2.0] Simple issues with sf::TcpSocket  (Read 4829 times)

0 Members and 1 Guest are viewing this topic.

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
[SOLVED] [2.0] Simple issues with sf::TcpSocket
« on: September 03, 2012, 09:54:31 pm »
It appears that the buffer I am receiving with is self accumulative or something. However it is, I'm not doing it right. I just want to output all data received correctly to the console, if someone can point me in the right direction that would be great.

Source code relating to the issue:
(This loop is run in another thread)
while (true)
{
    char Buffer[100];
    std::size_t received;
    tcpSock.receive(Buffer,sizeof(Buffer),received);
    std::cout << Buffer;
}
 

Describing the results and problem:

--- SENT FROM SERVER

1234567890
1234567890
1
2
3
4
5
6
7
8
9
0

--- DISPLAYED ON CLIENT

1234567890
00000000000000000
1234567890
00000000000000000
1
34567890
00000000000000000
2
34567890
00000000000000000
3
34567890
00000000000000000
4
34567890
00000000000000000
5
34567890
00000000000000000
6
34567890
00000000000000000
7
34567890
00000000000000000
8
34567890
00000000000000000
9
34567890
00000000000000000
0
34567890
00000000000000000
 

Thanks in advance.
« Last Edit: September 04, 2012, 09:44:02 am by Jishaxe »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #1 on: September 03, 2012, 10:26:32 pm »
What about checking the status returned by receive, in order to know if it was successful? ;)

And do you send the string with or without its final '\0'? If not, you must add it on the client side in order to correctly terminate your string and avoid displaying garbage.
Laurent Gomila - SFML developer

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #2 on: September 03, 2012, 10:44:17 pm »
I've added a check for the status and I'm still getting garbage.

Also, in the test, the data coming from the server is being sent with netcat, and I'm sure that does add a terminating '\0' (I tried adding it on client-side and still garbage). Also, this will be an IRC bot, so I won't have control of the data coming from the other side.
My new code:

    while (true)
    {
        char Buffer[100];
        std::size_t received = 0;

        if (ircConnection.receive(Buffer,sizeof(Buffer) == sf::Socket::Done)
        {
            std::cout << Buffer;
        }

        delete Buffer;
    }
 
« Last Edit: September 03, 2012, 11:10:30 pm by Jishaxe »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #3 on: September 03, 2012, 10:51:05 pm »
I don't know what's wrong with your code, but there's a working example in SFML sources named "sockets". You can try it, and if it works, try to find the difference with your code.
Laurent Gomila - SFML developer

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #4 on: September 03, 2012, 10:53:09 pm »
Alright, will do, thanks :)

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #5 on: September 03, 2012, 11:10:03 pm »
I've looked through the example but couldn't find any differences in the receiving code.

But there is a difference in where the receiving code is. First of all, in the main thread, the connection is established, and then it spawns a thread with that while loop to receive and process data. Could this be where my problem is? Does it not work with multithreading, or using it in a while loop?

EDIT:
There is also another difference from the example. In the example, receive is called once and only once. On mine, it's put in an infinite loop. It appears the first receive() is correct, but the next ones are garbled/have text remaining in it. It can't be because of the buffer because the buffer is recreated every loop. Maybe it's something internal? Do I need to "flush" the socket or something? Has anyone had experience with using receive multiple times?
« Last Edit: September 03, 2012, 11:18:56 pm by Jishaxe »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #6 on: September 03, 2012, 11:38:54 pm »
Quote
Could this be where my problem is? Does it not work with multithreading, or using it in a while loop?
No, it should work fine.

Quote
Maybe it's something internal? Do I need to "flush" the socket or something?
No.

Can you show the code that sends the messages?
Can you run the client again, but with the "received" variable displayed too (and with a std::endl to end each line)?
Laurent Gomila - SFML developer

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #7 on: September 03, 2012, 11:52:19 pm »
Right now, I am connecting the client to netcat and sending data through that, so there is no code for that.

I redid the test and it now prints the received var with this line:
std::cout << "Received: " << received << std::endl;

Here are the results of the test:

What was sent from the server (netcat):

123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012
1234567890123
12345678901234
123456789012345
1234567890123456
12345678901234567
123456789012345678
1234567890123456789
12345678901234567890
 

And this is how the client displayed it: (It's pretty mangled so I had to display it without the code tags so the actual symbols are there)

123
■   h♥×w‗☺×wReceived: 4
1234
   h♥×w‗☺×wReceived: 5
12345
  h♥×w‗☺×wReceived: 6
123456
 h♥×w‗☺×wReceived: 7
1234567
h♥×w‗☺×wReceived: 8
12345678
♥×w‗☺×wReceived: 9
123456789
×w‗☺×wReceived: 10
1234567890
w‗☺×wReceived: 11
12345678901
‗☺×wReceived: 12
123456789012
☺×wReceived: 13
1234567890123
×wReceived: 14
12345678901234
wReceived: 15
123456789012345
Received: 16
1234567890123456
Received: 17
12345678901234567
UvH¹═☻Received: 18
123456789012345678
vH¹═☻Received: 19
1234567890123456789
H¹═☻Received: 20
12345678901234567890
¹═☻Received: 21


With some other tests I did, I noticed a pattern.

I will illustrate it.

Server: 12356
Client: 123456

Server: 123
Client: 123<linebreak>56

Server: 12
Client: 12<linebreak>56

Server: 123456
Client: 123456

Server: 1234567
Client: 1234567

Server: 123
Client: 123<linebreak>567

It's like, a "shadow" of the largest amount of characters so far is kept in the buffer and it will print out the amount of characters in the shadow minus the amount of characters actually sent, after a linebreak.

Also, before the the number before the linebreak is sent, there is a bunch of garbled characters, IE
 h♥×w‗☺×w123

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #8 on: September 03, 2012, 11:55:05 pm »
How do you add the terminating '\0' on client side?
Laurent Gomila - SFML developer

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #9 on: September 03, 2012, 11:59:14 pm »
I only did this for a moment for testing, and it didn't make a difference. And you've just made me realise I did it wrong. I did Buffer[99] = '\0' but of course the buffer might not be filled up to that extent.

But the \0 should have already be added right?

I will try and add it like this, Buffer[received] = '\0'

UPDATE:
Wow. That fixed it. Can't believe it actually worked. My data appears to be arriving correctly now. Thanks for the help, Laurent! I will do more testing then if it works completely, I will mark the topic as solved. :)
« Last Edit: September 04, 2012, 12:01:00 am by Jishaxe »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [2.0] Simple issues with sf::TcpSocket
« Reply #10 on: September 04, 2012, 12:01:48 am »
Quote
But the \0 should have already be added right?
It seems like netcat ends its messages with a line break rather than a 0. Which seems natural because 0 is very specific to C / C++.

Quote
I will try and add it like this, Buffer[received] = '\0'
And I'm pretty sure it will solve your problem ;)
Laurent Gomila - SFML developer

Jishaxe

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: [SOLVED] [2.0] Simple issues with sf::TcpSocket
« Reply #11 on: September 04, 2012, 09:44:13 am »
Solved. Thanks for the help :)

 

anything