SFML community forums
Help => Network => Topic started 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.
-
You obviously need to read the tutorial first.
-
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:
-
You need to read the tutorial about sockets.
-
You write data once and then you read it twice, first in own line then in if.
-
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 ;)
-
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!
-
What about connecting your sockets first?
-
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?
-
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).
-
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
-
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.