Environment: Win 7 Enterprise 64, VC++ 2010 express, SFML 1.6I'm trying to make a simple game. A boy is running from a cow. The boy is running on the server PC and the cow is running on the client. It's a simple test to see if it's possible to run a two player game over a LAN. I've followed the tutorials but somehow, I'm stuck.
The class both inherit from (could as well has been a struct, I think):
class spelare
{
public:
sf::Uint8 ID; //0=boy, 1=cow
sf::Int32 hastighetX; //velocity, x
sf::Int32 hastighetY; //velocity y
sf::Sprite sprite; //Sprite with pic
};
The packet, more or less a copy from the lesson:
sf::Packet &operator <<(sf::Packet &Packet, const spelare &C)
{return Packet << C.ID << C.hastighetX << C.hastighetY;}
sf::Packet &operator >>(sf::Packet &Packet, spelare &C)
{ return Packet >> C.ID >> C.hastighetX >> C.hastighetY;}
Function for a server, this function seems to work just fine.
void RunServer(unsigned short Port)
{
sf::SocketTCP Server;
if (!Server.Listen(Port))
return;
std::cout << "Listen on port " << Port << ", wait for connect... " << std::endl;
sf::IPAddress ClientAddress;
sf::SocketTCP Client;
Server.Accept(Client, &ClientAddress);
std::cout << "Client connected : " << ClientAddress << std::endl;
}
And the client, seems to work too.
void RunClient(unsigned short Port)
{
sf::IPAddress ServerAddress;
do
{
std::cout << "IP to connect to? : ";
std::cin >> ServerAddress;
}
while (!ServerAddress.IsValid());
sf::SocketTCP Client;
if (Client.Connect(Port, ServerAddress) != sf::Socket::Done)
return;
std::cout << "Connected to server " << ServerAddress << std::endl;
}
These two functions are run once, at the startup before any windows are shown. I suppose they will be valid for the rest of the session.
Then, I have a function to send the packet with information. Since I never recieve anything, I'm not really sure if it works, but I think something is sent...
void PlayerSend(class spelare &s)
{
spelare C = {s.ID, s.hastighetX, s.hastighetY};
sf::Packet RegularPacket; //Packet created
sf::SocketTCP Client; //Socket created
RegularPacket << C; //Packet recieve it's three values
if (C.ID == 0)
{std::cout << "Boy sent to client: " << std::endl;}
else
{std::cout << "Cow sent to client: " << std::endl;}
if (Client.Send(RegularPacket) != sf::Socket::Done)
return;
}
And finally, the recieving function that doesn't seem to work at all.
void PlayerRecieve(class spelare &s)
{
sf::Packet RegularPacket; //Packet created
sf::SocketTCP Client;
//Client.SetBlocking(false);
spelare C; //Create new C
if (Client.Receive(RegularPacket) != sf::Socket::Done)
return;
//Nothing below this point ever activates
if (RegularPacket >> C)
{
if (C.ID == 0)
{std::cout << "Boy recieved from server: " << std::endl;}
else
{std::cout << "Cow recieved from server: " << std::endl;}
}
else //No regular packet C recieved
{std::cout << "Nothing recieved from server: " << std::endl;}
}
The "PlayerSend" function fires every time an arrow key is pressed so it should be possible for the other PC to have the player moved.
The "PlayerRecieve" function is fired just before the cow and boy are moved. As it is now, it's possible for the client to move the cow and the server to move the boy and it seems as if the two computers are connected somehow.
I can't find what I'm doing wrong. I have followed the tutorial but obviously I have made something wrong. If someone else who is better than I can show me what might be wrong, I'll apprciate it a lot.
Yours sincerely
Ingemar
(
The complete code, even though the comments are in swedish, can be found here at the bottom of the page)