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

Author Topic: A really curious problem and some simple doubts  (Read 3325 times)

0 Members and 1 Guest are viewing this topic.

Apolo12

  • Newbie
  • *
  • Posts: 1
    • View Profile
A really curious problem and some simple doubts
« on: July 09, 2014, 09:43:55 pm »
Hello to everyone,  I have been trying to use sockets with SFML, but I had a really curious problem, so I used the code from the tutorial of the official website: http://www.sfml-dev.org/documentation/2.1/classsf_1_1SocketSelector.php#details
There is the code example for selector, I tried to use it (adding std::cout to know if the program works), but it doesn't work:
#include <SFML/Network.hpp>
#include <list>
#include <iostream>

bool running;

int main(int argc, char** argv){
        sf::TcpListener listener;
        listener.listen(30000);
        running = true;
        std::list<sf::TcpSocket*> clients;
        sf::SocketSelector selector;
        selector.add(listener);
        std::cout << "listener added to selector";
        while (running)
        {

                if (selector.wait())
                {
                    std::cout << "inside conditional if(selector.wait())";
                        if (selector.isReady(listener))
                        {
                                sf::TcpSocket* client = new sf::TcpSocket;
                                if (listener.accept(*client) == sf::Socket::Done)
                                {
                                          clients.push_back(client);
                                          selector.add(*client);
                                }
                                else
                                 {
                                          delete client;
                                 }      
                        }
                        else
                        {
                                for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
                                {
                                        sf::TcpSocket& client = **it;
                                        if (selector.isReady(client))
                                        {
                                                sf::Packet packet;
                                         if (client.receive(packet) == sf::Socket::Done)
                                                {
                                                   // ...
                                                }
                                        } // END_IF selector.isReady(client)
                                }  // END_FOR
                         } // END_ELSE selector.isReady(listener)
                 } // END_IF selector.wait()
         } // END_WHILE RUNNING  
}

Okey, that code doesn't work on my computer (doesn't work means that the messages aren't showed, std::cout << ...)  Buuuut, if I change the std::cout << "somestring"; statement to this one: std::cout << "somestring" << std::endl; it works!!!!
For example,  this code works:
#include <SFML/Network.hpp>
#include <list>
#include <iostream>

bool running;

int main(int argc, char** argv){
        sf::TcpListener listener;
        listener.listen(30000);
        running = true;
        std::list<sf::TcpSocket*> clients;
        sf::SocketSelector selector;
        selector.add(listener);
        std::cout << "listener added to selector" << std::endl;
        while (running)
        {

                if (selector.wait())
                {
                    std::cout << "inside conditional if(selector.wait())" << std::endl;
                        if (selector.isReady(listener))
                        {
                                sf::TcpSocket* client = new sf::TcpSocket;
                                if (listener.accept(*client) == sf::Socket::Done)
                                {
                                          clients.push_back(client);
                                          selector.add(*client);
                                }
                                else
                                 {
                                          delete client;
                                 }      
                        }
                        else
                        {
                                for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it)
                                {
                                        sf::TcpSocket& client = **it;
                                        if (selector.isReady(client))
                                        {
                                                sf::Packet packet;
                                         if (client.receive(packet) == sf::Socket::Done)
                                                {
                                                   // ...
                                                }
                                        } // END_IF selector.isReady(client)
                                }  // END_FOR
                         } // END_ELSE selector.isReady(listener)
                 } // END_IF selector.wait()
         } // END_WHILE RUNNING  
}

I don't know why it is happening that, but it is so curious. I use Netbeans 7.0.1, SFML2.1 on Ubuntu 12.04 LTS.



Aaand, another doubt I have is, if I wanted to create a server with a lot of connections, I mean over 500 or more connections, even 1000 clients connected and sending messages and connecting new clients, disconnecting... should I use sfml sockets? or maybe they are not as powerful, and also, should I use selector or threads?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: A really curious problem and some simple doubts
« Reply #1 on: July 09, 2014, 09:49:06 pm »
std::cout is (usually) buffered for performance, so until you flush the stream by outputting std::endl or std::flush (or in some cases reach some internal buffering limit) you can't expect to actually see the output.
If you want to see output at once, unbuffered, then write to std::cerr instead.

Regarding threads, read this:
http://en.sfml-dev.org/forums/index.php?topic=15430.msg109615#msg109615
http://en.sfml-dev.org/forums/index.php?topic=15443.msg109740#msg109740
« Last Edit: July 10, 2014, 08:25:19 am by Jesper Juhl »

 

anything