locked
Windows 7 falsely reports TCP sockets are connected RRS feed

  • Question

  • A sample code below will falsely report "connected" to TCP addresses: "localhost" and ports from 50020 to 50119 after some time working. Utility was tested on clear Windows 7 and for sure there are no applications which open mentioned ports for listening.

    The smallest code where the bug appears see below. Application was compiled on MS VS Studion 2005. Project should be a console application with multythreaded support. (Same result was ashived if compiled under Code Gear C++ Builder)

    OS informations: Just installed OS (no additions). Version: Windows 7, 32 bits (NT 6.1 7600 Enterprise).

    (By the way, the sample works fine under Windows XP)

    Am I wrong somewhere? Is it a known bug? (I haven’t find any mentions about such issue in Internet).

    Thank you for any ideas and advises.

    main.cpp:

     

    #include <winsock2.h>
    #include <windows.h>
    #include <process.h>
    #include <stdio.h>
    #pragma hdrstop

    #pragma comment( lib, "ws2_32.lib" )

    void showInfo(char* apInfo);
    void logOut(char* apInfo);

    unsigned __stdcall threadFunc(void* apThread);
    int tryConnect(int anPortNum);

    #define THREAD_COUNT 100

    int main(int argc, char* argv[])
    {
      WSADATA wsaData;

      int nResult = -1;
      nResult = ::WSAStartup(MAKEWORD(2, 0), &wsaData);
      if(nResult)
      {
        showInfo("WSAStartup() error");
      }
      else
      {
        showInfo("WSA Started");
      }


      // Lets create 10 threads
      HANDLE hThread = NULL;
      unsigned int uiThreadID = 0;
      int* pnPort = NULL;
      for(int i=0; i<THREAD_COUNT; i++)
      {

        // There are leaks, but it is not important now
        pnPort = new int;
        if(pnPort)
        {
          // We are goint to try connect to ports 50020 to 50029
          *pnPort = 50020 + i;
        }

        hThread = (HANDLE)_beginthreadex(
          NULL,
          1024*1024,
          &threadFunc,
          (void*)pnPort,
          0,
          &uiThreadID);
        if(hThread == INVALID_HANDLE_VALUE)
        {
          showInfo("Error thread creating");
          continue;
        }
      }

      // Don’t want to code correct exit. Ctrl+C work fine for exit.
      for(;;)
      {
        ::Sleep(INFINITE);
      }

      return 0;
    }
    //---------------------------------------------------------------------------


    unsigned __stdcall threadFunc(
      void* apThread)
    {
      for(;;)
      {
        tryConnect(*((int*)apThread));

        //::Sleep(50);
      }
    };
    //---------------------------------------------------------------------------

    int tryConnect(
      int anPortNum)
    {

      //:
      struct sockaddr_in sockaddr;
      ::ZeroMemory(&sockaddr, sizeof(sockaddr));

      sockaddr.sin_family = AF_INET;
      sockaddr.sin_addr.S_un.S_addr = ADDR_ANY;


      {
        const char* szHostName = "localhost";
        ULONG ulIP = ADDR_ANY;

        //
        struct hostent* pbuf_host = NULL;
        pbuf_host = ::gethostbyname(szHostName);
        if(!pbuf_host)
        {
          showInfo("pbuf_host is NULL");
          return -1;
        }

        ulIP = *((ULONG*)pbuf_host->h_addr_list[0]);

        sockaddr.sin_addr.S_un.S_addr = ulIP;
      }

      {
        sockaddr.sin_port = ::htons(anPortNum);
      }

      socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

      SOCKET sSocket = INVALID_SOCKET;
      sSocket = ::WSASocket(
        AF_INET,
        SOCK_STREAM,
        IPPROTO_TCP,
        NULL,
        0,
        WSA_FLAG_OVERLAPPED);
      if(sSocket == INVALID_SOCKET)
      {
        DWORD dwErrcode = WSAGetLastError();
        showInfo("Error creating socket");

        return -3;
      }


      // try connect:
      int nResult = -1;
      nResult = ::connect(
        sSocket,
        (const struct sockaddr*)&sockaddr,
        sizeof(sockaddr));
      if(nResult)
      {
        DWORD dwErrcode = WSAGetLastError();

        // it is correct behavior
        //showInfo("Error in connect");
        char buf[128];
        sprintf(buf, "Error connecting %d code %d", anPortNum, dwErrcode);
        logOut(buf);

        ::closesocket(sSocket);

        return -5;
      }
      else
      {
        DWORD dwErrcode = WSAGetLastError();

        char szInfo[128];
        sprintf(szInfo, "Connection is established! Port: %d Code: %d", anPortNum, dwErrcode);
        showInfo(szInfo);

        ::closesocket(sSocket);
      }

      return 0;
    };
    //---------------------------------------------------------------------------


    void showInfo(char* apInfo)
    {
      logOut(apInfo);
      ::MessageBoxA(NULL, apInfo, "Information", MB_OK);
    };
    //---------------------------------------------------------------------------

    void logOut(char* apInfo)
    {
      SYSTEMTIME sysTime;
      ::GetSystemTime(&sysTime);

      printf("%d:%d:%d.%d %s \r\n",
        sysTime.wHour,
        sysTime.wMinute,
        sysTime.wSecond,
        sysTime.wMilliseconds,
        apInfo);
    };
    //---------------------------------------------------------------------------

     

    Tuesday, July 27, 2010 12:03 PM