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

Author Topic: Socket connect max time  (Read 3799 times)

0 Members and 1 Guest are viewing this topic.

Merulo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Socket connect max time
« 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.

« Last Edit: February 05, 2016, 08:04:16 pm by Merulo »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Socket connect max time
« Reply #1 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"?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Merulo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Socket connect max time
« Reply #2 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/

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/
 
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/

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

« Last Edit: February 09, 2016, 12:45:52 pm by Merulo »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Socket connect max time
« Reply #3 on: February 09, 2016, 03:58:33 pm »
Can you provide the complete but minimal source code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Merulo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Socket connect max time
« Reply #4 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: Socket connect max time
« Reply #5 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Merulo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Socket connect max time
« Reply #6 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)

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Socket connect max time
« Reply #7 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?
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

Merulo

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Socket connect max time
« Reply #8 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