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

Author Topic: Tcp server dealing with clients disconnecting  (Read 7404 times)

0 Members and 1 Guest are viewing this topic.

Tytan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Tcp server dealing with clients disconnecting
« 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:
  • Using a deque instead of a vector to remove elements from the list, and move the ones above to it's place (Same error)
  • (Probs a dumb idea kek) Using a second vector to represent the other one, filled with ints that represent indexes in the client vector, allowing my to do clients[indexs].blah_blah

Extra notes:
  • Clients is a vector of sf::TcpSockets
  • The disconnect check thing is in a for loop iterating though each client connected
  • numClients is how many people are connected



Please don't treat me like people on stack overflow do xD
« Last Edit: August 23, 2017, 01:25:04 pm by Tytan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Tcp server dealing with clients disconnecting
« Reply #1 on: August 23, 2017, 01:28:46 pm »
A socket can't be copied, so don't try to copy it. ;)

Two possible solutions:
  • Use a std::vector<std::unique_ptr<sf::TcpSocket>>
  • Use a std::list<sf::TcpSocket>
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tytan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tcp server dealing with clients disconnecting
« Reply #2 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 )

Tytan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tcp server dealing with clients disconnecting
« Reply #3 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?

Tytan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tcp server dealing with clients disconnecting
« Reply #4 on: September 26, 2017, 06:37:10 am »
Please someone help me :(
This is very annoying...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Tcp server dealing with clients disconnecting
« Reply #5 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tytan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tcp server dealing with clients disconnecting
« Reply #6 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)

Gleade

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Tcp server dealing with clients disconnecting
« Reply #7 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. 

Tytan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tcp server dealing with clients disconnecting
« Reply #8 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Tcp server dealing with clients disconnecting
« Reply #9 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tytan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Tcp server dealing with clients disconnecting
« Reply #10 on: September 28, 2017, 12:59:43 pm »
Thank you so much!
I love you right now! :D