void CUIServer::Accept_handler(sock_pt sock) { if(m_bConnect) { //only save one connection Accept(); return; } m_socket = sock; m_bConnect = true; Object obj; if (CUIServer::IsInitalEnd == true) { obj.push_back(Pair("type", "init")); obj.push_back(Pair("msg", "initialize end")); } else { obj.push_back(Pair("type", "hello")); obj.push_back(Pair("msg", "hello asio")); } Accept(); string sendData = write_string(Value(std::move(obj)),true); PackageData(sendData); sock->async_write_some(asio::buffer(sendData), bind(&CUIServer::write_handler, this)); std::shared_ptr<vector<char> > str(new vector<char>(100, 0)); memset(data_,0,max_length); sock->async_read_some(asio::buffer(data_,max_length), bind(&CUIServer::read_handler, this, asio::placeholders::error, data_, sock)); }
void MMyLib::MyClient::conn_handler(const system::error_code& ec, sock_pt sock) { if(ec) return; cout<< "recive from "<< sock->remote_endpoint().address()<< endl; std::shared_ptr<vector<char> > str(new vector<char>(100, 0)); sock->async_read_some(buffer(*str), boost::bind(&MyClient::read_handler, this, boost::asio::placeholders::error, str)); }
void CRecvMsg::accept_handler(const boost::system::error_code& ec,sock_pt sock) { if(ec) { ip=sock->remote_endpoint().address().to_string(); std::string message=ip+" "+numToStr(port)+"连接异常"; writeLog(message,es); return; } start(); ip=sock->remote_endpoint().address().to_string(); QString content=tr("已连接"); QString relateDevice=QString("%1 %2").arg(ip.c_str()).arg(port); idR->insertLog(relateDevice,content); std::string message=ip+" "+numToStr(port)+"已连接"; writeLog(message,es); try{ sock->async_read_some(boost::asio::buffer(pRecvBuff, 1024), bind(&CRecvMsg::read_handler, this, placeholders::error, placeholders::bytes_transferred,sock)); //boost::posix_time::time_duration td(0,0,2,0); //boost::this_thread::sleep(td); } catch (std::exception& e) { //std::cerr << "Exception: " << e.what() << "/n"; std::string message=ip+" "+numToStr(port)+" "+e.what(); writeLog(message,es); } }