Hi,
While reading the tutorial about packets, I tried to do my own project to test it. So, I made a peer-to-peer connection between two computers (it works! ^^), and I sent a packet from one to the other... The problem is when I try to read from the packet: the >> operator always returns false.
Here is the code:
On the sender's side:
#include <sfml/network.hpp>
sf::UdpSocket UdpS;
namespace Contact
{
sf::IpAddress Ip;
unsigned short Port;
};
int main() {
UdpS.setBlocking(false);
//Establishes P2P connection,
//and finds the other contact's IP and port.
//Also binds UdpS.
//Returns true here.
if (!Connect()) {
std::cout << "Connection error." << std::endl;
return 1;
}
std::cout << "Connected." << std::endl;
sf::Uint16 u = 5;
sf::Packet pack; pack << u;
if (UdpS.send(pack, Contact::Ip, Contact::Port) != sf::Socket::Done) {
std::cout << "Failure." << std::endl;
}
std::cout << "Sent" << std::endl; //Sent is printed, thus the packet is sent correctly.
}
And on the recipient's side:
#include <sfml/network.hpp>
sf::UdpSocket UdpS;
namespace Contact
{
sf::IpAddress Ip;
unsigned short Port;
};
int main() {
UdpS.setBlocking(false);
//Establishes P2P connection,
//and finds the other contact's IP and port.
//Also binds UdpS.
//Returns true here.
if (!Connect()) {
std::cout << "Connection error." << std::endl;
return 1;
}
std::cout << "Connected. " << std::endl;
UdpS.setBlocking(true);
sf::Packet pack;
if (UdpS.receive(pack, Contact::Ip, Contact::Port) != sf::Socket::Done) { return 1; }
sf::Uint16 i;
if (!(pack >> i)) { std::cout << "Reading failed." << std::endl; return 1; } //Reading failed is printed.
}