Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: TCP server freezes whole program  (Read 3435 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
TCP server freezes whole program
« on: February 21, 2017, 11:19:38 pm »
Hello. I am making multiplayer game. Currently, I want to implement playing on 1 computer. My idea is to start TCP server on localhost and connect clients in game screen here. But my server freezes whole program, so the render loop of the screen doesn't start. Also, I cannot receive my data (it has 0 size and trash in value)
My Server (please, ignore Logger class, it is just basic file stream logger):
struct ServerPlayer {
    TcpSocket sock;
    std::string name;
    int turns;
};

class GameServer {
    public:
        bool start(unsigned short port);
       //Handle gameplay
        void handleMove(int target, int x, int y);
        void handleAttack(int target, int attacker);
        void handleSpell(int target, int spell);
        void handleSkip();
        void parsePacket();
        void shutdown(string msg);
    private:
        bool isRunning;
        TcpListener listener;
        Logger logger;
        vector<ServerPlayer> players;
};
 

Implementation of start method:
bool GameServer::start(unsigned short port)
{
    logger.open("server.log");
    logger.log("Starting game server on port "+patch::to_string(port));
    if (listener.listen(port) != sf::Socket::Done)
    {
        logger.log("ERROR: Cannot start listening on given port!");
        logger.close();
        return false;
    }

    logger.log("Now listening to incoming connections...");

    sf::TcpSocket client;
    isRunning = true;

    while (isRunning) {
        if (listener.accept(client) == sf::Socket::Done) {
            Packet p;
            p << HANDSHAKE;
            client.send(p);
        }
    }

    logger.close();
}

How I start my server:
void startServer() {
  server.start(GAMEPORT);
}
...
   Thread serverThread(&startServer);
    serverThread.launch();
    TcpSocket sock;
    sf::TcpSocket socket;
    sf::Socket::Status status = socket.connect("127.0.0.1", GAMEPORT);
    if (status != sf::Socket::Done)
    {
        printf("ERROR: cannot connect to server!");
    }
    Packet p;
    sock.receive(p);
    int msg;
    if (p >> msg) {

    } else {
        printf("ERROR: couldn't receive data\n");
    }
    printf("Received packet from Server: %d of %d bytes\n", msg, p.getDataSize());
 

I hope you will help me and my code isn't too big to read :D.

Regards,
MrOnlineCoder
« Last Edit: February 21, 2017, 11:21:18 pm by MrOnlineCoder »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: TCP server freezes whole program
« Reply #1 on: February 21, 2017, 11:32:40 pm »
Kind of impossible to say what is really going on with just these snippets of code.
However, you are aware that sockets are blocking by default, meaning they'll wait for a connection to come in, right?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: TCP server freezes whole program
« Reply #2 on: February 22, 2017, 12:05:05 am »
Yea, I am aware of it. If it is impossible to find the problem, then I came up with some questions:
1.Is it a good way to start local game server in the same screen (game state)?
2.Why packet couldn't be read correctly?
3.In my situation, what sockets are better: blocking or not?

EDIT: Hah, probably I found out the problem. Server waits for the client, sends the packet and then again starts waiting for the client, which will never connect 2nd time. But why it freezes whole program while it is started in thread.
« Last Edit: February 22, 2017, 12:11:01 am by MrOnlineCoder »

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: TCP server freezes whole program
« Reply #3 on: February 22, 2017, 11:05:51 am »
 What can cause packet not to be sent correctly?

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: TCP server freezes whole program
« Reply #4 on: February 23, 2017, 06:55:31 am »
Fixed. I was sending raw data and receiving packets, instead of raw data :)