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

Author Topic: std::vector of sockets [c++]  (Read 6174 times)

0 Members and 1 Guest are viewing this topic.

paz

  • Newbie
  • *
  • Posts: 2
    • View Profile
std::vector of sockets [c++]
« on: October 16, 2016, 03:24:27 pm »
I've constructed a vector of sf::TcpSockets however I can't push a socket back onto the vector.

Running:
  • Fedora 24 64 bit
  • libstdc++.so.6.0.22
  • sfml-2.3.2

Error:
Code: [Select]
/usr/include/c++/6.2.1/ext/new_allocator.h:120:4: error: use of deleted function ‘sf::TcpSocket::TcpSocket(const sf::TcpSocket&)’
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code:
       
    std::vector<sf::TcpSocket> sockets;

        sf::TcpListener listener;

        // bind the listener to a port
        if (listener.listen(53000) != sf::Socket::Done)
        {
            // error...
        }

    while(true)
    {
        // accept a new connection
        sf::TcpSocket client;
        if (listener.accept(client) != sf::Socket::Done)
        {
        // error...
        }
        else
        {
            sockets.push_back(client);

            for(auto &client : sockets)
            {
/*----------------------- snip -----------------------------------*/
           

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: std::vector of sockets [c++]
« Reply #1 on: October 16, 2016, 03:37:10 pm »
SFML socket classes are neither copyable nor movable (will change in SFML 3), so they can't be stored directly in a container. A simple solution is to store pointers to sockets.

std::vector<std::unique_ptr<sf::TcpSocket>> sockets;
Laurent Gomila - SFML developer

paz

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: std::vector of sockets [c++]
« Reply #2 on: October 16, 2016, 03:40:05 pm »
Ah okay thanks for the quick reply, my fault for not reading the error properly.

Maybe update the docs to make this a bit clearer?

http://www.sfml-dev.org/tutorials/2.3/network-socket.php

Quote
A selector is not a socket container. It only references (points to) the sockets that you add, it doesn't store them. There is no way to retrieve or count the sockets that you put inside. Instead, it is up to you to have your own separate socket storage (like a std::vector or a std::list).

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: std::vector of sockets [c++]
« Reply #3 on: October 16, 2016, 07:20:11 pm »
I know it's OT a bit, but if I were you, I would not write server in C++. SFML/Network is great for p2p connections and clients, but C++ itself doesn't really fit server role. If you know JS, node.js is great to write such applications as it scales well and has simillar syntax.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: std::vector of sockets [c++]
« Reply #4 on: October 17, 2016, 05:41:41 am »
(will change in SFML 3)

Sorry for detract the thread but, ¿Any hints/plans about when SFML 3 will be released?
I would like a spanish/latin community...
Problems building for Android? Look here

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: std::vector of sockets [c++]
« Reply #5 on: October 17, 2016, 07:42:49 am »
No. It hasn't started yet, so we're far from talking about a release date.
Laurent Gomila - SFML developer

 

anything