简体   繁体   English

asio boost套接字连接被拒绝

[英]asio boost socket connection refused

I'm attempting to connect a client and server through asio boost but my connection keeps getting refused.我正在尝试通过 asio boost 连接客户端和服务器,但我的连接一直被拒绝。 My expected output is a clean connection with no errors.我预期的 output 是一个干净的连接,没有错误。

client.cpp客户端.cpp

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

using namespace std;
using namespace boost;
using namespace boost::asio;
using namespace boost::asio::ip;
using ip::tcp;

int main()
{
    io_service io_service;
    ip::tcp::socket client_socket(io_service);
    client_socket.connect(
        tcp::endpoint(address::from_string("127.0.0.1"), 1234));
}

server.cpp服务器.cpp

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

using namespace std;
using namespace boost;
using namespace boost::asio;
using namespace boost::asio::ip;
using ip::tcp;

int main() {
    io_service io_service;

    tcp::acceptor acceptor_server(
        io_service, tcp::endpoint(address ::from_string("127.0.0.1"), 1234));

    ip::tcp::socket server_socket(io_service);

    acceptor_server.accept(server_socket);
}

This is my error message:这是我的错误信息:

terminate called after throwing an instance of                                                       
      'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  connect: Connection refused
  Aborted (core dumped)

First off, with all the unhygienic namespace... abuse and then even naming your variables conflicting names (like io_context ) it's hard to tell wether the code is correct, or should even compile.首先,由于所有不卫生的命名空间......滥用,然后甚至命名您的变量名称冲突(如io_context ),很难判断代码是否正确,或者甚至应该编译。

Here's my counter-offer这是我的还价

  • File server.cpp文件server.cpp

     #include <boost/asio.hpp> using boost::asio::ip::tcp; int main() { boost::asio::io_service ctx; tcp::acceptor s(ctx, tcp::endpoint({}, 1234)); tcp::socket conn = s.accept(); }
  • File client.cpp文件client.cpp

     #include <boost/asio.hpp> using boost::asio::ip::tcp; int main() { boost::asio::io_service ctx; tcp::socket s(ctx); s.connect(tcp::endpoint({}, 1234)); }

See them Live On Compiler Explorer .查看它们Live On Compiler Explorer

Unsurprisingly that works (see that live on Coliru with a combined source).不出所料,这很有效(请参阅Coliru的直播与组合源)。

Now if it doesn't work for you then:现在,如果它对您不起作用,那么:

  • maybe you are not running the code you think you are (check the build is up to date)也许您没有运行您认为的代码(检查构建是最新的)
  • maybe you are running in virtualized/isolated environments (like a docker container?)也许您正在虚拟化/隔离环境中运行(例如 docker 容器?)
  • maybe you're in fact not having/keeping the server running while the client is started也许您实际上在客户端启动时没有/保持服务器运行
  • maybe there's a permission/firewall issue可能存在权限/防火墙问题
  • maybe there's a broken assumption (like, eg if you don't realize the server exits after a single connect or a network config change eg)也许有一个错误的假设(例如,如果您没有意识到服务器在单次连接或网络配置更改后退出)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM