使用async_accept的处理程序

时间:2013-05-13 12:33:55

标签: c++ boost boost-asio

我编写了自己的名为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 */

1 个答案:

答案 0 :(得分:3)

您必须将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(..)函数时服务器套接字未处于侦听状态,则会发出错误信号,表明传递的套接字对于完成请求的操作无效(在我们的示例中接受客户端套接字)