为什么我的boost http客户端无法连接到python http服务器?

时间:2013-12-27 03:14:38

标签: python http boost

我使用官方的python http服务器代码,请参阅here

import asyncore

class EchoHandler(asyncore.dispatcher_with_send):

    def handle_read(self):
        data = self.recv(8192)
        if data:
            self.send(data)

class EchoServer(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket()
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)

    def handle_accepted(self, sock, addr):
        print('Incoming connection from %s' % repr(addr))
        handler = EchoHandler(sock)

server = EchoServer('localhost', 8080)
asyncore.loop()

客户端代码是C ++ boost asio代码:

// echo_client.cpp
// g++ -o echo_client -O3 echo_client.cpp -lboost_system -lboost_thread
#include <boost/asio.hpp>
namespace asio = boost::asio;
using asio::ip::tcp;
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>


class session
: public boost::enable_shared_from_this<session>
{
    public:
    session(asio::io_service& io)
    : socket_(io)
    { }

    tcp::socket& socket()
    { return socket_; }

    void start()
    {
        asio::async_write(socket_, asio::buffer(output_buffer_, 12), boost::bind(&session::handle_write, shared_from_this(), _1, _2));
    }

    void handle_write(const boost::system::error_code& ec, std::size_t bytes_transfered)
    {
        if(!ec)
        {
            asio::async_read(socket_, asio::buffer(input_buffer_, 12), boost::bind(&session::handle_read, shared_from_this(), _1, _2));
        } else {
            std::cerr << "write error:" << ec.message() << std:: endl;
        }
    }

    void handle_read(const boost::system::error_code& ec, std::size_t bytes_transfered)
    {
        if(ec)
        {
            std::cerr << "read error:" << ec.message() << std::endl;
        }
    }

    private:
    tcp::socket socket_;
    char output_buffer_[12];
    char input_buffer_[12];
};


void handle_connect(boost::shared_ptr<session> session_ptr, const boost::system::error_code& ec)
{
    if(ec)
    {
        std::cerr << "connect error:" << ec.message() << std::endl;
    } else {
        session_ptr->start();
    }
}


int main(int argc, char* argv[])
{
    asio::io_service io;
    tcp::resolver resolver(io);
    tcp::resolver::iterator endpoint = resolver.resolve(tcp::resolver::query("localhost", argv[1]));
    boost::shared_ptr<session> session_ptr;
    for(int i = 0; i < 10; i++)
    {
        session_ptr.reset(new session(io));
        asio::async_connect(session_ptr->socket(), endpoint, boost::bind(handle_connect, session_ptr, _1));
    }
    io.run();
}

当我启动服务器然后使用./echo_client 8080运行C ++ boost客户端代码时,输​​出为:

write error:?8??
write error:?8???8??]?@?]?????8?8???8?
write error:    ?8???????]??]???]?
write error:?8???????]??]?????8???8?
write error:?8???????]??]????
                             ?8??
                                 ?8?
?8???`?8?or:?8???????]??]????
write error:?8??????
write error:?8???????]??]???@?8? ?8?@?8?????8?`?]???]?
write error:?8???????]??]????8???8??8?
write error:?8???????]??]???

它是什么原因?

1 个答案:

答案 0 :(得分:2)

您的Python脚本只能处理5个连接的积压。您的客户端快速连续启动10个连接,服务器处理速度太快,因此连接被丢弃。我得到了:

$ ./echo_client 8080
write error:Broken pipe
write error:Broken pipe
read error:Connection reset by peer
write error:Broken pipe
$ 

self.listen(5)更改为更大的内容。我使用self.listen(15),没有错误。

至于错误消息显示问号的原因,这可能是您的C ++程序使用的编码与终端预期编码的问题。我系统上显示的错误消息(Mac OS X 10.9)如上所示。