title: Why is Windows 7 firewall preventing PASV FTP connections?
body: <p>I was trying to get CFtpServer's first example program running on a Windows 7 Pro, x64 system. After much beating around the bush and not believing what I was seeing, I got the problem down to the following simple program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  #include &lt;windows.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;stdio.h&gt;
  
  
  #define die(code) { cerr &lt;&lt; "die at " &lt;&lt; __FILE__ &lt;&lt; " " &lt;&lt; __LINE__ &lt;&lt; " "; exit(code); }
  
  int main(int argc, char **argv)
  {
      short port = 21;
  
      if (argc == 2) {
          port = atoi(argv[1]);
      }
  
      WSADATA WSAData;
      if ( WSAStartup( MAKEWORD(2, 2), &amp;WSAData) != 0)
          die(1);
  
      SOCKET ls = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//!!! proto 0 in ftpdmin!
      if (ls == INVALID_SOCKET) die(1);
  
      struct sockaddr_in sin;
      sin.sin_family = AF_INET;
      sin.sin_addr.s_addr = INADDR_ANY;
      sin.sin_port = htons( port );
  
      if (bind( ls, (struct sockaddr *) &amp;sin, sizeof( struct sockaddr_in ) )
              == SOCKET_ERROR) die(2);
  
      if (listen( ls, 1 ) == SOCKET_ERROR ) //!!! backlog 1 in ftpdmin!
          die(3);
  
  
      // wait for connect, transmit till error
      SOCKET ts;
      for( ;; ) {
          ts = accept( ls, NULL, NULL );
          if (ts == INVALID_SOCKET) die(5);
  
          // now write some things to that socket.
          int i=0;
          for(;;) {
              char buf[256];
              sprintf(buf, "%d Testing...\r\n",i+224);
              if (send(ts, buf, strlen(buf), 0) &lt; 0) {
                  DWORD err = WSAGetLastError();
                  cerr &lt;&lt; "send failed with " &lt;&lt; err &lt;&lt; endl;
                  break;
              }
              Sleep(1000);
              i = (i+1)%10;
          }
  
          Sleep(1000);
          closesocket(ts);
      }
  }
  </code></pre>
  
  <p>This program opens the specified socket, listens on it for connections. When it gets a connection, it proceeds to write strings that bear a passing resemblance to the string an FTP server might use to respond to the PASV command. It will keep transmitting strings, once a second, until something goes wrong.</p>
  
  <p>On my system, connecting to this 'server' using the nc.exe command, I see a few strings, then the socket will close (the error printed by the 'server' is 10053). </p>
  
  <p>If I disabled the Windows firewall, I see strings as long as I care to leave the nc command running.</p>
  
  <p>I've seen two different variations, and I don't know what causes the difference: Sometimes it would stop when it transmitted the string '227 ', later it started dying on '229 '. It's giving every appearance of being sensitive to the text being sent.</p>