SFML community forums

Help => Network => Topic started by: Grimlen on September 23, 2012, 06:37:03 am

Title: How do I set up a UDP connection for my server?
Post by: Grimlen on September 23, 2012, 06:37:03 am
Here's my current server code:
/************************************************************
SERVER
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

#include <SFML/Graphics.hpp>

bool running = true;

void netThread(){
        char buffer[128];
        std::size_t received;
        sf::IpAddress sender;
        unsigned short port;

        // Create the UDP socket
        sf::UdpSocket socket;
        socket.setBlocking(false);
        // Bind it (listen) to the port 4567
        if (!socket.bind(4567)){
                // Error...
                cout << "Unable to bind UDP socket: 4567" << endl;
        }

        while(running){
                int rec = socket.receive(buffer, sizeof(buffer), received, sender, port);
                if(rec != sf::Socket::Done){
                        //Error
                }
                if(rec == sf::Socket::Disconnected)
                        cout << "D/C" << endl;
                // Show the address / port of the sender
                std::cout << sender << ":" << port << std::endl;
                // Show the message
                std::cout << buffer << std::endl; // "Hi guys !"
        }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::Thread thread(&netThread);
        thread.launch();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        if (event.type == sf::Event::Closed){
                                running = false;
                window.close();
                        }
        }

        window.clear();
        window.display();
    }
        running = false;
    return 0;
}
 

Can anyone make any suggestions as to what I should do for a UDP connection?
I'm trying to have multiple clients for my server, so...would I use just one socket? Or multiple sockets?
I'm assuming I just use one as there's no actual connection and it's just packets coming into
my server, however how would I differentiate different players on my game? By IP?
Or would I have them, for each message they send, send a special ID that uniquely identifies
who they are on the server...?

Not sure how to set this up.
Title: Re: How do I set up a UDP connection for my server?
Post by: Laurent on September 23, 2012, 10:19:54 am
The IP will be unique for each player, so you can use that to differentiate them. Using a single UDP socket is fine.

So, do you have an actual problem, or are you just afraid to test your ideas? ;)
Title: Re: How do I set up a UDP connection for my server?
Post by: Grimlen on September 23, 2012, 07:36:28 pm
Well what if two people from the same IP connect?

Also I'm basically asking from my code, is it set up right?
Like what would you do different?
Title: Re: How do I set up a UDP connection for my server?
Post by: Laurent on September 23, 2012, 08:16:14 pm
Quote
Well what if two people from the same IP connect?
Then you would have to use the port to differentiate them.

Quote
Also I'm basically asking from my code, is it set up right?
I would change one thing: either make my receive loop sleep, so that it doesn't use 100% of the CPU for nothing, or use blocking sockets with a selector -- there's no need to use non-blocking sockets if all that your thread does is receiving network messages.