SFML community forums

Help => Network => Topic started by: iride on April 15, 2015, 06:20:57 am

Title: accepting a socket that has already been accepted
Post by: iride on April 15, 2015, 06:20:57 am
 What happens when I accept a socket that has already been accepted?
will it just continue to return sf::Socket::Done?
listener.accept(socket); //accepted
listener.accept(socket); //??

Title: Re: accepting a socket that has already been accepted
Post by: Gambit on April 15, 2015, 11:12:43 am
Why would you need to do this? If a socket has been accepted, any request to be re-accepted will be ignored until the original connection is terminated. Do not confuse a socket connection with a connection from the same computer. A single computer can have multiple socket connections to the same server.
Title: Re: accepting a socket that has already been accepted
Post by: binary1248 on April 15, 2015, 11:23:48 am
Like Gambit already said, there is little reason to do this in practice, especially if like in your example, those 2 statements are really executed one after the other.

The first statement will block until an incoming connection arrives and accept it into the socket object.

The second statement will block until an incoming arrives, disconnect the connection currently in the socket object, and populate it with the new connection.

What you essentially end up doing is prevent multiple connections from ever being processed simultaneously. As soon as another connection request arrives, the previous one will be disconnected. This is all disregarding the fact that if you are running this in a single thread, it would block your whole application until the second connection attempt is made anyway.

Just don't do this, it is not only bad practice, but it really has no purpose and might not even do what you expected.
Title: Re: accepting a socket that has already been accepted
Post by: iride on April 15, 2015, 08:38:44 pm
If a socket has been accepted, any request to be re-accepted will be ignored until the original connection is terminated.

As soon as another connection request arrives, the previous one will be disconnected.

I think these are 2 conflicting responses. So does the previous connection get disconnected or not?

Title: Re: accepting a socket that has already been accepted
Post by: Hiura on April 15, 2015, 09:12:38 pm
Binary's right: the socket is first closed and connected to the new source. (source) (https://github.com/SFML/SFML/blob/master/src/SFML/Network/TcpListener.cpp#L118-L119)
Title: Re: accepting a socket that has already been accepted
Post by: Jesper Juhl on April 15, 2015, 10:57:53 pm
Allow me to suggest some reading material:  UNIX Network Programming (http://www.unixnetworkprogramming.com/).