Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

program blocked when tcp client connection to a tcp server failed #15

Closed
roboticanyair opened this issue Feb 21, 2021 · 4 comments
Closed

Comments

@roboticanyair
Copy link

Hi.
first of all - great work. the library is easy to use and understand.

When i try to connect to a tcp server that does not exist, the program get stuck after "Connection failed to the host" message is printed.
to reproduce that I ran the tcp-client.cpp example, I added print inside the while loop. It never gets printed.

while (input != "exit")
    {
        std::cout << "loop" << std::endl;
    
        tcpSocket.Send(input);
        getline(cin, input);
    }
@eminfedar
Copy link
Owner

Hi, did you remove the getline() above the loop?

    string input;
    getline(cin, input); // <---------------------- This blocks before entering the while loop.
    while (input != "exit")
    {
        tcpSocket.Send(input);
        getline(cin, input);
    }

@roboticanyair
Copy link
Author

oops. my bad...

but it still happens in my code.
I have a timer that timer runs once every second. in the callback of the timer I'm trying to connect to the server each time.
when the connection succeeds - a message is printed and the timer is canceled. if it fails to connect to the server a message should be printed and another connection attempt should occur when the timer is being fired.
to verify that the program hangs, I created a second timer that runs every seconds and only prints the following message:
"another callback"
I have added a print in the beginning of the connecting timer callback, the prints "callback" and the current unix time in seconds.

this is the code inside the timer's callback:

std::cout.setf(std::ios::fixed);
std::cout << "callback " << std::setprecision(0)  << base_node_->now().seconds() << std::endl;;
command_client_.Connect(cell_address_, command_client_port_, [&] {
                                std::cout << "Connected to cell " << name_ << " server successfully." << std::endl;
        
                                connecting_timer_->cancel();
        
                            },
                            [&](int errorCode, const std::string &errorMessage) {
                                // CONNECTION FAILED
                                std::cout << "failed to connect to server : error " <<
                                          std::to_string(errorCode) << " : " << errorMessage << std::endl;
        
        
                            });

the output is:

callback 1613910931
failed to connect to server : error 110 : Connection failed to the host.
callback 1613911061
failed to connect to server : error 110 : Connection failed to the host.
callback 1613911192
failed to connect to server : error 110 : Connection failed to the host.
callback 1613911323
failed to connect to server : error 110 : Connection failed to the host.
callback 1613911454
failed to connect to server : error 110 : Connection failed to the host.
callback 1613911586
failed to connect to server : error 110 : Connection failed to the host.
callback 1613911717

it always hangs for 131 seconds (ish) and the callback of the second timer is never being called.
If i remove the part of the connection from the callback, it runs as it should and prints the "callback" message every second and "another callback" is printed every 2 seconds.

when there is listening server - every thing is OK.

when I run it in debug mode it's clear the it hangs inside the Connect function.

again, thanks for the hard work!

@eminfedar
Copy link
Owner

eminfedar commented Feb 21, 2021

It is probably about "Connection Timeout". It is waiting for timeout when there is no server to reach.

I have added tcpSocket.setTimeout(secs) method and pushed today. (537d4d4)

Can you pull the project and try it again with setting a timeout value?

Example:

// Set timeout seconds:
tcpSocket.setTimeout(1);

// Then try to connect:
tcpSocket.Connect(...);

@roboticanyair
Copy link
Author

It looks like the problem is solved! thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants