用于在本地主机上运行 boost asio 服务器的网络设置

问题描述 投票:0回答:1

我写了一个客户端client.cpp和一个服务器server.cpp,它们编译得很好。但我不知道如何将client.cpp连接到localhost中的server.cpp

我用Boost.Asio编写代码,它们编译得很好,但我不知道如何进行网络设置,以便server.cpp始终等待特定端口上的新连接。

我想将客户端代码连接到特定端口上的服务器并从server.cpp中获取信息,但我不知道网络设置。

我的操作系统是Windows 10

客户端.cpp:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

enum { max_length = 1024 };

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

        if (argc != 3){
              std::cerr << "Usage: blocking_tcp_echo_client <host> <port>\n";
              return 1;
        }

        boost::asio::io_context io_context;

        tcp::resolver resolver(io_context);
        tcp::resolver::results_type endpoints =
        resolver.resolve(tcp::v4(), argv[1], argv[2]);

        tcp::socket s(io_context);
        boost::asio::connect(s, endpoints);

        using namespace std; // For strlen.
        std::cout << "Enter message: ";
        char request[max_length];
        std::cin.getline(request, max_length);
        size_t request_length = strlen(request);
        boost::asio::write(s, boost::asio::buffer(request, request_length));

        char reply[max_length];
        size_t reply_length = boost::asio::read(s,
        boost::asio::buffer(reply, request_length));
        std::cout << "Reply is: ";
        std::cout.write(reply, reply_length);
        std::cout << "\n";
   }
   catch (std::exception& e)
   {
        std::cerr << "Exception: " << e.what() << "\n";
   }

   return 0;
}

服务器.cpp

#include <cstdlib>
#include <iostream>
#include <boost/bind/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>

using boost::asio::ip::tcp;

const int max_length = 1024;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

void session(socket_ptr sock){

    try{

        for (;;){
            char data[max_length];

            boost::system::error_code error;
            size_t length = sock->read_some(boost::asio::buffer(data), error);
            if (error == boost::asio::error::eof)
                 break; // Connection closed cleanly by peer.
            else if (error)
                throw boost::system::system_error(error); // Some other error.

            boost::asio::write(*sock, boost::asio::buffer(data, length));
        }
    }
    catch (std::exception& e){
         std::cerr << "Exception in thread: " << e.what() << "\n";
    }
}

void server(boost::asio::io_context& io_context, unsigned short port)
{
     tcp::acceptor a(io_context, tcp::endpoint(tcp::v4(), port));
     for (;;){
          socket_ptr sock(new tcp::socket(io_context));
          a.accept(*sock);
          boost::thread t(boost::bind(session, sock));
     }
}

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

        if (argc != 2){
            std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
            return 1;
        }

        boost::asio::io_context io_context;

        using namespace std; // For atoi.
        server(io_context, atoi(argv[1]));
 }
 catch (std::exception& e)
 {
     std::cerr << "Exception: " << e.what() << "\n";
 }

return 0;
}
c++ sockets boost boost-asio asio
1个回答
0
投票

环回网络上的本地主机始终为 127.0.0.1。

没有“设置”。例如。

tcp::endpoint{{}, port}
在一个简单的程序中:

住在Coliru

void run_server(uint16_t port, unsigned max_connections) {
    tcp::acceptor acc(ioc, {{}, port});
    while (max_connections--) {
        out() << "Server accepting new connection" << std::endl;
        std::thread(
            [](tcp::iostream s) {
                out() << "Server accepted new connection from " << s.socket().remote_endpoint() << std::endl;

                if (std::string line; getline(s, line)) {
                    reverse(begin(line), end(line));
                    s << "Echo reversed: " << line << std::endl;
                }
            },
            tcp::iostream(acc.accept()))
            .detach();
    }
}

void run_client(uint16_t port, std::string msg) {
    tcp::iostream s(tcp::endpoint{{}, port});

    s << msg << std::endl;
    out() << "Client received: " << s.rdbuf() << std::endl;
}

int main() {
    using namespace std::chrono_literals;

    std::thread server(run_server, 7878, 2);

    std::this_thread::sleep_for(1s);
    run_client(7878, "Hello world");
    std::this_thread::sleep_for(500ms);
    run_client(7878, "Bye world");

    server.join();
}

打印

Server accepting new connection
Server accepting new connection
Server accepted new connection from 127.0.0.1:36068
Client received: Echo reversed: dlrow olleH

Server accepted new connection from 127.0.0.1:36074
Client received: Echo reversed: dlrow eyB

约1.5秒

© www.soinside.com 2019 - 2024. All rights reserved.