简体   繁体   中英

Boost.Asio wrong local endpoint

code example:

#include "stdafx.h"
#include <boost/asio.hpp>
#include <winsock2.h>
#include <iostream>
#include <string>


int _tmain(int argc, _TCHAR* argv[])
{
    boost::asio::io_service  service;
    auto sock_ = new boost::asio::basic_stream_socket< boost::asio::ip::tcp >(service);
    if(sock_){
        try {
            boost::asio::ip::address_v4 ipa = boost::asio::ip::address_v4::from_string(argv[1]);
            boost::asio::ip::basic_endpoint<  boost::asio::ip::tcp > address(ipa, (unsigned short) atoi(argv[2]));
            sock_->connect(address);
            std::cout<<"connected. local address:"<<sock_->local_endpoint()<<" remote address:"<<sock_->remote_endpoint()<<std::endl;
        } catch (const boost::system::system_error& e)
        {
            std::cout<<"ERROR:"<<e.what();
        }
    }
    int dummy;
    std::cin>>dummy;
    return 0;
}

I have 2 computers.

  1. output from computer A:connected. local address:XXXXXX remote address:YYYYY (where XXXXX and YYYY real IPs(IPs same to ping output))

  2. output from computer B:connected. local address: 127.0.0.1 remote address:YYYYY (where YYYY real IP(IPs same to ping output))

Computer A and B have only 1 NIC
Why I got 127.0.0.1? I cant make real connection from IP 127.0.0.1 to YYYY.
how to fix it?
update:
even windows sockets return 127.0.0.1 on problematic host, see code below

WSADATA wsaData;
auto iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    return 1;
}
SOCKET ConnectSocket;
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {

    WSACleanup();
    return 1;
}
sockaddr_in clientService;
clientService.sin_family = AF_INET;

clientService.sin_addr.s_addr = inet_addr(argv[1]);
clientService.sin_port = htons((unsigned short) atoi(argv[2]));
iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService));
if (iResult == SOCKET_ERROR) {
    WSACleanup();
    return 1;
}
struct sockaddr_in sin;
int addrlen = sizeof(sin);
if(getsockname(ConnectSocket, (struct sockaddr *)&sin, &addrlen) == 0 &&
   sin.sin_family == AF_INET &&
   addrlen == sizeof(sin))
{
    char *ip = inet_ntoa(sin.sin_addr);
    std::cout<<ip<<std::endl;
}

127.0.0.1通过真实网卡路由:)连接成功从127.0.0.1到远程ip因为路由:'(

You're mixing APIs, compiler glitches and making the wrong assumptions. Here's the code greatly simplified:

Live On Coliru

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

int main(int argc, char* argv[])
{
    namespace ba = boost::asio;
    using ba::ip::tcp;

    ba::io_service service;
    tcp::socket s(service); 

    if (argc<3)
        return 255;

    try {
        auto it = tcp::resolver(service).resolve({argv[1], argv[2]});
        s.connect(*it); // first resolved value

        std::cout << "Connected " << s.local_endpoint() << " -> " << s.remote_endpoint() << "\n";

        // demo write
        ba::write(s, ba::buffer("hello world\n"));
    } catch (const boost::system::system_error& e) {
        std::cout << "ERROR:" << e.what() << "\n";
    }
}

On the online demo, the output is:

Connected 127.0.0.1:40468 -> 127.0.0.1:6767
hello world

Indeed, when I connect to a remote host not on the loopback network, I get:

sehe@desktop:/tmp$ ./test serverbox 6767
Connected 192.168.188.20:47338 -> 192.168.188.24:6767

I'm not too sure hwy you'd get the loopback address there, but I wouldn't at all rule out the possibility that you're doing the tostring wrong.

See

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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