簡體   English   中英

cpp-netlib如何使這樣的示例通過TCP返回(刪除http標頭)?

[英]cpp-netlib how to make such example return over TCP (remove http header)?

我知道有一個boost :: asio專門用於這種東西,但是如果有一種方法可以在使用http和不使用http之間切換的話,這對我來說會容易得多(我的意思是我有10個使用http的服務和4使用TCP但執行完全相同的操作(接收http get請求並返回TCP消息),所以這樣的功能確實對我有幫助)

因此,讓我們來看一個http serrver示例

#include <boost/network/protocol/http/server.hpp>
#include <string>
#include <iostream>

namespace http = boost::network::http;

struct hello_world;
typedef http::server<hello_world> server;

struct hello_world
{
    void operator() (server::request const &request,
                     server::response &response)
    {
        std::string ip = source(request);
        response = server::response::stock_reply(
                       server::response::ok, std::string("Hello, ") + ip + "!");
    }
};

int
main(int argc, char * argv[])
{

    if (argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " address port" << std::endl;
        return 1;
    }

    try
    {
        hello_world handler;
        server server_(argv[1], argv[2], handler);
        server_.run();
    }
    catch (std::exception &e)
    {
        std::cerr << e.what() << std::endl;
        return 1;
    }
    return 0;
}

如何使它返回沒有HTTP響應群的消息?

如果不想使用HTTP庫,則可以嘗試使用netcat或其他工具通過TCP發送直接消息而無需任何特定協議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM