使用Boost.Asio连接到域以托管服务器

时间:2016-01-22 20:59:26

标签: c++ boost boost-asio

如何通过我的服务器连接到我的域,以便我可以“托管”它?

我已经尝试更改下面的代码段,以便将IP与我的域名相匹配,但是发现了一个异常,说明提供了无效的参数,我假设我应该解析域名,获取IP并使用IP我得到的不是下面代码片段中的那个,但似乎我正在连接到我的外部IP,这不会让我托管服务器,因为它说机器主动拒绝连接。

这是代码片段:

boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.3"), 8001));

这是完整的代码:

Main.cpp的

#include "Server.h"

#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

int main()
{
    Server server;
    boost::thread([&server] {
        server.handleConnections();
    }).join();

    return 0;
}

Server.h

#ifndef SERVER_H
#define SERVER_H

#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

class Server
{
private:
    boost::asio::io_service service;
    boost::asio::ip::tcp::socket sock;
protected:

public:
    Server() : service(), sock(service) {

    }

    ~Server() {

    }

    void handleConnections();
};

#endif

Server.cpp

#include "Server.h"

void Server::handleConnections() {
    boost::system::error_code err;
    try {
        boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.3"), 8001));
        std::cout << acceptor.local_endpoint(err) << std::endl;
        while (true) {
            sock = boost::asio::ip::tcp::socket(service);
            boost::system::error_code errCode;
            acceptor.accept(this->sock, acceptor.local_endpoint(), errCode);
            boost::asio::write(sock, boost::asio::buffer("Olá"), errCode);
        }
        this->sock.close();
    }
    catch (std::exception & e)
    {
        std::cout << e.what();
    }
}

1 个答案:

答案 0 :(得分:1)

  1. 首先

    acceptor.accept(this->sock, acceptor.local_endpoint(), errCode);
    

    documentation说:

      

    此函数用于接受从对等方到给定套接字的新连接,并另外提供远程对等方的端点。函数调用将阻塞,直到成功接受新连接或发生错误。

    你...将接受者的本地端点作为端点传递给接收对等体的远程端点(如果编译,我会感到惊讶)。这没什么意义。我建议您不需要知道远程端点¹:

    acceptor.accept(this->sock, errCode);
    
  2. 其次,你已经绑定了环回适配器:

    boost::asio::ip::tcp::acceptor acceptor(service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string("192.168.1.3"), 8001));
    

    这直接意味着您无法从网络中访问它。将其更改为与NIC无关:

    tcp::acceptor acceptor(service, tcp::endpoint(ip::address(), 8001));
    
  3. ¹您通常可以稍后使用socket_->remote_endpoint()从套接字获取它,除非套接字无效(例如已关闭)

    ²除非你实现一些时髦的路由/隧道逻辑,这是一个很长的延伸

    更新

    包含错误处理的自包含演示:

    <强> Live On Coliru

    #include <iostream>
    #include <boost/asio.hpp>
    
    class Server
    {
        boost::asio::io_service service;
    public:
        void handleConnections() 
        {
            using namespace boost::asio;
    
            boost::system::error_code err;
    
            try {
                ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::address(), 6768));
    
                std::cout << acceptor.local_endpoint(err) << std::endl;
    
                while (!err) {
                    ip::tcp::socket sock(service);
    
                    if (   !acceptor.accept(sock, err)
                        && !write(sock, buffer("Olá"), err)
                        && !sock.close(err))
                    {
                        std::cout << "Error in connection: " << err << " " << err.message() << "\n";
                    }
                }
            } catch (std::exception & e) {
                std::cout << e.what();
            }
        }
    };
    
    #include <boost/thread.hpp>
    
    int main() {
        Server server;
        boost::thread([&server] { server.handleConnections(); }).join();
    }
    

    输出:

    $ ./a.out& 
    0.0.0.0:6768
    
    $ while sleep 1; do nc 127.0.0.1 6768; done
    OláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOláOlá