I'm giving up any tries to continue this kind of projekt. I just can't find a way to make it work. Here, I have narrowed down the code to a bare bone. If you hit space, you send the info to the other PC. It's not more complicated than that, but I've tried both with two copies on the same PC and with two different PC:s om the same network and it is nigh impossible to recieve anything and I can't send if I'm a server. As a client sending is just fine.
If someone smarter can find the bug, I'll be happy.
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Network.hpp>
//#include <vector>
#define SFML_STATIC //No extra DLL-filer
class spelare
{
public:
sf::Int32 ID; //0=pojke, 1=ko (sf::Uint8) ger ascii tecken
sf::Int32 hastighetX; //Hastighet i X-led, kan vara negativ
sf::Int32 hastighetY; //Hastighet i Y-led, kan vara negativ
sf::Sprite sprite; //Bilden som representerar pojken eller kon
};
// Server = handles boy
// Client = handles cow
//--------------------------------------------------------------
// Create packet structure
//--------------------------------------------------------------
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;
}
// Create a global TCP socket
sf::SocketTCP spelsocket;
//Functions
void RunClient(unsigned short Port);
void RunServer(unsigned short Port);
void PlayerSend(class spelare &s);
void PlayerRecieve( class spelare &s);
//---------------------------------------------------------
// Program start
//---------------------------------------------------------
int main (int argc, char **argv)
{
char Who = 'Z'; //Are you a client or a server?
//Create a boy
spelare pojke;
pojke.hastighetX = 100;
pojke.hastighetY = 100;
pojke.ID=0;
//Create a cow
spelare ko;
ko.hastighetX = 500;
ko.hastighetY = 500;
ko.ID=1;
//--------------------------------------------------------
// Show local adress, probably 192.168.x.x or 10.100.x.x or 169.254.x.x
//-------------------------------------------
sf::IPAddress Address1 = sf::IPAddress::GetLocalAddress();
std::string IP1 = Address1.ToString();
std::cout<<"Your local IP = " << IP1 << std::endl << std::endl;
//Create a port
const unsigned short Port = 2435;
// Client or server ?------------------------------------------------
std::cout << "Do you want to be a server ('s') or a client ('c') ? ";
std::cin >> Who;
if (Who == 's')
RunServer(Port);
else
RunClient(Port);
spelsocket.SetBlocking(false); //Recieve without stop
std::string caption;
if (Who == 's')
caption= "SERVER ";
else
caption= "CLIENT ";
sf::RenderWindow App(sf::VideoMode(800, 600, 32), caption);
while(App.IsOpened())
{ //Gameloop
sf::Event Event;
while (App.GetEvent(Event))
{ //while 2
if (Event.Type == sf::Event::Closed) //hit [x] symbol?
App.Close();
if (Event.Type == sf::Event::KeyPressed)
{ //if 1
if (Event.Key.Code == sf::Key::Escape) // ESC tangenten = close
App.Close();
if (Event.Key.Code == sf::Key::Space)
{
if (Who == 's')//server
{
PlayerSend(pojke); //Send info about boy
PlayerRecieve(ko); //Recieve info about cow
}
else //client
{
PlayerSend(ko); //Send info about cow
PlayerRecieve(pojke); //Recieve info about boy
}
}
} //end if 1
} //end, while 2
} // end Gameloop
return 0;
} //end program
void RunClient(unsigned short Port)
{
// Ask for server IP
sf::IPAddress ServerAddress;
do
{
std::cout << "Write down IP to connect to: ";
std::cin >> ServerAddress;
}
while (!ServerAddress.IsValid());
if (spelsocket.Connect(Port, ServerAddress) != sf::Socket::Done)
return;
std::cout << "Connecting to server " << ServerAddress << std::endl;
}
//Serverfunktionerna
void RunServer(unsigned short Port)
{
if (!spelsocket.Listen(Port))
return;
std::cout << "Server is listening to port " << Port << ", wait for connect... " << std::endl;
//Wait for connect
sf::IPAddress ClientAddress;
sf::SocketTCP Client;
spelsocket.Accept(Client, &ClientAddress);
std::cout << "Client connected : " << ClientAddress << std::endl;
}
void PlayerSend(class spelare &s)
{
sf::Packet paquetout;
spelare P; //Dummy player to save info in
P.ID = s.ID;
P.hastighetX = s.hastighetX;
P.hastighetY = s.hastighetY;
paquetout << P;
if (spelsocket.Send(paquetout) != sf::Socket::Done)
{
std::cout<<"Packet can't be sent"<<std::endl;
return;
}
else
{
std::cout<<"Packet is sent"<<std::endl;
std::cout<<"########################## " << std::endl;
std::cout<<"ID = " << P.ID << std::endl;
std::cout<<" X = " << P.hastighetX << std::endl;
std::cout<<" Y = " << P.hastighetY << std::endl;
std::cout<<"########################## " << std::endl;
}
}
void PlayerRecieve(class spelare &s)
{
spelare R; //Dummy player to extract info to
sf::Packet paquetin;
if (spelsocket.Receive(paquetin) != sf::Socket::Done)
{
std::cout << "Packet is lost" << std::endl;
return;
}
else
{
std::cout<<"Packet is recieved"<<std::endl;
paquetin >> R;
std::cout<<"00000000000000000000000000000000 " << std::endl;
std::cout<<"ID = " << R.ID << std::endl;
std::cout<<" X = " << R.hastighetX << std::endl;
std::cout<<" Y = " << R.hastighetY << std::endl;
std::cout<<"00000000000000000000000000000000000 " << std::endl
}
}
//Ingemar