chriskohlhoff / asio

Asio C++ Library
http://think-async.com/Asio
4.96k stars 1.22k forks source link

"Undefined error: 0" on the Mac OS 10.10 and 10.11 #121

Open ddovod opened 8 years ago

ddovod commented 8 years ago

Hello. I have some issue with examples from src/examples/cpp11/echo/async_tcp_echo_server.cpp and blocking_tcp_echo_client.cpp (also my own code works bad).

When I try to connect/read/write (blocking and async operations), i get "Undefined error: 0" in the asio::error_code parameter. Interesting thing is that data transfers successfully. Only problem is Undefined error and infinity reads with zero bytes read.

Compiling with xCode and standalone clang with command like clang++ -std=c++11 -Ipath/to/asio/include async_tcp_echo_server.cpp -oserver

Also using ASIO_STANDALONE definition.

Could you help me please? I'll check boost version soon. Thank you a lot!

ddovod commented 8 years ago

Addition: Boost version also causes Undefined error: 0. iOS (device and simulator) has this error too.

ddovod commented 8 years ago

Another addition: If there's nobody listening on the port I try to connect to, "Connection refused" error is returned to the async_connect callback. So "Undefined error: 0" appears only if somebody is listening port on the other side.

ddovod commented 8 years ago

Up!

ddovod commented 8 years ago

Up!

ddovod commented 8 years ago

Up!

ddovod commented 8 years ago

Up!

ddovod commented 8 years ago

Up!

ddovod commented 8 years ago

Up!

ddovod commented 8 years ago

Up!

ddovod commented 8 years ago

Update: examples works fine, sorry, my bad. Assuming there's somebody who is listening 8888 port on the localhost. The code below works bad on iOS/mac os:

    std::string m_address = "127.0.0.1";
    int m_port = 8888;
    asio::io_service m_service;
    asio::ip::tcp::socket m_socket(m_service);
    asio::ip::tcp::endpoint ep(asio::ip::address::from_string(m_address), m_port);
    m_socket.async_connect(ep, [] (const asio::error_code& err) {
        std::cout << "onConnect -> " << err.message() << std::endl;
    });
    m_service.run();

Output on a Debian:

onConnect -> Success

Output on a Mac OS:

onConnect -> Undefined error: 0
sywe1 commented 7 years ago

I think undefined error means no error, the error object is not initialized.

germandiagogomez commented 7 years ago

I am having the same problem. Any workaround?

ddovod commented 7 years ago

@germandiagogomez I came to the fact that I used asio incorrectly, just check that your code conforms to docs and restrictions

patrickelectric commented 4 years ago

@ddovod can you post the fixed code ?

ddovod commented 4 years ago

Oh, so much time has passed, I'm not sure what exactly I was doing wrong. Looking at the code above I would say that you can try to use asio::async_connect(m_socket.lowest_layer(), ...) instead of m_socket.async_connect(...). My connection code with asio now loos like this:

        auto query = std::make_shared<asio::ip::tcp::resolver::query>(m_host, m_port);
        m_resolver.async_resolve(*query, [this, query](const auto& error, auto iterator) {
            if (error) {
                onError();
            } else {
                asio::async_connect(m_socket.lowest_layer(), iterator, [this](const auto& connectError, const auto&) {
                    if (connectError) {
                        onError();
                    } else {
                        onConnectionFinished();
                    }
                });
            }
        });

        m_socketThread = std::thread([this] { m_ioService.run(); });

Hope this helps