0

该服务器运行在一个单独的线程中,除了经常抛出的异常“accept:Already open exception”外,似乎运行良好。在检查套接字是否已经打开时,在 acceptor.accept(...) 调用时会引发此异常。如果我调用 accepter.accept(...) iff 套接字未打开(注释行),行为变得不可预测。run 方法从同步的 mQueue 中获取数据,该 mQueue 正在以每秒大约 30 次的速度在另一个线程上填充。

我究竟做错了什么?

class Server{
    public:

Server(unsigned short port, ConcurrentQueue<uint8_t*>*queueToServer, unsigned int width, unsigned int height):mSocket(mIOService),mAcceptor(mIOService,ip::tcp::endpoint(ip::tcp::v4(), port)),mQueue(queueToServer), dSize(width*height*3){}
void run(){

    unsigned char* data;
    boost::system::error_code ignored_error;
    while(true){
        if (mQueue->try_pop(data)){
            const mutable_buffer image_buffer(data, dSize);
            //if (!mSocket.is_open())
            mAcceptor.accept(mSocket);
            boost::asio::write(mSocket, buffer(image_buffer), transfer_all(), ignored_error);
        }
    }
}
private:
    io_service mIOService;
    ip::tcp::socket mSocket;
    ip::tcp::acceptor mAcceptor;
    ConcurrentQueue<uint8_t*>* mQueue;
    std::size_t dSize;
};
4

1 回答 1

3

在 asio 术语中,如果套接字具有有效的套接字句柄(描述符),则该套接字是“打开的”。当你接受一个传入的连接时,你应该向接受者传递一个“新鲜的”、未打开的套接字。所以,问题出在你的代码逻辑上:你应该首先从你的客户端接受一个新的连接,然后你可以使用接受的套接字来发送/接收你想要的任何数据。

于 2012-05-24T12:05:29.990 回答