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

Author Topic: UDP and Selector  (Read 7804 times)

0 Members and 1 Guest are viewing this topic.

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
UDP and Selector
« on: September 07, 2010, 09:38:08 pm »
Hey, could someone please help and explain to me this example(..) and how to convert it for UDP?!
Code: [Select]

The loop from above...
{
    // Get the current socket
    sf::SocketTCP Socket = Selector.GetSocketReady(i);;

    if (Socket == Listener)
    {
        // If the listening socket is ready, it means that we can accept a new connection
        sf::IPAddress Address;
        sf::SocketTCP Client;
        Listener.Accept(Client, &Address);
        std::cout << "Client connected ! (" << Address << ")" << std::endl;

        // Add it to the selector
        Selector.Add(Client);
    }
    else
    {
        // Else, it is a client socket so we can read the data he sent
        sf::Packet Packet;
        if (Socket.Receive(Packet) == sf::Socket::Done)
        {
            // Extract the message and display it
            std::string Message;
            Packet >> Message;
            std::cout << "A client says : \"" << Message << "\"" << std::endl;
        }
        else
        {
            // Error : we'd better remove the socket from the selector
            Selector.Remove(Socket);
        }
    }
}

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
UDP and Selector
« Reply #1 on: September 08, 2010, 07:25:29 pm »
hellooo..llooo..ooo.. is anybody heere..heere..eeere.. :?
could someone please explain to me how to use selector with UDP?

 it would be nice, if someone would just give me example (:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP and Selector
« Reply #2 on: September 08, 2010, 07:55:50 pm »
First, do you know how to use UDP sockets without a selector?
Laurent Gomila - SFML developer

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
UDP and Selector
« Reply #3 on: September 11, 2010, 11:46:20 pm »
Quote from: "Laurent"
First, do you know how to use UDP sockets without a selector?

kinda..  :?
i can make server which receives packets from client..
but now i want to make that server receive packets from more than one client and send those packets(chat messages, cords,...) to ather clients

do i need to bind socket at client side too?

and i don't understand this example(from this page):
Code: [Select]
The loop from above...
{
    // Get the current socket
    sf::SocketTCP Socket = Selector.GetSocketReady(i);

    if (Socket == Listener)
    {

why that "if" is needed and what does it do :?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP and Selector
« Reply #4 on: September 12, 2010, 10:07:31 am »
There are two kinds of TCP sockets : the "regular" ones, that send and receive data, and the listeners, that listen for incoming connections. That's why the example above has two separate pieces of code, according to the type of the socket.

With UDP there are no connections and listeners, so you don't need this stuff, things are much simpler. Forget about listeners.
Laurent Gomila - SFML developer

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
UDP and Selector
« Reply #5 on: September 12, 2010, 10:50:46 am »
is it right to do like this:
Code: [Select]
sf::IPAddress SenderIP;
    sf::SocketUDP Server;
    if (!Server.Bind(2323))
    {
        // Error...
    }

    sf::SelectorUDP Selector;

    Selector.Add(Server);

    unsigned short SenderPort;

    while(true)
    {
        unsigned int NbSockets = Selector.Wait();

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

            // Do something with Socket...
            sf::Packet Packet;
            if (Socket.Receive(Packet, SenderIP, SenderPort) == sf::Socket::Done)
            {
                // Extract the message and display it
                float    x;
                float    y;
                Packet >> x >> y;
                std::cout << SenderIP << ":"<< SenderPort << " " << x << " " << y << std::endl;
            }
            else
            {
                // Error : we'd better remove the socket from the selector
                Selector.Remove(Socket);
            }
        }
    }
    // Close the socket when we're done
    Server.Close();

and at client side i need to do the same(bind and use selector)?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP and Selector
« Reply #6 on: September 12, 2010, 12:23:41 pm »
There's no need to use a selector when you only have one socket. The purpose of the selector is to make a blocking receive from multiple sockets, without knowing which one will be ready first.

In fact, you probably don't need selectors at all with UDP sockets in most situations. By the way, why did you want to use one? I mean, what problem are you trying to solve?
Laurent Gomila - SFML developer

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
UDP and Selector
« Reply #7 on: September 12, 2010, 12:32:50 pm »
Socket.Receive() freezes the programm..
that cant be solved with selector?  :lol:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP and Selector
« Reply #8 on: September 12, 2010, 12:35:28 pm »
Quote
Socket.Receive() freezes the programm..
that cant be solved with selector?

No, selectors don't solve this problem.
What you can do is to make the socket non-blocking, or put this part of the code in a thread.
Laurent Gomila - SFML developer

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
UDP and Selector
« Reply #9 on: September 12, 2010, 12:39:57 pm »
Quote from: "Laurent"

No, selectors don't solve this problem.

oouu  :oops: ;D

thread sounds dificult..
how to make socket non-blocking?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP and Selector
« Reply #10 on: September 12, 2010, 12:42:54 pm »
Quote
thread sounds dificult..

It's not, as long as you don't share variables between threads. Have you read the corresponding tutorial?

Quote
how to make socket non-blocking?

Look at the documentation:
http://www.sfml-dev.org/documentation/1.6/classsf_1_1SocketUDP.htm#128ebfff7472dfdd330b46b719e06fd6
Laurent Gomila - SFML developer

purkskis

  • Newbie
  • *
  • Posts: 22
    • View Profile
UDP and Selector
« Reply #11 on: September 12, 2010, 02:35:06 pm »
Quote from: "Laurent"

It's not, as long as you don't share variables between threads. Have you read the corresponding tutorial?

you think this:
http://www.sfml-dev.org/tutorials/1.6/system-threads.php
i haven't read it :lol:

so i just need to aad this: Server.SetBlocking(false); ?

 

anything