SFML community forums

Help => Network => Topic started by: ultrageek on September 06, 2013, 01:21:29 pm

Title: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: ultrageek on September 06, 2013, 01:21:29 pm
I have these 2 functions, I want server A to send a data packet to client B.

void A()
{
        sf::TcpSocket socket;
        sf::Packet packet;

   int test1 = 1;
   float test2 = 1.0f;
   double test3 = 1.111;
   packet << test1 << test2 << test3;
   
   socket.send(packet);
}

void B()
{
          sf::Packet packet;
   sf::TcpSocket socket;

        socket.receive(packet);
   
   int test1;
   float test2;
   double test3;
   
   packet >> test1 >> test2 >> test3;
   if (packet >> test1 >> test2 >> test3)
   {      
      cout << test1 <<" " << test2 << " " << test3 << endl;
   }
   else
   {
      cout << "Failed" << endl;
   }
}

Anything wrong with my code? When I test the program, "Failed" is always displayed instead of working correctly.
   
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: Laurent on September 06, 2013, 01:31:00 pm
You obviously need to read the tutorial first.
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: ultrageek on September 06, 2013, 01:43:19 pm
You obviously need to read the tutorial first.
Hi Laurent, thank you for your quick reply.

I have read the one regarding Packet, I thought I understood how it works but your comment here tells me I didn't...Can you please enlighten me? D:
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: Laurent on September 06, 2013, 01:50:39 pm
You need to read the tutorial about sockets.
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: FRex on September 06, 2013, 02:10:36 pm
You write data once and then you read it twice, first in own line then in if.
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: Laurent on September 06, 2013, 02:12:37 pm
Just in case: this is not the only error, so don't try to fix your code based on this, but read the tutorial instead ;)
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: ultrageek on September 06, 2013, 03:57:23 pm
You write data once and then you read it twice, first in own line then in if.
Hi FRex, thank you for your reply!

Just in case: this is not the only error, so don't try to fix your code based on this, but read the tutorial instead ;)
I have read the tutorial, from line to line carefully, but I'm still so confused and lost  :(

EDIT: It seems that the problem is with variable type!
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: Laurent on September 06, 2013, 06:17:48 pm
What about connecting your sockets first?
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: ultrageek on September 06, 2013, 06:29:43 pm
What about connecting your sockets first?
I have actually done it at the very beginning  ;D Thank you for your help, it works fine now with SFML's fixed size types. However, it does not seem to work with 2D array with vectors?
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: zsbzsb on September 06, 2013, 07:27:50 pm
I have actually done it at the very beginning  ;D Thank you for your help, it works fine now with SFML's fixed size types. However, it does not seem to work with 2D array with vectors?

First I would recommend against using 2D arrays. They generally just serve to make code more complicated.

As for your problem you really need to show some code of how your storing objects in a vector (probably your copying around an object somewhere).
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: ultrageek on September 06, 2013, 07:51:17 pm
I have actually done it at the very beginning  ;D Thank you for your help, it works fine now with SFML's fixed size types. However, it does not seem to work with 2D array with vectors?

First I would recommend against using 2D arrays. They generally just serve to make code more complicated.

As for your problem you really need to show some code of how your storing objects in a vector (probably your copying around an object somewhere).
Hi zsbzsb,
It does not seem to have another alternative to 2D array in my case :(
It is actually used to store numbers 0 and 1.

vector< vector<sf::Uint8> > grid;

for (int x=0; x<10; ++x)
{
      for (int y=0; y<10; ++y)
      {
        grid[ x ][y] = 0;
      }
}   

While under a condition, grid[ x ][y] will store 1 to indicate an existence in that particular slot.

And this data will be passed to the client.

Thank you in advance.  ;D
Title: Re: Help needed! tcpSocket.receive(packet) keeps on failing.
Post by: Nexus on September 08, 2013, 08:39:45 pm
It does not seem to have another alternative to 2D array in my case :(
There is always an alternative. The obvious one is using an 1D array and mapping indices.

In the case of zeros and ones, you might also think about std::bitset or std::vector<bool>. Or you assign bits manually.