SFML community forums

Help => Network => Topic started by: Tytan on August 23, 2017, 01:18:23 pm

Title: Tcp server dealing with clients disconnecting
Post by: Tytan on August 23, 2017, 01:18:23 pm
Oooookkayyyy, first off the code is for a server, but the problem is with me trying to remove a client from the list of connected clients.

Problematic code:
(click to show/hide)


Now wtf i'm trying to do:
When a client disconnects, I want to remove them from the list of clients (as said above), but the way I have set up the server makes this really annoying bug, basically if a client disconnects, it kicks the person that joined last.
The way I am trying to fix this is by moving the disconnected clients vector entry thingy to the back of the vector, so that it would continue server-ing normally.


Now the error I end up with by using the above code:

(click to show/hide)


What I've tried:

Extra notes:



Please don't treat me like people on stack overflow do xD
Title: Re: Tcp server dealing with clients disconnecting
Post by: eXpl0it3r on August 23, 2017, 01:28:46 pm
A socket can't be copied, so don't try to copy it. ;)

Two possible solutions:
Title: Re: Tcp server dealing with clients disconnecting
Post by: Tytan on August 24, 2017, 08:29:18 am
Thank you so much!
I think you saved a newb a lot of time with this ;P

Also, which one is better out of the two options? ( If one is better of course ;P )
Title: Re: Tcp server dealing with clients disconnecting
Post by: Tytan on August 24, 2017, 08:51:20 am
Also *2, I might go with the unique_ptr option, because with std::list (I think) I would have to remake my entire server loop thingy lol

Also *3, this line:
Quote
while (listener.accept(clients[numClients + 1].get()) == sf::Socket::Done)
Would be used to accept the newest conection, but I get an error saying that the initial value of reference to non-const must be an lvalue, what does this mean?
Title: Re: Tcp server dealing with clients disconnecting
Post by: Tytan on September 26, 2017, 06:37:10 am
Please someone help me :(
This is very annoying...
Title: Re: Tcp server dealing with clients disconnecting
Post by: eXpl0it3r on September 26, 2017, 12:13:15 pm
Quote
while (listener.accept(clients[numClients + 1].get()) == sf::Socket::Done)
Would be used to accept the newest conection, but I get an error saying that the initial value of reference to non-const must be an lvalue, what does this mean?
Did you google the error message?
It's a C++ error message and I bet you'll find a general explanation of it on StackOverflow or other places.

The best way to get some fast help, is to create a compilable and minimal example that demonstrates the issue. That way anyone can see the full scope and understand what is going on.

The error with the code here doesn't seem to make as much sense to me. It would mean that accept() expects a reference (which it does), but you're passing a r-value object (i.e. temporary variable), which you aren't really. Instead you're passing a raw pointer to the accept function. So that's why I feel the error isn't really describing the problem of the posted code.
Title: Re: Tcp server dealing with clients disconnecting
Post by: Tytan on September 28, 2017, 07:20:27 am
Quote
while (listener.accept(clients[numClients + 1].get()) == sf::Socket::Done)
Would be used to accept the newest conection, but I get an error saying that the initial value of reference to non-const must be an lvalue, what does this mean?
Did you google the error message?
It's a C++ error message and I bet you'll find a general explanation of it on StackOverflow or other places.

The best way to get some fast help, is to create a compilable and minimal example that demonstrates the issue. That way anyone can see the full scope and understand what is going on.

The error with the code here doesn't seem to make as much sense to me. It would mean that accept() expects a reference (which it does), but you're passing a r-value object (i.e. temporary variable), which you aren't really. Instead you're passing a raw pointer to the accept function. So that's why I feel the error isn't really describing the problem of the posted code.

Yes I'm an idiot for not googling it.
Sorry about that.

Anyways, I took that same code listener.accept code and put it in a new project.
Same problem...

(click to show/hide)

Might show this to stack overflow (And also prepare for insults lol)
Title: Re: Tcp server dealing with clients disconnecting
Post by: Gleade on September 28, 2017, 07:58:57 am
You're making some very basic mistakes here. You should probably look deeper into vectors and smart pointers. 
Title: Re: Tcp server dealing with clients disconnecting
Post by: Tytan on September 28, 2017, 09:41:55 am
Was discussing the problem with a friend and he told me that std::unique_ptr doesn't call the constructor for sf::TcpSocket, and that I need to make them own the objects.

As far as I know, I could do this by using:

(click to show/hide)

But I don't know how I would do this in my case since I'm making a vector of them and can't pass args to the constructor of std::unique_ptr...

You're making some very basic mistakes here. You should probably look deeper into vectors and smart pointers.

Is this the what you were talking about?
Title: Re: Tcp server dealing with clients disconnecting
Post by: eXpl0it3r on September 28, 2017, 12:12:15 pm
#include <iostream>
#include <vector>
#include <memory>
#include <SFML/Network.hpp>

int main()
{
    const int maxClients = 100;
    std::vector<std::unique_ptr<sf::TcpSocket>> clients;
    sf::TcpListener listener;
    std::unique_ptr<sf::TcpSocket> socket = std::make_unique<sf::TcpSocket>();

    while (clients.size() < maxClients && listener.accept(*socket) == sf::Socket::Done)
    {
        clients.push_back(std::move(socket));
        std::cout << "new guy connected" << std::endl;
       
        if (clients.size() < maxClients)
            socket = std::make_unique<sf::TcpSocket>();
    }
}

It's not a good idea to create 100 sockets and then potentially fill them with new connections. Instead you should just add new connections when they actually happen.
Title: Re: Tcp server dealing with clients disconnecting
Post by: Tytan on September 28, 2017, 12:59:43 pm
Thank you so much!
I love you right now! :D