如何使用asio本地数据报构建回显服务器和客户端?

时间:2018-08-12 10:21:44

标签: c++ boost-asio

//服务器

#include <boost/asio.hpp>

int main()
{
    ::unlink("local_socket");
    boost::asio::io_service io_service;
    boost::asio::local::datagram_protocol::endpoint endpoint("local_socket");
    boost::asio::local::datagram_protocol::socket socket(io_service, endpoint);

    char recv_buf[1024];
    while (1)
    {
        boost::asio::local::datagram_protocol::endpoint senderEndpoint;
        size_t len = socket.receive_from(
            boost::asio::buffer(recv_buf, 1024), senderEndpoint);
        printf("***%s###\n", senderEndpoint.path().c_str());

        socket.send_to(
            boost::asio::buffer(recv_buf, len), senderEndpoint);
    }

    return 0;
}

//客户端

#include <boost/asio.hpp>

enum { max_length = 1024 };

int main(int argc, char* argv[])
{
    boost::asio::io_service io_service;
    boost::asio::local::datagram_protocol::endpoint endpoint("local_socket");
    boost::asio::local::datagram_protocol::socket socket(io_service);
    socket.open();

    std::cout << "Enter message: ";
    char request[max_length];
    std::cin.getline(request, max_length);
    size_t request_length = strlen(request);

    socket.send_to(
        boost::asio::buffer(request, request_length), endpoint);

    char reply[max_length];
    size_t reply_length = socket.receive_from(
        boost::asio::buffer(reply, max_length), endpoint);

    std::cout << "Reply is: ";
    std::cout.write(reply, reply_length);
    std::cout << "\n";

    return 0;
}

当我使用客户端发送“ 123”并且服务器通过以下方式进行核心转储时:

  

*** ###终止在引发'boost :: exception_detail :: clone_impl

实例后调用      
    

'what():send_to:无效的参数已中止

  

2 个答案:

答案 0 :(得分:0)

找到答案hereclientserver

答案 1 :(得分:0)

服务器中止的原因是由于senderEndpoint的调用未填充receive_from-客户端套接字未绑定任何内容,因此它不发送要接收的文件描述符回应。如果我们这样重构客户:

#include <iostream>

#include <boost/asio.hpp>

enum { max_length = 1024 };

int main(int argc, char* argv[])
{
    boost::asio::io_service io_service;
    boost::asio::local::datagram_protocol::endpoint server_endpoint("local_socket");
    // Create client endpoint on which to receive response
    std::string client_addr = "local_socket.";
    client_addr += std::to_string(::getpid());
    ::unlink(client_addr.c_str());
    boost::asio::local::datagram_protocol::endpoint client_endpoint(client_addr.c_str());
    boost::asio::local::datagram_protocol::socket socket(io_service, client_endpoint);

    std::cout << "Enter message: ";
    char request[max_length];
    std::cin.getline(request, max_length);
    size_t request_length = strlen(request);

    socket.send_to(
        boost::asio::buffer(request, request_length), server_endpoint);

    char reply[max_length];
    size_t reply_length = socket.receive_from(
        boost::asio::buffer(reply, max_length), client_endpoint);

    std::cout << "Reply is: ";
    std::cout.write(reply, reply_length);
    std::cout << "\n";

    return 0;
}

然后按预期工作:

Enter message: 123
***local_socket.29635###
Reply is: 123