SFML community forums
Help => Network => Topic started 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
sf::TcpListener listener;
listener.listen( 1234 );
sf::TcpSocket client;
listener.accept( client );
cout << "Connected" << endl;
Client
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.
-
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"?
-
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:
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?
-
Can you provide the complete but minimal source code?
-
Whole code:
Server
#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
#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.
-
You never check the returned status code, so you can't just assume that because it progressed, the client connected.
-
Then tell my why it didn't connect.
That's the whole point of this thread.
Why
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
socket.connect("127.0.0.1", 1234);
this DOESconnect to the server. (status = Done, remote address = 127.0.0.1)
-
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?
-
Well, as you can see on the screenshots it takes 0ms to connect. But i tried also version with 1s and 5s