久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何將 boost asio tcp 套接字傳遞給線程以將心跳發

How to pass a boost asio tcp socket to a thread for sending heartbeat to client or server(如何將 boost asio tcp 套接字傳遞給線程以將心跳發送到客戶端或服務器)
本文介紹了如何將 boost asio tcp 套接字傳遞給線程以將心跳發送到客戶端或服務器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在用 boost TCP 編寫一個客戶端/服務器程序,其中我想每 2 秒向客戶端發送一次 HEARTBEAT 消息,我正在嘗試創建一個新線程,通過它我可以輕松地發送它但無法解決它.我正在使用 boost::thread t(hearbeatSender,sock); 創建線程.但給出了很多錯誤.我也使用 bind 將函數名與套接字綁定,但沒有解決錯誤.

I am writing a client/server program in boost TCP in which I want to send a HEARTBEAT message to the client every 2 seconds for which I am trying to create a new thread by which I can send it easily but unable to solve it. I am creating thread using boost::thread t(hearbeatSender,sock); this. but giving lots of errors. I also use bind to bind function name with the socket but not resolved the error.

void process(boost::asio::ip::tcp::socket & sock);
std::string read_data(boost::asio::ip::tcp::socket & sock);
void write_data(boost::asio::ip::tcp::socket & sock,std::string);
void hearbeatSender(boost::asio::ip::tcp::socket & sock);
int main()
{

    unsigned short port_num = 3333;
    boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address_v4::any(), port_num);
    boost::asio::io_service io;
    try
    {
        boost::asio::ip::tcp::acceptor acceptor(io, ep.protocol());
        acceptor.bind(ep);
        acceptor.listen();
        boost::asio::ip::tcp::socket sock(io);
        acceptor.accept(sock);
        boost::thread t(hearbeatSender,sock); 
        process(sock);
        t.join();

    }
    catch (boost::system::system_error &e)
    {
        std::cout << "Error occured! Error code = " << e.code()
        << ". Message: " << e.what();

        return e.code().value();
    }
  return 0;

}
void process(boost::asio::ip::tcp::socket & sock)
{
    while(1){
    std::string data = read_data(sock);
    std::cout<<"Client's request is: "<<data<<std::endl;
    write_data(sock,data);
    }
}
std::string read_data(boost::asio::ip::tcp::socket & sock)
{
    boost::asio::streambuf buf;
    boost::asio::read_until(sock, buf, "
");
    std::string data = boost::asio::buffer_cast<const char*>(buf.data());
    return data;
}
void write_data(boost::asio::ip::tcp::socket & sock,std::string data)
{
    boost::system::error_code error;
    std::string msg;
    int ch = data[0]-'0';
    switch(ch)
    {
        case 1: msg = "Case 1
"; break;
        case 2: msg = "Case 2
"; break;
        case 3: msg = "Case 3
"; break;
        case 4: msg = "Case 4
"; break;
        default: msg  = "Case default
"; break;
    }
    boost::asio::write( sock, boost::asio::buffer(msg+ "
"), error );
     if( !error ) {
        std::cout << "Server sent hello message!" << std::endl;
     }
     else {
        std::cout << "send failed: " << error.message() << std::endl;
     }
}
void hearbeatSender(boost::asio::ip::tcp::socket & sock)
{
    boost::system::error_code error;
    std::string msg = "HEARTBEAT";
    while(1)
    {
        sleep(2);
        std::cout<<msg<<std::endl;
        boost::asio::write( sock, boost::asio::buffer(msg+ "
"), error );
        if( !error ) {
        std::cout << "Server sent HEARTBEAT message!" << std::endl;
        }
        else {
            std::cout << "send failed: " << error.message() << std::endl;
        }
    }
}

這是一個服務端代碼,用于響應客戶端的消息并向客戶端發送心跳.這是一個同步 TCP 服務器.

This is a server-side code for responding to the message of the client and sending heartbeat to the client. This is a synchronous TCP server.

推薦答案

代替這個:

try
    {
        boost::asio::ip::tcp::acceptor acceptor(io, ep.protocol());
        acceptor.bind(ep);
        acceptor.listen();
        auto sock = acceptor.accept();
        std::thread t([&sock]() {
            hearbeatSender(sock);
        });
        process(sock);
        t.join();

    }

使用它:

try{
        boost::asio::ip::tcp::acceptor acceptor(io, ep.protocol());
        acceptor.bind(ep);
        acceptor.listen();
        boost::asio::ip::tcp::socket sock(io);
        acceptor.accept(sock);

        std::thread t([&sock]() {
            hearbeatSender(sock);
        });
        process(sock);
        t.join();
}

還包括頭文件:

#include <thread>
#include <chrono>

(可選)你也可以使用 this_thread::sleep_for 而不是 sleep()std::this_thread::sleep_for(std::chrono::seconds(10));

(Optional) you can also use this_thread::sleep_for instead of sleep() std::this_thread::sleep_for(std::chrono::seconds(10));

把socket傳遞給線程的問題解決了.

The problem of passing a socket to the thread is solved.

現在,用于在客戶端和服務器之間進行 HEARTBEAT 對話.可以從這里檢查完整的代碼:

Now, for conversing a HEARTBEAT between a client and a server. Complete code can be checked from here:

客戶端代碼 HEARTBEAT 每 5 次傳輸秒

服務器代碼,用于響應客戶

這篇關于如何將 boost asio tcp 套接字傳遞給線程以將心跳發送到客戶端或服務器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 午夜视频一区二区三区 | 久久婷婷网 | 久久久天堂国产精品女人 | a级片免费在线观看 | 97国产精品| 国产一级片在线 | 国产一区不卡 | 黄色片观看| 天天躁日日躁bbbbb | 欧美精品一区在线 | 日韩精品成人 | 久久草视频 | 欧美日韩国产激情 | 99热1 | 午夜天堂在线 | 欧美日韩三区 | 中文字幕婷婷 | 亚洲精品视频在线观看免费 | 欧美又粗又长 | 日本在线视频观看 | аⅴ资源新版在线天堂 | 日韩精品免费一区二区夜夜嗨 | 中文字幕一级片 | 成人av一区二区三区在线观看 | 国产精品高潮呻吟久久 | 久久精品国产精品 | 午夜免费av| 欧美性生交xxxxx久久久 | 99精品视频在线 | 精品在线免费观看 | 亚洲精品一区二区三区在线观看 | 日本特级黄色片 | 高hnp失禁3p小公主 | 亚洲精品999| 国产精品美女久久久久久久久 | 免费av不卡 | 亚洲国产毛片 | 欧美黑人性猛交 | 可以看的毛片 | 午夜精品影院 | 国产99对白在线播放 |