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

Author Topic: Writing a multiplayer game server  (Read 4971 times)

0 Members and 1 Guest are viewing this topic.

noodlesgc

  • Guest
Writing a multiplayer game server
« on: December 30, 2009, 07:55:25 am »
I've begun work on adding multiplayer to my RTS game, but I've gotten a bit stuck.

So with a simple chat application, the server will wait until a client sends something, then they can send that to all the other clients. My question is how do other clients recieve data if they are busy sending it?

In the code below, the server recieves the message, but how would I go about sending the messages to all the clients once it arrives?


Thanks.

Here's my attempt from code from the tutorials.
(I know there are memory leaks.)
Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Network.hpp>

#include <vector>
#include <iostream>
#include <ctime>
#include <cstring>

int PORT = 1234;

class GameServer
{
public:
    GameServer() {};
    ~GameServer() {};

    void RecieveConnections()
    {
        sf::SocketTCP Listener;
        if (!Listener.Listen(PORT))
            return;
        std::cout << "Listening to port " << PORT << ", waiting for connections..." << std::endl;

        sf::SelectorTCP Selector;
        Selector.Add(Listener);
        while (true)
        {
            std::cout << "Waiting..." << std::endl;
            unsigned int NbSockets = Selector.Wait();

            for (unsigned int i = 0; i < NbSockets; ++i)
            {
                sf::SocketTCP Socket = Selector.GetSocketReady(i);

                if (Socket == Listener)
                {
                    sf::IPAddress Address;
                    sf::SocketTCP Client;
                    Listener.Accept(Client, &Address);
                    std::cout << "Client connected ! (" << Address << ")" << std::endl;

                    Selector.Add(Client);
                    _client_sockets.push_back(&Client);
                }
                else
                {
                    sf::Packet Packet;
                    if (Socket.Receive(Packet) == sf::Socket::Done)
                    {
                        std::string Message;
                        Packet >> Message;
                        std::cout << "A client says : \"" << Message << "\"" << std::endl;
                    }
                    else
                    {
                        Selector.Remove(Socket);
                    }
                }
            }
        }
    }

private:
    std::vector<sf::SocketTCP*> _client_sockets;
};

class VirtualGame
{
public:
    VirtualGame()
    {
    };
   
    ~VirtualGame() {};

    void Run()
    {
        std::cerr << "Attempting to connect" << std::endl;
        if (_socket.Connect(PORT,"127.0.0.1") != sf::Socket::Done)
        {
            std::cerr << "Unable to connect" << std::endl;
        }
        else
        {
            std::cerr << "connected" << std::endl;
           
            bool Connected = true;
            while (true)
            {
                std::string Message;
                std::cout << "Say something to the server : ";
                std::getline(std::cin, Message);

                sf::Packet Packet;
                Packet << Message;
                _socket.Send(Packet);
            }
           
        }
    }
   
private:
    sf::SocketTCP _socket;
};


int main()
{
    char w;
    std::cout << "Enter 's' for server or anything else for client" << std::endl;
    std::cin >> w;
    if (w=='s')
    {
        GameServer gameserver = GameServer();
        gameserver.RecieveConnections();
    }
    else
    {
        VirtualGame *virtualgame = new VirtualGame();
        virtualgame->Run();
    }
}

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Writing a multiplayer game server
« Reply #1 on: December 30, 2009, 09:12:52 pm »
I didn't look at your code(nor do I use the Network Library), but the first idea that pops into my head is multithreading. One thread for sending messages, one for receiving. Probably not the best way, but it should work if you do it right. Also, make a list of clients that are connected, and when you receive a package, iterate through the list, sending the packet to everyone.
I use the latest build of SFML2

noodlesgc

  • Guest
Writing a multiplayer game server
« Reply #2 on: December 31, 2009, 05:49:44 pm »
Thanks for the reply.

In the end I decided to go with boost::asio combine with boost::thread.
The chat example at: http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/examples.html
was exactly what I was looking for.