SFML community forums

Help => Network => Topic started by: Merulo on February 05, 2016, 08:02:38 pm

Title: Socket connect max time
Post by: Merulo on February 05, 2016, 08:02:38 pm
I have a problem with setting maximum time for socket to connect.

There is code:
Server
Quote
sf::TcpListener listener;
listener.listen( 1234 );
sf::TcpSocket client;
listener.accept( client );
cout << "Connected" << endl;

Client
Quote
sf::Time Timeout =sf:: milliseconds(300);
sf::TcpSocket socket;
socket.connect( 127.0.0.1, 1234, Timeout );

If I remove the "Timeout" it works fine. It also works fine if i set it to sf::Time::Zero. But the problem is that it's too long.

Title: Re: Socket connect max time
Post by: eXpl0it3r on February 09, 2016, 10:16:20 am
If it takes "too long" chances are it's not connecting at all. You need to be a bit more precise with the actual problem description. What is happening? What should be happening? What do you mean by "working fine"? How long is "too long"?
Title: Re: Socket connect max time
Post by: Merulo on February 09, 2016, 12:42:30 pm
EDIT: Sorry for weird syntax on the screenshots, programming in the morning can end up strange.

Normal connection(to prove i know what i am doing[or at least i think so]):
http://postimg.org/image/o1q9mqzqh/ (http://postimg.org/image/o1q9mqzqh/)

Default socket.connect( 127.0.0.1, 1234) takes about 1034ms (It's the sf::Time::Zero for my OS) to go to next line of code if the server doesn't exist - which means connection can't be established.

http://postimg.org/image/sgve6a227/ (http://postimg.org/image/sgve6a227/)
 
But i think the sf::Time::Zero (the 1036ms) is too long. I want to have it set for 300ms. So i used the code below:
Quote
sf::Time Timeout =sf:: milliseconds(300);
sf::TcpSocket socket;
socket.connect( 127.0.0.1, 1234, Timeout );

Problem is simple. The "socket.connect( 127.0.0.1, 1234, Timeout );" never connects to the server.
If i replace it with "socket.connect( 127.0.0.1, 1234)" it does.

http://postimg.org/image/7vvivrf29/ (http://postimg.org/image/7vvivrf29/)

Why the one with "Timeout" doesn't work?

Title: Re: Socket connect max time
Post by: eXpl0it3r on February 09, 2016, 03:58:33 pm
Can you provide the complete but minimal source code?
Title: Re: Socket connect max time
Post by: Merulo on February 09, 2016, 04:57:43 pm
Whole code:
Server
Quote
#include <SFML/Network.hpp>
#include <iostream>

using namespace std;

int main(){
    sf::TcpListener listener;
    listener.listen(1234);
    sf::TcpSocket client;
    listener.accept(client);
    cout<<"Connected - server"<<endl;
    getchar();
return 0;
}

Client
Quote

#include <SFML/Network.hpp>
#include <iostream>
#include <ctime>

using namespace std;

int main(){
    sf::Time Timeout = sf::milliseconds( 300 );
    sf::TcpSocket socket;
    socket.connect("127.0.0.1", 1234, Timeout);
    clock_t start = clock();
    printf( "It took ms to connect %lu ms\n", clock() - start );
    getchar();

 return 0;
}

It was part of a bigger code, but when i realized that it was not working i removed rest. The basic, which is also shown on the screenshots, still doesn't work.
Title: Re: Socket connect max time
Post by: eXpl0it3r on February 10, 2016, 02:07:39 am
You never check the returned status code, so you can't just assume that because it progressed, the client connected.
Title: Re: Socket connect max time
Post by: Merulo on February 10, 2016, 12:16:29 pm
Then tell my why it didn't connect.
That's the whole point of this thread.

Why
Quote
socket.connect("127.0.0.1", 1234, Timeout);

this DOES NOT connect to the server. NEVER. (status = Disconnected, remote address = 0.0.0.0)

whereas
Quote
socket.connect("127.0.0.1", 1234);

this DOESconnect to the server. (status = Done, remote address = 127.0.0.1)
Title: Re: Socket connect max time
Post by: AlexxanderX on February 10, 2016, 04:25:16 pm
I don't know if I'm correctly in what I will say but what if the client need more than Timeout to connect to the server?
Title: Re: Socket connect max time
Post by: Merulo on February 10, 2016, 05:57:52 pm
Well, as you can see on the screenshots it takes 0ms to connect. But i tried also version with 1s and 5s