[RESOLVED] WinSock listen() method seems to be blocked only after a reboot
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: [RESOLVED] WinSock listen() method seems to be blocked only after a reboot

  1. #1
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    [RESOLVED] WinSock listen() method seems to be blocked only after a reboot

    I'm not sure what's causing it. I'm developing a server/client type applications that communicate via TCP protocol using WinSock. All connections are done on a local network using IP addresses via arbitrary port 32888 and up.

    The code is very similar to this one. I'll copy it here as well:

    Code:
    #ifndef UNICODE
    #define UNICODE
    #endif
    
    #define WIN32_LEAN_AND_MEAN
    
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdio.h>
    
    // Need to link with Ws2_32.lib
    #pragma comment(lib, "ws2_32.lib")
    
    int wmain()
    {
    
        //----------------------
        // Initialize Winsock
        WSADATA wsaData;
        int iResult = 0;
    
        SOCKET ListenSocket = INVALID_SOCKET;
        sockaddr_in service;
    
        iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
        if (iResult != NO_ERROR) {
            wprintf(L"WSAStartup() failed with error: %d\n", iResult);
            return 1;
        }
        //----------------------
        // Create a SOCKET for listening for incoming connection requests.
        ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (ListenSocket == INVALID_SOCKET) {
            wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }
        //----------------------
        // The sockaddr_in structure specifies the address family,
        // IP address, and port for the socket that is being bound.
        service.sin_family = AF_INET;
        service.sin_addr.s_addr = htonl(INADDR_ANY);
        service.sin_port = htons(32888);
    
        iResult = bind(ListenSocket, (SOCKADDR *) & service, sizeof (service));
        if (iResult == SOCKET_ERROR) {
            wprintf(L"bind function failed with error %d\n", WSAGetLastError());
            iResult = closesocket(ListenSocket);
            if (iResult == SOCKET_ERROR)
                wprintf(L"closesocket function failed with error %d\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }
        //----------------------
        // Listen for incoming connection requests 
        // on the created socket
        if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
            wprintf(L"listen function failed with error: %d\n", WSAGetLastError());
    
        wprintf(L"Listening on socket...\n");
    
        iResult = closesocket(ListenSocket);
        if (iResult == SOCKET_ERROR) {
            wprintf(L"closesocket function failed with error %d\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }
    
        WSACleanup();
        return 0;
    }
    So I'm testing it on Windows 10 pro. When the app first runs, I get a popup from the built-in Windows Firewall, where I enable both private and public connections. After that the app starts to work... until I reboot.

    Additional Details: When the app start up, it adds itself into the Task Scheduler to begin running with "highest privileges" when a user logs on. The app also has requireAdministrator UAC level in the manifest.

    So what happens after a reboot is that the app starts up fine but it remains mute for all incoming connection until I close and restart it manually. There's no warnings or anything in the event log to see why incoming connections are blocked. No WinSock APIs in the code seem to fail either.

    Any idea what's going on there?

    PS. There's no third-party antivirus software installed on this Windows 10.
    Last edited by dc_2000; March 29th, 2017 at 06:04 PM. Reason: Adjusted htonl(INADDR_ANY) part

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,928

    Re: WinSock listen() method seems to be blocked only after a reboot

    Did you add your application into zhe list of "allowed" applications of the Firewall? As well as the TCP port number?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: WinSock listen() method seems to be blocked only after a reboot

    Quote Originally Posted by VictorN View Post
    Did you add your application into zhe list of "allowed" applications of the Firewall? As well as the TCP port number?
    Didn't it do it automatically when the Windows default warning came up and said "yes" to it? You see the app works if I start it manually. It doesn't work only after a reboot. Then if I close it and restart it manually, it works just fine.

    Nonetheless, can I apply those firewall changes programmatically?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,928

    Re: WinSock listen() method seems to be blocked only after a reboot

    Quote Originally Posted by dc_2000 View Post
    Didn't it do it automatically when the Windows default warning came up and said "yes" to it?
    I don't know. But why didn't you check it yourself?
    Victor Nijegorodov

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,928

    Re: WinSock listen() method seems to be blocked only after a reboot

    Quote Originally Posted by dc_2000 View Post
    Nonetheless, can I apply those firewall changes programmatically?
    Banned by Google?
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: WinSock listen() method seems to be blocked only after a reboot

    There's nothing banned there by Google.

    Anyway, just to be on a safe side I completely disabled built in Windows firewall:

    Name:  Capture1.jpg
Views: 15
Size:  22.4 KB

    so here's how it looks now:

    Name:  Capture2.jpg
Views: 16
Size:  45.5 KB

    and rebooted. And it still didn't work. Tried several times.

    Then when I closed the app (with the listen() method) and restarted it manually by double-clicking it, it started to work.

    It's as if something in Windows 10 blocks it besides the firewall. The question is what else?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,928

    Re: WinSock listen() method seems to be blocked only after a reboot

    So the problem is only when your app starts from Task Scheduler. Isn't it?
    Did you try to implement some logging in your app?
    Victor Nijegorodov

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,928

    Re: WinSock listen() method seems to be blocked only after a reboot

    Quote Originally Posted by dc_2000 View Post
    ...
    It's as if something in Windows 10 blocks it besides the firewall. The question is what else?
    Did you test it in other OS like Windows 7/8/8.1?
    Victor Nijegorodov

  9. #9
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: WinSock listen() method seems to be blocked only after a reboot

    Yep, just tried it on Windows 7 Pro. It works like a charm there.

    As for logging, there's really nothing I can log here. The listen() method returns a value other than SOCKET_ERROR and then accept() goes into a waiting state and never returns.
    Last edited by dc_2000; March 28th, 2017 at 02:34 PM.

  10. #10
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: WinSock listen() method seems to be blocked only after a reboot

    Also just disabled the built-in Windows 10 antivirus and still no luck:

    Name:  Capture3.PNG
Views: 14
Size:  21.1 KB

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,928

    Re: WinSock listen() method seems to be blocked only after a reboot

    Quote Originally Posted by dc_2000 View Post
    Yep, just tried it on Windows 7 Pro. It works like a charm there.
    Then sorry! I don't use Windows 10.
    Victor Nijegorodov

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,588

    Re: WinSock listen() method seems to be blocked only after a reboot

    Use Wireshark to see what is happening on the network. Set the trigger time for the entry in the task scheduler so that you have time to start Wireshark before the program is triggered. Then you should be able to determine what is going on.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  13. #13
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: WinSock listen() method seems to be blocked only after a reboot

    Thanks.

    You know I don't have much experience with the Wireshark. I'm assuming that you meant installing it on the "listener"-app side, right? Also will it catch connections happening during a reboot, because that's when the failure happens?

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,588

    Re: WinSock listen() method seems to be blocked only after a reboot

    When the app start up, it adds itself into the Task Scheduler to begin running with "highest privileges" when a user logs on
    Either change the Task Scheduler trigger for when it starts running to give a delay so that wireshark can be started - or also create a task scheduler event to also start wireshark. Wireshark will record all network traffic. it's not that difficult to use but does require some understanding of network traffic to interpret the results.

    the app starts up fine but it remains mute for all incoming connection until I close and restart it manually.
    When you re-start it manually, is this also done via the Task Scheduler - or by just running the program?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  15. #15
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    894

    Re: WinSock listen() method seems to be blocked only after a reboot

    I restart it from the Windows Explorer by double clicking it. Would that matter though?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)