简体   繁体   中英

Winsock2's listen() function finds a connection for every port; even those that don't exist?

I'm attempting to create a method that listens for a connection request to a specific port using a TCP protocol, with no libraries other than those that come with the Windows OS. The method seems to work fine with creating a socket and binding to a port; the problem seems to be with the listen() function. Even with no connection request to any port, it continually returns the value of zero, meaning, straight off of Microsoft's website -

If no error occurs, listen returns zero.

The strange part is that this happens with all port values; it seems to find a connection request for randomly attempted ports, ranging from 1234, to 8000, to -154326. For each of these, it's returning a value of zero.

What it should be doing is continually running until a connection request is found (this is what SOMAXCONN apparently indicates); once again, straight off of Microsoft's website -

If there are no available socket descriptors, listen attempts to continue to function.

Here is the method itself -

bool listenOnPort(SOCKET networkSocket, int portNumber) {
    WSADATA wsadata;
    int error = WSAStartup(0x0202, &wsadata);
    if(error) {
        cout << "Failed to start up Windows Sockets API." << endl;
        return false;
    }
    if(wsadata.wVersion != 0x0202) {
        WSACleanup();
        cout << "Failed to find a valid Windows Sockets API." << endl;
        return false;
    }
    SOCKADDR_IN address;
    address.sin_family = AF_INET;
    address.sin_port = htons(portNumber);
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    networkSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if(networkSocket == INVALID_SOCKET) {
        cout << "Failed to create a network socket." << endl;
        return false;
    }
    if(bind(networkSocket, (LPSOCKADDR)&address, sizeof(address)) == SOCKET_ERROR) {
        cout << "Failed to bind to the port." << endl;
        return false;
    }
    cout << "Listening for a connection to port " << portNumber <<"..." << endl;
    listen(networkSocket, SOMAXCONN);
    cout << "Found a connection!" << endl;
}

Any explanation/word of advice is appreciated - thank you ahead of time!

You've confused listen with accept . listen reserves the port for your application, and queues incoming connections. accept waits for an incoming connection (if one isn't already queued).

listen will succeed when there is no incoming connection attempt.

http://linux.die.net/man/2/listen

listen() marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming connection requests using accept(2).

You must call "listen()" before you can call "accept()"; but "accept()" is the call that accepts new connections (and gives you a new socket for each new connection).

Here's the man page for "accept()":

http://linux.die.net/man/2/accept

Better, look at Beej's Guide for an excellent introduction to sockets programming:

http://beej.us/guide/bgnet/output/html/multipage/

PS:

And don't forget to call WSAStartup() if you're using Windows sockets :)

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