gpt4 book ai didi

c++ - async_accept 处理程序的用法

转载 作者:行者123 更新时间:2023-11-30 02:55:23 25 4
gpt4 key购买 nike

我自己写了一个名为asyncAccept的函数来封装标准的boost::asio::ip::acceptor::async_accept,但是当我运行我的程序时出现了问题。抛出错误并打印“参数无效”。好像表达有问题:

boost::bind(handle_accept, placeholders::error, socket, tcp_version, port_num, handler));

请问是什么问题。完整的代码附在下面。我的母语不是英语,所以请耐心等待~~非常感谢!

----------------------------------------asyncAccept.cpp-------------------------------
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include "impl/asyncAccept.h"

using namespace boost::asio;

namespace asyncTcp{

error_type illegal_version = -1;
error_type accept_error = -2;
const int TCPV4 = 4;
const int TCPV6 = 6;

io_service io_server;
ip::tcp::acceptor _acceptor(io_server);

ip::tcp::socket _socket(io_server);
void *servSocket = &_socket;

void handle_accept(const boost::system::error_code& error, void* socketPtr, const int tcp_version, unsigned short port_num, callback handler);

void init_acceptor(const int tcp_version, unsigned short port_num);

//bind a socket with a port
void asyncAccept(void* socketPtr, const int tcp_version, unsigned short port_num, callback handler){

if (tcp_version != TCPV4 && tcp_version != TCPV6)
throw illegal_version;

init_acceptor(tcp_version, port_num);

ip::tcp::socket *socket = reinterpret_cast<ip::tcp::socket*>(socketPtr);

_acceptor.async_accept((*socket),
boost::bind(handle_accept, placeholders::error, socket, tcp_version, port_num, handler));

return;
}

void handle_accept(const boost::system::error_code& error, void* socketPtr, const int tcp_version, unsigned short port_num, callback handler){
if (!error){
//cycle server
handler();

asyncAccept(socketPtr, tcp_version, port_num, handler);
}
else
throw boost::system::system_error(error);
}

void init_acceptor(const int tcp_version, unsigned short port_num){
if (TCPV4 == tcp_version){
ip::tcp::endpoint ep(ip::tcp::v4(), port_num);
_acceptor.open(ip::tcp::v4());
_acceptor.bind(ep);
}
else{
ip::tcp::endpoint ep(ip::tcp::v6(), port_num);
_acceptor.open(ip::tcp::v6());
_acceptor.bind(ep);
}
}

void run_server(){
io_server.run();

return;
}

}//namespace



-----------------------------------impl/asyncAccept.h----------------------------------
#ifndef SERV_SOCKET_H
#define SERV_SOCKET_H

namespace asyncTcp{

#ifndef CALL_BACK
#define CALL_BACK
typedef void (*callback)();
#endif

#ifndef ERROR_TYPE
#define ERROR_TYPE
typedef int error_type;
#endif

extern error_type illegal_version;
extern error_type accept_error;
//extern void* io_server;
extern void* servSocket;
extern const int TCPV4;
extern const int TCPV6;

void asyncAccept(void* socketPtr, const int tcp_version, unsigned short port_num, callback handler);

void run_server();
}

#endif /* SERV_SOCKET_H */

最佳答案

您必须将 boost::asio::ip::tcp::acceptor 置于监听状态。这可以通过调用 boost::asio::ip::tcp::listen() 来完成。像这样:

void init_acceptor(const int tcp_version, unsigned short port_num){
if (TCPV4 == tcp_version){
ip::tcp::endpoint ep(ip::tcp::v4(), port_num);
_acceptor.open(ip::tcp::v4());
_acceptor.bind(ep);
}
else{
ip::tcp::endpoint ep(ip::tcp::v6(), port_num);
_acceptor.open(ip::tcp::v6());
_acceptor.bind(ep);
}
_acceptor.listen();
}

解释:如果在调用套接字 API 的底层 accept(..) 函数时服务器套接字未处于监听状态,则会发出错误信号,即传递的套接字对于完成请求的操作无效(在我们的案例接受客户端套接字)。

关于c++ - async_accept 处理程序的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16522167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com