SFML community forums
Help => Network => Topic started by: newbie123 on September 10, 2011, 06:12:48 am
-
Hello
i have a small problem, when the client receive data its not the same data that i send, its different.
here what the server send:
Age: 12, Name: Bill, Hight: 1.32f
and here what the client receives:
Age: 52428 Name: -blank- Hight: -1.07374e+008
Updated Code 9-12-2011 2:06 pm :-
Server
#include <SFML\Network.hpp>
#include <iostream>
struct PersonData
{
sf::Uint16 Age;
std::string Name;
float Height;
};
sf::Packet& operator <<(sf::Packet& Packet, const PersonData& PD)
{
return Packet << PD.Age << PD.Name << PD.Height;
}
sf::Packet& operator >>(sf::Packet& Packet, PersonData& PD)
{
return Packet >> PD.Age >> PD.Name >> PD.Height;
}
void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;
std::cout << "type the ip address you want to send data to" << std::endl;
std::cin >> Address;
if (Address.IsValid())
std::cout << "Valid" << std::endl;
std::cout << "Press Enter to send packet." <<std::endl;
getchar();
char input = getchar();
PersonData PersonData1 = {12, "Bill", 1.32f};
SendPacket << PersonData1;
while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();
if(input == 'z')
break;
}
Server.Close();
}
int main()
{
const unsigned short Port = 4000;
RunServer(Port);
system("pause");
return 0;
}
Client
#include <SFML\Network.hpp>
#include <iostream>
struct PersonData
{
sf::Uint16 Age;
std::string Name;
float Height;
};
sf::Packet& operator <<(sf::Packet& Packet, const PersonData& PD)
{
return Packet << PD.Age << PD.Name << PD.Height;
}
sf::Packet& operator >>(sf::Packet& Packet, PersonData& PD)
{
return Packet >> PD.Age >> PD.Name >> PD.Height;
}
void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;
if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}
while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
std::cout << "Disconnected" << std::endl;
break;
case sf::Socket::Error:
std::cout << "Error sending" << std::endl;
break;
case sf::Socket::Done:
std::cout << "Done" << std::endl;
break;
case sf::Socket::NotReady:
std::cout << "NotReady" << std::endl;
break;
}
PersonData PersonData2;
if (ReceivePacket >> PersonData2)
{
std::cout << "Age: " << PersonData2.Age << "Name: " << PersonData2.Name << "Hight: "<< PersonData2.Height << std::endl;
}
}
Client.Close();
}
int main()
{
const unsigned short Port = 4000;
RunClient(Port);
system("pause");
return 0;
}
-
its not working
Can you be more precise? Where is it failing?
-
its not working
Can you be more precise? Where is it failing?
well i really dont know what happens.
when i run the client it just sites there waiting to receive the data and nothing shows up on the CMD window. then when i run the server it just give me "Press any key to continue ...".
In the SFML network - packet tutorial source code its just showing how to send packet in TCP connection, i took it and tried to modify it to make it work with UDP but as i told you its not working.
-
You should try the Sockets sample from the SFML SDK. It works with raw data but it can easily be replaced with packets.
-
ok i found the problem.
so what i'm doing is making the server send data and the client receives it and print it out.
so what i did in the client side is:
if (RegularPacket >> RP1)
{
//do...
}
which is wrong, i should have done this
if (RegularPacket << RP1)
{
//do...
}
same in the server side i did this
RegularPacket << C1;
which is wrong i should have do this
(RegularPacket >>RP1)
so now the server sends the data and the client receive it FINALY ! :D .
But i have a small problem now, when the client receive the data its not the same data that i send, its different.
here what the server send:
Age: 12, Name: Bill, Hight: 1.32f
and here what the client receives:
Age: 52428 Name: -blank- Hight: -1.07374e+008
Why ?!
i'll post the updated code above.
-
please guys someone help its been 3 days and i still could not fix this problem .
i alos posted here but no one could fix my problem too.
http://www.gamedev.net/topic/610454-problem-when-trying-to-send-a-packet-in-udp-sfml/
-
If you have updated your code, you should show us the new version.
-
If you have updated your code, you should show us the new version.
i posted the new version above, but i'm still getting Socket state = 3 (error).
-
So it's a new problem, you don't receive garbage anymore? And where does it return an error state?
-
So it's a new problem, you don't receive garbage anymore
yah the new problem is that i don't receive garbage anymore because i changed the operator overloading signs "<<".
where does it return an error state?
after i press Enter key 3 times in the server i get an error in the client.
-
What's the output of both the client and the server?
-
What's the output of both the client and the server?
Client EXE
http://www.mediafire.com/?7585wacih8grhhy
Server EXE
http://www.mediafire.com/?ou2ms3whwma8mgh
(http://img691.imageshack.us/img691/3281/66541892.png)
By fantasyxii (http://profile.imageshack.us/user/fantasyxii)
(http://img594.imageshack.us/img594/6801/47852459.png)
By fantasyxii (http://profile.imageshack.us/user/fantasyxii)
(http://img190.imageshack.us/img190/2305/93087745.png)
By fantasyxii (http://profile.imageshack.us/user/fantasyxii)
(http://img687.imageshack.us/img687/4312/13395323.png)
By fantasyxii (http://profile.imageshack.us/user/fantasyxii)
(http://img813.imageshack.us/img813/2906/78575515.png)
By fantasyxii (http://profile.imageshack.us/user/fantasyxii)
(http://img585.imageshack.us/img585/6989/80624111.png)
By fantasyxii (http://profile.imageshack.us/user/fantasyxii)
-
The address that you pass to Send in the server is uninitialized, you're sending to a random address.
The "choose the address you want to connect to" piece of code should be in the server, not the client since the client only receives.
-
The address that you pass to Send in the server is uninitialized, you're sending to a random address.
The "choose the address you want to connect to" piece of code should be in the server, not the client since the client only receives.
ok i'm not sure if i understood you correctly, is this what you mean i should do?
move this piece of code from client to server
std::cout << "type the server ip address you want to connect to" << std::endl;
std::cin >> Address;
i did that but i still get the some error. should i use other ip than 127.0.0.1?
Server
void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;
std::cout << "type IP " << std::endl;
std::cin >> Address;
std::cout << "Press Enter to send packet." <<std::endl;
getchar();
char input = getchar();
PersonData PersonData1 = {12, "Bill", 1.32f};
SendPacket << PersonData1;
while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();
if(input == 'z')
break;
}
Server.Close();
}
Client
void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;
if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}
while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
std::cout << "Disconnected" << std::endl;
break;
case sf::Socket::Error:
std::cout << "Error sending" << std::endl;
break;
case sf::Socket::Done:
std::cout << "Done" << std::endl;
break;
case sf::Socket::NotReady:
std::cout << "NotReady" << std::endl;
break;
}
PersonData PersonData2;
if (ReceivePacket >> PersonData2)
{
std::cout << "Age: " << PersonData2.Age << "Name: " << PersonData2.Name << "Hight: "<< PersonData2.Height << std::endl;
}
}
Client.Close();
}
-
Is it exactly the same error, after pressing enter 3 times?
127.0.0.1 should be ok. But check the validity of the address in your code, just to be sure you don't mistype it.
-
Is it exactly the same error, after pressing enter 3 times?
127.0.0.1 should be ok. But check the validity of the address in your code, just to be sure you don't mistype it.
yes its the same exact error and i check the validity of the address and its valid.
this thing is really really driving me crazy i dont know what the hell is wrong with it.
in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.
i'll post the updated code above.
-
in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.
You should use the sample directly, and only replace the char[] buffer with a packet -- rather than rewriting it entirely. That would seriously limit the number of potential mistakes.
-
in the first SFML tutorial where you send a "char Buffer[]="i'm client" it works perfectly with both UDP and TCP, but when i try to use packets it doesn't work.
You should use the sample directly, and only replace the char[] buffer with a packet -- rather than rewriting it entirely. That would seriously limit the number of potential mistakes.
its not working, there is something wrong either with my PC or the class packet.
i'm getting the same Socket state which is 3 (error).
when i do this i get Socket state 3 (error)
Server:
SendPacket << message;
Client:
ReceivePacket >> message;
However when i do this i get Socket state 1 which is (Done) but the data is corrupted.
SendPacket >>message;
Client:
ReceivePacket << message;
Please try take my code and run it on your machine and see if you get the same results, maybe i have a virus or something.
here is the code:
Client
#include <SFML\Network.hpp>
#include <iostream>
void RunClient(unsigned short Port)
{
sf::SocketUDP Client;
sf::IPAddress Address;
sf::Packet ReceivePacket;
if (!Client.Bind(Port))
{
std::cout<<"Could not listen";
}
while (true)
{
sf::Socket::Status status = Client.Receive(ReceivePacket, Address, Port);
switch(status)
{
case sf::Socket::Disconnected:
std::cout << "Disconnected" << std::endl;
break;
case sf::Socket::Error:
std::cout << "Socket Status:" << status << std::endl;
break;
case sf::Socket::Done:
{
std::string message;
ReceivePacket >> message;
std::cout << "IP: " << Address << std::endl << "Port: "<< Port << std::endl << "Message: " << message<< std::endl;
break;
}
case sf::Socket::NotReady:
std::cout << "NotReady" << std::endl;
break;
}
}
Client.Close();
}
int main()
{
const unsigned short Port = 4000;
RunClient(Port);
system("pause");
return 0;
}
Server:
#include <SFML\Network.hpp>
#include <iostream>
void RunServer(unsigned short Port)
{
sf::SocketUDP Server;
sf::IPAddress Address;
sf::Packet SendPacket;
std::cout << "type the ip address you want to send data to" << std::endl;
std::cin >> Address;
if (Address.IsValid())
std::cout << "Valid" << std::endl;
std::cout << "Press Enter to send packet." <<std::endl;
getchar();
char input = getchar();
std::string message = "PLEASE WORK!!!";
SendPacket << message;
while(input == 10)
{
if (Server.Send(SendPacket, Address , Port) != sf::Socket::Done)
{
std::cout<<"Could not send data";
}
input = getchar();
if(input == 'z')
break;
}
Server.Close();
}
int main()
{
const unsigned short Port = 4000;
RunServer(Port);
system("pause");
return 0;
}
-
I can't test your code, I don't maintain SFML 1.6 anymore (only have 2.0 builds ready for tests), and I don't know where this getchar() function is coming from.
If you already tried to modify the Sockets sample, can you show the corresponding code?
-
i give up -_-
i'm going to learn Winsock 2. i know its 10 times harder but i might have some luck learning it.
-
i give up -_-
i'm going to learn Winsock 2. i know its 10 times harder but i might have some luck learning it.
Or you could do the smart thing and use SFML2, which is FAR more stable than 1.x.
-
I don't know how copying/pasting a sample and modifying 2 lines of code is more complicated than learning Winsocks. But for some reason you don't want to do that...
And yes, SFML 1.6 might have bugs with UDP and packets (although this very simple code works for me), so try SFML 2 ;)