uvw icon indicating copy to clipboard operation
uvw copied to clipboard

tcp client can't receive data

Open Seazer-x opened this issue 7 months ago • 2 comments

    loop_ = uvw::loop::get_default();
    tcp_handle_ = loop_->resource<uvw::tcp_handle>();
    tcp_handle_->init();

    tcp_handle_->on<uvw::error_event>([this](const uvw::error_event &event, uvw::tcp_handle &h) {
        LOG(ERROR) << "Error: " << event.what();
    });

    tcp_handle_->on<uvw::connect_event>([this](const uvw::connect_event &, uvw::tcp_handle &h) {
        LOG(INFO) << "Successfully connected to " << lidar_info->IP;
        isConnected = true;
    });

    tcp_handle_->on<uvw::data_event>([this](const uvw::data_event &data, uvw::tcp_handle &h) {
        LOG(INFO) << "Received data: " << data.length;
        Process(data.data.get(), data.length);
    });

    tcp_handle_->connect(lidar_info->IP, stoi(lidar_info->Port));

Why can't I receive the messages sent from the server? I have successfully connected to the server.

Image

Image

Seazer-x avatar May 24 '25 15:05 Seazer-x

Can you provide a minimal, reproducible example, please? It's hard to know what's wrong otherwise. Thanks.

skypjack avatar May 26 '25 21:05 skypjack

I've been troubleshooting something similar for a couple of hours and I could just be dumb, but I found that none of the data_events would fire for a tcp_handle unless you read() once after accepting it:

int main() {
    auto loop = uvw::loop::get_default();
    auto tcp = loop->resource<uvw::tcp_handle>();

    tcp->on<uvw::error_event>([](const uvw::error_event &, uvw::tcp_handle &) { /* something went wrong */ });

    tcp->on<uvw::listen_event>([](const uvw::listen_event &, uvw::tcp_handle &srv) {
        std::cout << "listen event fired" << std::endl;
        std::shared_ptr<uvw::tcp_handle> client = srv.parent().resource<uvw::tcp_handle>();
        client->on<uvw::end_event>([](const uvw::end_event &, uvw::tcp_handle &client) { client.close(); });
        client->on<uvw::data_event>([](const uvw::data_event &, uvw::tcp_handle &) {
            std::cout << "Data received" << std::endl;
        });
        srv.accept(*client);
        client->read();
    });

    tcp->bind("127.0.0.1", 4242);
    tcp->listen();
    loop->run();
    return 0;
}

e.g., without client->read() above, the data events just never happen, which seems counter intuitive. I'm curious if OP is running into something similar since I don't see any calls to read() in that snippet.

Algebro7 avatar Aug 22 '25 16:08 Algebro7