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

Author Topic: Trying to make a server with multiple connections  (Read 4605 times)

0 Members and 1 Guest are viewing this topic.

Axenntio

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trying to make a server with multiple connections
« on: May 02, 2015, 11:27:31 pm »
Hi everyone ! I've got some weird problem with my code...
The first time, I've make an array and if a client disconnected, I push the last elements to overwrite the client disconnected
for(int i=0;i<coNumber /*Number of connected clients*/;i++){
    if(socket[i].receive(packet)!=sf::Socket::Done){
        coNumber--; //Remove one client
        for(int j=i;j<coNumber;j++){
            socket[i]=socket[i+1];
        }
    }
}
 

But got the awesome
NonCopyable error

So I've ask to Lukas/eXpl0it3r (Thanks to him !)
And suggest me to use
std::vector<std::unique_ptr<sf::TcpSocket>> socket;
 

instead of
sf::TcpSocketsocket[MAXCONNECTIONS];
 

play with somes pointers, and then get stuck...
An error occured with this line:
if(listener.accept(*socket[coNumber])==sf::Socket::Done && coNumber<MAXCONNEXIONS){
 

No error during compiling, but during the execution (Application not responding)
Error 255

So there is my actual code:

(click to show/hide)

Thanks for your help !
« Last Edit: May 02, 2015, 11:31:35 pm by Axenntio »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Trying to make a server with multiple connections
« Reply #1 on: May 03, 2015, 04:06:27 pm »
It isn't very clear what you need help with / what the problem is. So can you please clearly explain what the problem is and what you expect to happen?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Axenntio

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trying to make a server with multiple connections
« Reply #2 on: May 03, 2015, 09:07:36 pm »
Ok so, this line make my Application no responding
if(listener.accept(*socket[coNumber])==sf::Socket::Done && coNumber<MAXCONNEXIONS){
 

I know it because when I comment it the program run normaly.
This line is supposed to accept an connection from the client, but this make the code get the 255 error (not responding [The program crash])
listener.accept(*socket[coNumber])==sf::Socket::Done
 

I want to make a server for my clients, so multiple connections

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trying to make a server with multiple connections
« Reply #3 on: May 03, 2015, 09:11:12 pm »
Your socket vector is empty, so accessing socket[coNumber] is an undefined behaviour. You should learn more about std::vector (and possibly std::unique_ptr) before using them ;)
Laurent Gomila - SFML developer

Axenntio

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trying to make a server with multiple connections
« Reply #4 on: May 03, 2015, 10:44:09 pm »
Ow... Sorry for my mistakes :$
I didn't know std::vector and std::unique_ptr until eXplOit3r suggest them to me ^^'
I've also see the use of selector with the TCP socket ? This can make an Server multi-client you think ?

Edit: Seem to be usable only in SFML 1.6... There is an alternative for SFML 2.0 ?
« Last Edit: May 03, 2015, 10:48:08 pm by Axenntio »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Trying to make a server with multiple connections
« Reply #5 on: May 04, 2015, 01:07:10 am »
I've also see the use of selector with the TCP socket ? This can make an Server multi-client you think ?

Edit: Seem to be usable only in SFML 1.6... There is an alternative for SFML 2.0 ?
Where have you looked? In SFML 2.2, there is still sf::SocketSelector 8)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Axenntio

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trying to make a server with multiple connections
« Reply #6 on: May 04, 2015, 07:36:01 pm »
Yea but I use SFML 2.0 and not 2.2 :/

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Trying to make a server with multiple connections
« Reply #7 on: May 04, 2015, 07:49:04 pm »
sf::SocketSelector is there in 2.0 as well, judging by the old documentation. But why are you still using 2.0? As far as I know it has no advantages over 2.2.

Axenntio

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trying to make a server with multiple connections
« Reply #8 on: May 04, 2015, 08:41:49 pm »
OK let make an upgrade ^^' (Probably because I'm a dev' and dev' are a little bit lazy)

Edit: Okay, finally done ! I've use the SocketSelector (rename with not same function between 1.6 and 2.0) Now I've a multi-client server !
For those how need this, I just use this page: http://www.sfml-dev.org/documentation/2.0-fr/classsf_1_1SocketSelector.php

Thanks to SFML community forhis help ! <3
« Last Edit: May 04, 2015, 09:06:46 pm by Axenntio »