0
我使用的boost :: ASIO,有8個線程的boost :: ASIO async_accept總是出現錯誤,error_code.value()是22,這意味着無效的參數
boost::asio::io_service ios;
boost::asio::ip::tcp::acceptor(ios);
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);
acceptor.open(endpoint.protocol());
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor.listen();
LocalTcpServer::getInstance()->initialize(ios, acceptor, pool);
boost::thread_group th_group;
for(i=0; i< 8; i++)
th_group.add_thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &ios)));
th_group.join_all();
session::start()
{
socket.async_read_some(boost::asio::buffer(buffer), m_strand.wrap(boost::bind(&session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)))
}
session::handleread(boost::system::error_code &e, size_t byteTrans)
{
if(e || byteTrans == 0)
{
socket.shutdown(...)
//socketRelease close the socket and delete this
timeInfo->timer->async_wait(boost::bind(socketRelease(), ...);
}
else
{
//deal with data whit pool;
}
socket.async_read_some(.....);
}
LocaltcpServer::initialize(ios, acceptor, pool){
//init, pool is inherit from threadpool, used in handle read to deal with receive data
...;
startaccept();
}
LocalTcpServer::Accept()
{
session* pSession = new session(acceptor->get_io_service, pool);
acceptor.async_accept(session->socket, boost::bind(handle_accept, this, pSession, boost::asio::placeholder::error))
}
LocalTcpServer::handle_accept(boost::system::error_code& e; ...);
{
if(e)
{
//when app run sometime(serveral hours or days, e has always error 22, means invalid argument)
LOG_ERROR << e.message() << e.value();
delete newSession;
accept();
}
else
{
session.start();
accept();
}
}
應用程序是做工精細的第一,但有些時候後,可以serveral的時間,1兩天後,錯誤出現,hander_accpte總是得到一個犯錯,無效的參數。那麼,有沒有新的連接,
插座連接幾乎是10000,並且文件打開限制爲65535, 我有使用netstat來檢查插座通常是關閉的,沒有插座whitout關閉 我不知道爲什麼犯錯發生,我怎麼能修復它, 或者,如果我的代碼有一些錯誤? 我希望我能清楚地描述問題。謝謝。
呢'm_strand.wrap()'封裝了一個線程中回調?出於某種原因,8個線程聽起來很像一個庫,它可以在單個線程上處理50k連接,而不會降低性能。 asio例程意味着與io_service :: run()從同一個線程運行;我認爲你正在遇到某種競爭狀況。 –
很難說哪裏的錯誤來自...如果監聽套接字失敗爲好,主要犯罪嫌疑人之一是DHCP。接口的IP地址可能已更改。在這種情況下,綁定到該接口的所有打開的套接字將變爲無效,並且必須關閉,包括偵聽套接字,然後必須使用新套接字重新啓動偵聽。 –
我發現當錯誤ocurr時,listen聽不見了。例如,我使用netstat -nat | grep 1234顯示連接,沒有監聽行的localAddress是127.0.0.1:1234,狀態是listen。但是當它正確運行時您可以看到該行。但是之前建立了一些連接,一段時間後,連接減少到零。我很好奇爲什麼聽不見了?如果我創建一個新的接受者來聆聽,那麼這樣做是否奏效? – rick