3

If you run this example, you'll see the port is never listed by netstat. Why? And how do I make it so?

#include <WinSock.h>
#include <io.h>
#include <stdio.h>

#pragma comment(lib, "WS2_32")

int main() {
    WORD wVers = MAKEWORD(2, 2);
    WSADATA wsa;
    WSAStartup(wVers, &wsa);
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 6);
    if (sock != INVALID_SOCKET) {
        struct sockaddr_in addr = { 0 };
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        int addrlen = sizeof(addr);
        bind(sock, (struct sockaddr *)&addr, addrlen);
        if (getsockname(sock, (struct sockaddr *)&addr, &addrlen) == 0) {
            fprintf(stdout, "HANDLE = %d, port = %d\n", sock, addr.sin_port);
            fflush(stdout);
            system("netstat -a -n");
        }
        closesocket(sock);
    }
    WSACleanup();
}
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 2
    Did you verify that bind() succeeds ? What if you also call listen() after bind() ? – nos Nov 29 '16 at 21:17
  • @nos: Yes it succeeds, otherwise getsockname wouldn't have had a port to return. But I'm not sure why listen is necessary. The port is already assigned to me, right? – user541686 Nov 29 '16 at 21:31
  • `bind` does indeed [reserve](http://pastebin.com/LQavGgUf) the port then and there, at least on Linux. – ikegami Nov 29 '16 at 21:49
  • 1
    I believe netstat only shows listening (or connected) sockets. While your socket is created and the port assigned, it's not yet listening for connections. – nos Nov 29 '16 at 21:49
  • @nos: do you know of any way to check which ports are in use? – user541686 Nov 29 '16 at 21:52
  • The best and indeed the only reliable way to see whether *anything* is available is to try to use it. – user207421 Nov 29 '16 at 21:54
  • @EJP: sorry, I meant via command line. Like netstat. Not through writing code. – user541686 Nov 29 '16 at 21:55
  • huh, `netstat` was obtained through writing code. – ikegami Nov 29 '16 at 21:56
  • Note, there is a bug in your code, the port number is in network endian, so you should do `fprintf(stdout, "HANDLE = %d, port = %d\n", sock, ntohs(addr.sin_port));` - still netstat is not going to show it until connect or listen is called. – nos Nov 29 '16 at 21:56
  • @ikegami: I mean for a user, not a programmer. You understand what I'm asking. – user541686 Nov 29 '16 at 21:57
  • @nos: whoa, really?? I have to check this but that would potentially explain a lot, thanks! – user541686 Nov 29 '16 at 21:58
  • Windows sometimes lists sockets opened by applications under "svchost.exe" or just system process. – Daniel Nov 29 '16 at 21:58
  • @Dani: not the case here – user541686 Nov 29 '16 at 21:59
  • Re "*You understand what I'm asking*", I do. I'm not suggesting you have each of your users write a program; I'm suggesting they could use a program that you wrote just like they are using one that Microsoft wrote (or ported). – ikegami Nov 29 '16 at 22:04
  • @ikegami: So there's no built-in way? That's all I'm asking. – user541686 Nov 29 '16 at 22:06
  • @nos: Just verified this :) thanks for letting me know! – user541686 Nov 29 '16 at 22:53

1 Answers1

17

netstat -a only lists connected sockets and listening socket.

  -a            Displays all connections and listening ports.

Neither connect nor listen was called on your socket, so it falls outside the purview of netstat -a.

However, since Windows 10, you can use netstat -q.

  -q            Displays all connections, listening ports, and bound
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    Actually - at least on windows 10 netstat has this flag `" -q Displays all connections, listening ports, and bound"` This shows these incomplete sockets with the word "BOUND" in the State column – nos Nov 29 '16 at 22:05
  • @nos, Thanks. Added to my answer. – ikegami Nov 29 '16 at 22:07
  • Now that's a complete answer. Thanks! – user541686 Nov 29 '16 at 22:08
  • 1
    The `-q` flag seems not to work in conjunction with the `-b` (shows binary/exe name), but it does works with `-o` (shows owning pid), which is good enough. (or at least this is my experience on Win10 1903) – Eli Finkel Jun 22 '19 at 07:50