本篇內(nèi)容介紹了“socket通信怎么使用”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
十年的正藍(lán)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整正藍(lán)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“正藍(lán)網(wǎng)站設(shè)計(jì)”,“正藍(lán)網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
#include#include #include using boost::asio::io_service; using boost::asio::ip::tcp; using boost::system::error_code; using boost::asio::deadline_timer; using boost::asio::buffer; class MyNet { public: void Accept(unsigned short port, boost::function fun) { _pAcceptor = new tcp::acceptor(_ios, tcp::endpoint(tcp::v4(), port)); _pTimer = new deadline_timer(_ios); _pClentSkt = new tcp::socket(_ios); _pAcceptor->async_accept(*_pClentSkt, boost::bind(&MyNet::AcceptHandle, this, boost::asio::placeholders::error, //傳遞錯(cuò)誤碼 fun//傳遞一個(gè)函數(shù)指針 )); _ios.reset(); //充值事件循環(huán)狀態(tài) if (_ios.run() == 0) //啟動(dòng)事件循環(huán) { throw std::runtime_error("io_service run return 0."); } } void Send(const string& data) { _pClentSkt->write_some(buffer(data, data.size())); } string Recv() { _ret = _pClentSkt->read_some(buffer(_buf, 1024)); return string(_buf, _ret); } void Close() { _mutex.lock(); _pAcceptor->close(); _pClentSkt->shutdown(tcp::socket::shutdown_both); _pClentSkt->close(); _ios.stop(); _mutex.unlock(); } string GetClientIp() {//獲得客戶(hù)端的ip boost::asio::ip::tcp::endpoint ep = _pClentSkt->remote_endpoint(); return ep.address().to_string(); } unsigned short GetPort() {//獲得監(jiān)控的端口 return _pAcceptor->local_endpoint().port(); } static unsigned short GetLocalValidPort() {//隨機(jī)過(guò)去一個(gè)本機(jī)的可用端口 io_service ios; tcp::acceptor acceptor(ios, tcp::endpoint(tcp::v4(), 0)); tcp::acceptor::endpoint_type et = acceptor.local_endpoint(); return et.port(); } static string GetLocalIp() {//獲取本機(jī)ip boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(boost::asio::ip::host_name(), ""); tcp::resolver::iterator iter = resolver.resolve(query); tcp::resolver::iterator end; // End marker. if (iter != end) { tcp::endpoint ep = *iter; return ep.address().to_string(); } } ~MyNet(void) { delete _pTimer; delete _pAcceptor; delete _pClentSkt; } protected: void AcceptHandle(const error_code& error, boost::function fun) {//異步接收連接的回調(diào)函數(shù) if (error) return; //傳遞進(jìn)來(lái)的函數(shù)指針fun未用到,本來(lái)是為了啟動(dòng)此函數(shù)的線(xiàn)程,boost::thread trd(fun); } private: io_service _ios; tcp::acceptor* _pAcceptor; tcp::socket* _pClentSkt; tcp::socket* _pClentTmp; deadline_timer* _pTimer; char _buf[1024]; size_t _ret; static string _ip; boost::mutex _mutex; };
“socket通信怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!