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

Author Topic: How do I make it so the program doesn't stop for a message?  (Read 2105 times)

0 Members and 1 Guest are viewing this topic.

MarthKoopa

  • Guest
How do I make it so the program doesn't stop for a message?
« on: August 19, 2010, 10:15:00 am »
I'm attempting my first multiplayer game (nothing complex, just a 2D shooter arena thingy), and have encountered a problem...

The server will also be displaying graphics of what's going on in the game, and is used to edit the map in real time (like that new popular flash game, Everybody Edits, yes I'm a copy cat!)

The editor itself is done, but when I add in the server functionality, the client will only process the editor's logic for a single frame after a message has been received.

Here's the code for my server.  The Run function is called every loop.  How do I make it so that if there's no message to do stuff with, it skips this part?

Code: [Select]
class Server{
public:
    UdpSocket      Socket;
    unsigned short Port;

    Server(unsigned short GetPort){
        Port = GetPort;
        Socket.Bind(Port);
    }

    void Run(){
        char           buffer[1024];
        size_t         received = 0;
        IpAddress      sender;

        cout << "Waiting For Next Message" << endl;

        // Get Messages
        Socket.Receive(buffer, sizeof(buffer), received, sender, Port);
        cout << sender.ToString() << buffer << endl;

        string M = buffer;

        // A Player is connecting
        if (M.find("Connecting")){
            // Send an answer
            string message = "Accepted";
            Socket.Send(message.c_str(), message.size() + 1, sender, Port);
        }
    }
};

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How do I make it so the program doesn't stop for a message?
« Reply #1 on: August 19, 2010, 10:29:13 am »
You can make your socket non-blocking (SetBlocking(false)), or execute this code in a thread.
Laurent Gomila - SFML developer