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;
}