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

Author Topic: Could someone explain me this?  (Read 2900 times)

0 Members and 3 Guests are viewing this topic.

pufixas

  • Newbie
  • *
  • Posts: 5
    • View Profile
Could someone explain me this?
« on: June 22, 2012, 09:24:56 pm »
#include <SFML/Network.hpp>
#include <iostream>

using namespace std;

int main()
{
        sf::SocketTCP Listener;

        Listener.Listen(2435);

        sf::SelectorTCP Selector;

        cout << "Waiting for port 2435 connection" << endl;

        Selector.Add(Listener);

        while(true)
        {
               
                int Sockets = Selector.Wait();
                cout << "Sockets: " << Sockets << endl;

                for(int i = 0; i < Sockets; i++)
                {
                        sf::SocketTCP Socket = Selector.GetSocketReady(i);
                        cout << "For loop after Selector.GetSocketReady(i);" << endl;

                        if(Socket == Listener)
                        {
                                sf::IPAddress adress;
                                sf::SocketTCP Client;
                                Listener.Accept(Client,&adress);

                                cout << "Address " << adress << " connected" << endl;
                               
                                Selector.Add(Client);
                        }
                        else
                        {
                                sf::Packet packet;
                                if(Socket.Receive(packet) == sf::Socket::Done)
                                {
                                        string String;
                                        packet >> String;
                                        cout << String << endl;
                                }
                                else
                                {
                                        cout << "Disconnected" << endl;
                                        Selector.Remove(Socket);
                                }
                        }
                }

        }

    return EXIT_SUCCESS;
}
 



I can't understand some things here... Because SFML documentation sucks.

So my first question is what is:
what Selector.GetSocketReady(i) does really return?
And what is if(Socket == Listener)?
And why doesn't Socket always equal to Listener?
This is soo strange. I don't get it. It is equal to Listener just when someone connects, but after that its not...

Could someone help me understand this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Could someone explain me this?
« Reply #1 on: June 22, 2012, 09:54:29 pm »
First off I advise you to use SFML 2.0.

I can't understand some things here... Because SFML documentation sucks.
The documentatino doesn't suck! :o
It just requires a bit knowledge on C++ and how code works together to understand some things.
Also if it would suck, you think I could answer your question although I've never done anything with SFML 1.6 and never done anything with networking?


what Selector.GetSocketReady(i) does really return?
It returns Type as be seen here.v
To understand what Type is you need some knowledge in C++ to see that sf::Selector<Type> is a template that works with diffrent types.
You're using sf::SelectorTCP which is a typedef and defined in the header file here (line 00111).

And what is if(Socket == Listener)?
For this you have to know how networking works and what a Socket and Listener is. I myself do not have much knowledge about this, but the documentation states for a Selector/Listener:
Quote
Selector allow reading from multiple sockets without blocking.
It's a kind of multiplexer

And the if statement checks if the socket you've defined and the selector are the same.

And why doesn't Socket always equal to Listener?
As stated above a listener is a non-blocking selector of sockets. In the example you're using only one socket, thus your listener and your socket will always be the same.

This is soo strange. I don't get it. It is equal to Listener just when someone connects, but after that its not...
So your basic problem is not that the documentation sucks, but that your knowledge with networking sucks (like mine). Go and Google a bit. SFML was developed as an simple API for some functionallities, the documentation explains what the code is for, but how you now have to adapt the API to your application so that it does what you want is not of concerne for the documentation. There are some tutorials that could enlight you.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

pufixas

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Could someone explain me this?
« Reply #2 on: June 23, 2012, 08:33:24 am »
Ok, but after int Sockets = Selector.Wait(); I have put cout << "Sockets: " << Sockets << endl;
and it always show that it has only 1 socket, even after I connect one more.
So Selector.Add(Listener); would be first socket, and Selector.Add(Client); should be second, but it always shows only 1 socket...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Could someone explain me this?
« Reply #3 on: June 23, 2012, 10:54:15 am »
Wait() returns the number of sockets that are ready for reading, not the total number of sockets that the selector contains. It would be very hard to get two sockets ready at the exact same moment (you'd need at least two clients, and a lot of luck).

You should really learn more about the basics of networking before trying to do something with SFML. The API doc and tutorials won't teach you how to write a network program.
« Last Edit: June 23, 2012, 10:55:58 am by Laurent »
Laurent Gomila - SFML developer

pufixas

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Could someone explain me this?
« Reply #4 on: June 24, 2012, 05:10:53 pm »
Wait() returns the number of sockets that are ready for reading, not the total number of sockets that the selector contains. It would be very hard to get two sockets ready at the exact same moment (you'd need at least two clients, and a lot of luck).

You should really learn more about the basics of networking before trying to do something with SFML. The API doc and tutorials won't teach you how to write a network program.
Do you have any recommendations? Sites, forums maybe?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Could someone explain me this?
« Reply #5 on: June 24, 2012, 05:42:47 pm »
No, sorry. I learned this stuff a long time ago :P
Laurent Gomila - SFML developer

 

anything