我正在努力使用同步客户端代码接收数据,该同步客户端代码使用带有 Boost.Asio 的数据报 Unix 套接字。
服务器似乎工作正常,因为如果我用netcat连接到它,我会收到数据。但是,当尝试使用下面的代码时,它会卡在 receive_from() 中。strace显示调用了 receive_from() 系统调用但没有收到任何内容,而服务器上的strace显示正在尝试向客户端发送数据但它没有这样做。
boost::asio::io_service io_service;
boost::asio::local::datagram_protocol::socket socket(io_service);
socket.open();
cmd::cmd cmd;
cmd.type = cmd::cmdtype::request;
cmd.id = cmd::cmdid::dumptop;
boost::asio::local::datagram_protocol::endpoint receiver_endpoint("socket.unix");
/* The server receives this data successfully */
socket.send_to(
boost::asio::buffer(reinterpret_cast<char*>(&cmd),
sizeof(cmd)),
receiver_endpoint
);
boost::array<char, 128> recv_buf;
boost::asio::local::datagram_protocol::endpoint ep;
/* When the server sends data, nothing is received here.
Maybe it's an issue with the endpoint??? */
size_t len = socket.receive_from(
boost::asio::buffer(recv_buf), ep);