1
Network / 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:
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:
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?
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
}
#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
}
#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?