Search code examples
c++socketsclient-servergetaddrinfo

getaddrinfo always connect. even when no passive-open connection listen on that port


I'm writing a server client program using socket programming c++ in Ubuntu.

This is the code connect client to server.

void setParent(string name,int parentPort){
    struct addrinfo hints, *serverInfo , *rp;
    int errcode;
    char addrstr[100];
    void *ptr;
    int sfd;
    std::string parentPortStr = std::to_string(parentPort);
    memset (&hints, 0, sizeof (hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    hints.ai_flags    = AI_PASSIVE;
    cerr << name << " " << parentPortStr << endl;
    errcode = getaddrinfo (name.c_str() , parentPortStr.c_str(), &hints, &serverInfo);
    if (errcode != 0)
    {
        cerr << "getaddrinfo has error" << endl;
        return;
    }

    for (rp = serverInfo; rp != NULL; rp = rp->ai_next) {
        cerr << "Trying next api " << rp->ai_family << " " << rp->ai_socktype << " " << rp ->ai_protocol << endl;
        sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (sfd == -1)
            continue;
        if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1){
            int enabled = 1;
            setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &enabled, sizeof(int));
            break;
        }
        close(sfd);
    }
    freeaddrinfo(serverInfo);
    if(sfd == -1){
        cerr << "cannot connect to father" << endl;
        return;
    }
    cerr << "connected to father successfuly. socket: " << sfd << endl;
    fatherSocket = sfd;
    return;
}

when I call this code like this: setParent("localhost" , "300"); It will always accept connection. It doesnt matter if any program listen on port 7300 or not.

here is Debug output:

setparent localhost 300
localhost 7300
Trying next api 2 1 6
connected to father successfully. socket: 5

and it doesn't matter if I change the port. It always try api with ai_family: 2, ai_socktype: 1, ai_protocol: 6 and will successfully connect to it.

here is "sudo netstat -tulpn" result:

tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      1163/dnsmasq    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      4814/cupsd      
tcp6       0      0 ::1:631                 :::*                    LISTEN          4814/cupsd      
udp        0      0 0.0.0.0:45464           0.0.0.0:*                           601/avahi-daemon: r
udp        0      0 0.0.0.0:631             0.0.0.0:*                           989/cups-browsed
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           601/avahi-daemon: r
udp        0      0 0.0.0.0:26517           0.0.0.0:*                           5053/dhclient   
udp        0      0 127.0.1.1:53            0.0.0.0:*                           1163/dnsmasq    
udp        0      0 0.0.0.0:68              0.0.0.0:*                           5053/dhclient   
udp6       0      0 :::50297                :::*                                601/avahi-daemon: r
udp6       0      0 :::5353                 :::*                                601/avahi-daemon: r
udp6       0      0 :::46583                :::*                                5053/dhclient

As you see no one listen on port 7300.

I can't get what is happening there.


Solution

  • As you see from your own netstat display, no-one is connected to 7300 either.

    You're testing the wrong thing. You should test whether enabled has become 1. connect() won't (can't) magically set sfd to -1 if it fails.