In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use socket communication". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
# 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, / / pass error code fun// passes a function pointer)) _ ios.reset (); / / recharge event loop status if (_ ios.run () = = 0) / / start event loop {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 () {/ / get the client's ip boost::asio::ip::tcp::endpoint ep = _ pClentSkt- > remote_endpoint (); return ep.address () .to_string () } unsigned short GetPort () {/ / get the monitored port return _ pAcceptor- > local_endpoint () .port ();} static unsigned short GetLocalValidPort () {/ / randomly pass a local available port 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 () {/ / get native 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) {/ / the function pointer fun passed in by the callback function if (error) return; / / of the asynchronous receive connection is not used and is originally intended to start the thread of this function, 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;}; that's all for "how to use socket Communications". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.