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

Author Topic: SFML Server doesn't accept more than one client  (Read 3109 times)

0 Members and 2 Guests are viewing this topic.

FlyPT

  • Newbie
  • *
  • Posts: 2
    • View Profile
SFML Server doesn't accept more than one client
« on: July 27, 2015, 10:02:28 am »
Hi everyone im trying to make a server for the first time and i've got a problem.
Apparently only one client can join.
I dont really know what is happening.

#include <SFML\Network.hpp>
#include <iostream>

unsigned short port = 6702;
bool open = true;

int main() {
        sf::TcpListener listener;
        std::vector<sf::TcpSocket*> clients;

        if(listener.listen(port) != sf::Socket::Done) {
                return -1;
        }

        std::cout << "Waiting for connections...\n";

        while (open) {
                sf::TcpSocket* newConnection = new sf::TcpSocket;
                if(listener.accept(*newConnection) == sf::Socket::Done) {
                        clients.push_back(newConnection);
                        sf::Packet confirmation;
                        confirmation << true;
                        clients[clients.size()-1]->send(confirmation);
                        std::cout << "Player Joined! Players Online: " << clients.size() << "\n";
                }
                else {
                        delete newConnection;
                }

                for (auto& client : clients) {
                        sf::Packet packet;
                        sf::Socket::Status status = client->receive(packet);
                        if (status == sf::Socket::Done) {
                                for (auto& client2 : clients) {
                                        if(client != client2) {
                                                std::cout << "a2";
                                        }
                                }
                        }
                        else if (status == sf::Socket::Disconnected) {

                        }
                }
        }
}

Thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML Server doesn't accept more than one client
« Reply #1 on: July 27, 2015, 10:22:45 am »
Quote
        for (auto& client : clients) {
            sf::Packet packet;
            sf::Socket::Status status = client->receive(packet);
Your main loop will only be able to end one iteration after all the connected clients have sent something. If they don't, this code will block and your app will be stuck waiting for data.

In such a loop, you can put all your sockets in non-blocking mode.
Laurent Gomila - SFML developer

FlyPT

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: SFML Server doesn't accept more than one client
« Reply #2 on: July 27, 2015, 08:59:06 pm »
I put every socket in non blocking but now it doesnt receive data from players.

Jim70

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: SFML Server doesn't accept more than one client
« Reply #3 on: September 22, 2015, 02:08:07 pm »
That's how tcp works. you could use a socket selector though.

gus

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML Server doesn't accept more than one client
« Reply #4 on: September 27, 2015, 11:30:25 am »
I think the tcp listener needs to be set to non blocking too right?

 

anything