编辑:看起来不是我的代码而是构建环境。这有好有坏,因为现在我知道代码没问题,但不知道如何调试环境。这里有什么建议吗?请记住,我在这台计算机上没有管理员权限。
我被困在试图让简单的代码在 FreeBSD 下工作。这是 Boost 1.64 asio 对 async_accept
的调用,但不正常。相同的代码在 Windows 下工作正常,但在 FreeBSD 下它接受客户端连接(客户端上的连接调用成功)但从不调用其处理程序。甚至不知道如何处理这个。 (请注意,与其他相关问题不同,我会调用 io_service.run()
)请帮忙。
显示问题的自包含代码:
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
namespace asio = boost::asio;
namespace ph = asio::placeholders;
namespace sys = boost::system;
using asio::ip::tcp;
static void accept_handler(const sys::error_code& error)
{
// THIS IS NEVER CALLED UNDER FREEBSD
if (error)
std::cout << "failed to connected to server" << std::endl;
else
std::cout << "connected to server" << std::endl;
}
int main(int argc, char* argv[])
{
if (argc < 3)
{
std::cerr << "Usage: accept_test <port> <1 for async and 0 for sync accept>" << std::endl;
return 1;
}
asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), atoi(argv[1])));
std::cout << "waiting for server connection ";
tcp::socket sock(io_service);
if (argv[2][0] == '1')
{
//THIS WORKS UNDER WIN BUT DOESN'T CALL HANDLER UNDER FREEBSD
std::cout << "using async accept..." << std::endl;
acceptor.async_accept(sock, boost::bind(&accept_handler, ph::error));
}
else
{
//THIS WORKS FINE UNDER BOTH WIN AND FREEBSD
std::cout << "using sync accept..." << std::endl;
sys::error_code error;
acceptor.accept(sock, error);
if (error)
std::cout << "failed to connected to server" << std::endl;
else
std::cout << "connected to server" << std::endl;
}
io_service.run();
return 0;
}
最佳答案
我怀疑您的构建环境有问题。同步和异步都适用于:
Target: x86_64-unknown-freebsd12.0
Thread model: posix
InstalledDir: /usr/bin
build 使用
# clang++ -std=c++11 -o test -isystem /usr/local/include/ ./test.cpp -L /usr/local/lib -lboost_system
结果二进制:
# ldd test
test:
libboost_system.so.1.64.0 => /usr/local/lib/libboost_system.so.1.64.0 (0x80083c000)
libc++.so.1 => /usr/lib/libc++.so.1 (0x800a3f000)
libcxxrt.so.1 => /lib/libcxxrt.so.1 (0x800d06000)
libm.so.5 => /lib/libm.so.5 (0x800f24000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x801151000)
libc.so.7 => /lib/libc.so.7 (0x801367000)
libthr.so.3 => /lib/libthr.so.3 (0x80172e000)
构建环境的粗略说明:
vagrant init freebsd/FreeBSD-12.0-CURRENT
vagrant up --provider virtualbox
vagrant ssh
su
pkg install lang/clang-devel
pkg install devel/boost-all
# copy test.cpp
关于c++ - Boost asio async_accept 在 Windows 下工作但在 FreeBSD 下失败。怎么了?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44565222/