Guys, I'm facing a trouble while trying to send packets. I open two instances of the same project in the same machine - the first one is set as Server and the second one as Client. When the connection is established, the Server instance of the project crashes. Could you please check the code below and tell me what's going on?
sf::TcpSocket socket;
int main()
{
Server server;
sf::Packet j0;
if (eServer == true) // boolean indicating if it's the server or a client
{
sf::TcpListener listener;
listener.listen(2000);
listener.accept(socket);
j0 << server.player1.attack << server.player1.defense << server.player1.name << server.player2.attack << server.player2.defense << server.player2.name;
socket.send(j0);
while (servLoop == true)
{
// server keeps running while the game happens
}
}
else
{
socket.connect(sf::IpAddress::getLocalAddress(), 2000);
Client client(width, height, title); // client's class
int atk, def;
std::string name;
socket.receive(j0);
j0 >> atk >> def >> name;
client.setPlayer(atk, def, name);
while (client.getWindow().isOpen())
{
sf::Event event;
while (client.getWindow().pollEvent(event))
{
if (event.type == sf::Event::Closed){ c.getWindow().close(); }
client.getWindow().clear();
client.game(); // where the game happens
client.getWindow().display();
}
}
}
return 0;
}