C ++-Boost ASIO网络服务器/客户端 [英] c++ - Boost ASIO networking server/client

查看:276
本文介绍了C ++-Boost ASIO网络服务器/客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为客户端和服务器编写了程序.该程序当前执行以下操作:

I have written a program for client and server. The program currently does the following:

  1. 服务器侦听连接的端点
  2. 客户端连接到服务器
  3. 服务器在接受连接时发送消息
  4. 客户收到消息

我正在异步执行此操作.但是,问题在于它们只能发送/接收一条消息.在那之后,他们就终止了.下面是我的代码:

I'm doing this asynchronously. But, the problem is they can only send/receive one message. After that, they just terminate. Below is my code:

#include <iostream>
#include <vector>
#include<boost/asio.hpp>

std::vector<char> buff(256);

void SendHandler(boost::system::error_code ex){
    std::cout << " do something here" << std::endl;
}

void ReadHandler(boost::system::error_code ex){
    std::cout << " print the buffer data..." << std::endl;
    std::cout << buff.data() << std::endl;

}

void Server(){
    boost::asio::io_service service;
    using namespace boost::asio::ip;
    tcp::endpoint endpoint(tcp::v4(), 4000);
    tcp::acceptor acceptor(service, endpoint); 
    tcp::socket socket(service);
    std::cout << "[Server] Waiting for connection" << std::endl;


    acceptor.accept(socket);
    std::cout << "[Server] Accepted a connection from client" << std::endl;

    std::string msg = "Message from server";
    socket.async_send(boost::asio::buffer(msg), SendHandler);
    service.run();

}

void Client(){
    boost::asio::io_service service;
    using namespace boost::asio::ip;
    tcp::endpoint endpoint(address::from_string("127.0.0.1"), 4000);
    tcp::socket socket(service);
    std::cout << "[Client] Connecting to server..." << std::endl;
    socket.connect(endpoint);
    std::cout << "[Client] Connection successful" << std::endl;

    socket.async_read_some(boost::asio::buffer(buff), ReadHandler);
    service.run();
}

int main(int argc, char **argv) {
    if(argc == 1){
        std::cout << "Please specify s for server or c for client" << std::endl;
        return -1;
    }
    if(argv[1][0] == 's'){
        Server();
    }
    else{
        Client();
    }
    return 0;
}

我要缩放此程序,以便:

I want to scale this program so that:

    服务器可以监听,客户端可以无限期发送请求.更像是单向聊天系统.
  1. 服务器能够连接到多个客户端.
  1. server can listen and client can send request indefinitely. More like a one way chat system.
  2. server being able to connect to multiple clients.

async_send()service.run()无限循环放入并没有帮助.它只是一遍又一遍地在客户端打印消息,直到客户端终止.

Putting the async_send() and service.run() in an infinite loop didn't help. It just prints the message over and over on the client side until the client terminates.

我对boost::asio甚至对network programming都是陌生的.请告诉我应该在代码中的什么地方修改什么内容?

I'm fairly new to boost::asio even to network programming. Please tell me where and what I should modify in my code?

推荐答案

从学习一些基础知识开始.该库提供了出色的教程和示例,将带您逐步了解同步和异步服务器的概念和示例.

Start with learning some of the basics. There are excellent tutorials and examples provided by the library that will walk you through concepts and examples of both synchronous and async servers.

http://www.boost.org/doc /libs/1_62_0/doc/html/boost_asio.html

从头开始.通过计时器和回调了解基本概念.继续本教程,进入网络.在几个小时的过程中,通过编辑教程程序来尝试自己的想法,以此为基础构建核心想法.

Start at the beginning. Work through the basic concepts with timers and callbacks. Continue with the tutorial into the networking. Build on the core ideas over the course of a few hours by editing the tutorial programs to try out ideas on your own.

然后继续进行示例部分. 聊天"示例确实与您要构建的示例非常接近.此特定示例说明如何通过在读取处理程序中重新发出async_reads来打开连接.只要服务线程继续与分配的async_accept任务一起运行,异步asio服务器将为多个客户端提供服务.

Then work your way over to the examples section. The 'Chat' example is really close to what you are looking to build. This specific example shows how to connections open by re-issuing async_reads in the read handlers. Asynchronous asio servers will service multiple clients as long as the service threads continue to run with the assigned async_accept task.

http://www.boost .org/doc/libs/1_62_0/doc/html/boost_asio/examples/cpp11_examples.html http://www. boost.org/doc/libs/1_62_0/doc/html/boost_asio/example/cpp11/chat/chat_server.cpp

这篇关于C ++-Boost ASIO网络服务器/客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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