gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:32:57 24 4
gpt4 key购买 nike

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

我尝试更改下面的代码片段以将 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

服务器.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. 首先

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

    documentation说:

    This function is used to accept a new connection from a peer into the given socket, and additionally provide the endpoint of the remote peer. The function call will block until a new connection has been accepted successfully or an error occurs.

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

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

    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));

¹ 您通常可以稍后使用 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á

关于c++ - 使用 Boost.Asio 连接到域以托管服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34955858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com