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

Author Topic: Help needed! tcpSocket.receive(packet) keeps on failing.  (Read 3854 times)

0 Members and 5 Guests are viewing this topic.

ultrageek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Help needed! tcpSocket.receive(packet) keeps on failing.
« 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.
   
« Last Edit: September 06, 2013, 01:25:36 pm by ultrageek »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #1 on: September 06, 2013, 01:31:00 pm »
You obviously need to read the tutorial first.
Laurent Gomila - SFML developer

ultrageek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #2 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:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #3 on: September 06, 2013, 01:50:39 pm »
You need to read the tutorial about sockets.
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #4 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.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #5 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 ;)
Laurent Gomila - SFML developer

ultrageek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #6 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!
« Last Edit: September 06, 2013, 04:21:01 pm by ultrageek »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #7 on: September 06, 2013, 06:17:48 pm »
What about connecting your sockets first?
Laurent Gomila - SFML developer

ultrageek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #8 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?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #9 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).
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

ultrageek

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #10 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
« Last Edit: September 06, 2013, 07:53:26 pm by ultrageek »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Help needed! tcpSocket.receive(packet) keeps on failing.
« Reply #11 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: