Boost asio发送和接收消息 [英] Boost asio send and receive messages

查看:130
本文介绍了Boost asio发送和接收消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用TCP从客户端和服务器发送和接收消息.我正在尝试使用线程,但我根本不知道该怎么做.我可以很好地连接到服务器,但是我需要能够按需从两个地方发送和接收消息.我一直在搜索几个小时,却空了一下,因为Google上的所有结果都过于复杂和混乱.

I'm trying to send and receive messages from a client and server using TCP. I'm trying it with threading, and I don't know how to do this at all. I can connect to the server just fine, but I need to be able to send and receive messages from both places on demand. I've been searching for hours and have come up empty, as all of the results on Google are overcomplicated and cluttering.

struct Client
{
boost::asio::ip::tcp::socket socket;

Client(const char* host = HOST, const char* port = PORT) : socket(io_service) 
{
    boost::asio::ip::tcp::resolver resolver(io_service);
    boost::asio::ip::tcp::resolver::iterator endpoint = resolver.resolve(boost::asio::ip::tcp::resolver::query(HOST, PORT));
    boost::asio::connect(this->socket, endpoint);
   };
};

到目前为止,这就是我要给客户的全部信息.

That's all I have so far for the client.

对于服务器:

const int PORT = 52275;

int main()
{
    try
{
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), PORT));

    {
        boost::asio::ip::tcp::socket socket(io_service);
        acceptor.accept(socket);
        main();
    }
}
catch (std::exception& e)
{
    std::cerr << "Exception: " << e.what() << std::endl;
}
}

推荐答案

由于可以肯定的是,假设您只是想以最简单的方式发送/any/消息,就很安全了:

Since it's apparently safe to assume you just want to send /any/ message by the simplest means possible:

在Coliru上直播

#include <boost/asio.hpp>

struct Client
{
    boost::asio::io_service& io_service;
    boost::asio::ip::tcp::socket socket;

    Client(boost::asio::io_service& svc, std::string const& host, std::string const& port) 
        : io_service(svc), socket(io_service) 
    {
        boost::asio::ip::tcp::resolver resolver(io_service);
        boost::asio::ip::tcp::resolver::iterator endpoint = resolver.resolve(boost::asio::ip::tcp::resolver::query(host, port));
        boost::asio::connect(this->socket, endpoint);
    };

    void send(std::string const& message) {
        socket.send(boost::asio::buffer(message));
    }
};


#include <iostream>

static const int PORT = 52275;

void client_thread() {
    boost::asio::io_service svc;
    Client client(svc, "127.0.0.1", std::to_string(PORT));

    client.send("hello world\n");
    client.send("bye world\n");
}

void server_thread() {
    try
    {
        boost::asio::io_service io_service;
        boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), PORT));

        {
            boost::asio::ip::tcp::socket socket(io_service);
            acceptor.accept(socket);

            boost::asio::streambuf sb;
            boost::system::error_code ec;
            while (boost::asio::read(socket, sb, ec)) {
                std::cout << "received: '" << &sb << "'\n";

                if (ec) {
                    std::cout << "status: " << ec.message() << "\n";
                    break;
                }
            }
        }
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << std::endl;
    }
}

#include <boost/thread.hpp>

int main() {
    boost::thread_group tg;
    tg.create_thread(server_thread);

    boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
    tg.create_thread(client_thread);

    tg.join_all();
}

这篇关于Boost asio发送和接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆