Diff
Not logged in

Differences From:

File src/winhttp.c part of check-in [b9eec2d277] - Fix a bug in the new win32 server implementation. by drh on 2008-05-17 19:24:52. [view]

To:

File src/winhttp.c part of check-in [d8ceb4ad47] - The "ui" and "server" commands no longer quit if they cannot open TCP port 8080. They keep trying with consecutive ports until they find one that works - up to 100 ports. by drh on 2008-11-10 01:13:35. [view]

@@ -135,35 +135,50 @@
 /*
 ** Start a listening socket and process incoming HTTP requests on
 ** that socket.
 */
-void win32_http_server(int iPort, char *zBrowser){
+void win32_http_server(int mnPort, int mxPort, char *zBrowser){
   WSADATA wd;
   SOCKET s;
   SOCKADDR_IN addr;
   int idCnt = 0;
+  int iPort = mnPort;
 
   if( WSAStartup(MAKEWORD(1,1), &wd) ){
     fossil_fatal("unable to initialize winsock");
   }
-  zTempPrefix = mprintf("fossil_server_P%d_", iPort);
-  s = socket(AF_INET, SOCK_STREAM, 0);
-  if( s==INVALID_SOCKET ){
-    fossil_fatal("unable to create a socket");
+  while( iPort<mxPort ){
+    s = socket(AF_INET, SOCK_STREAM, 0);
+    if( s==INVALID_SOCKET ){
+      fossil_fatal("unable to create a socket");
+    }
+    addr.sin_family = AF_INET;
+    addr.sin_port = htons(iPort);
+    addr.sin_addr.s_addr = htonl(INADDR_ANY);
+    if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
+      closesocket(s);
+      iPort++;
+      continue;
+    }
+    if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
+      closesocket(s);
+      iPort++;
+      continue;
+    }
+    break;
   }
-  addr.sin_family = AF_INET;
-  addr.sin_port = htons(iPort);
-  addr.sin_addr.s_addr = htonl(INADDR_ANY);
-  if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
-    closesocket(s);
-    fossil_fatal("unable to bind");
+  if( iPort>mxPort ){
+    if( mnPort==mxPort ){
+      fossil_fatal("unable to open listening socket on ports %d", mnPort);
+    }else{
+      fossil_fatal("unable to open listening socket on any"
+                   " ports %d..%d", mnPort, mxPort);
+    }
   }
-  if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
-    closesocket(s);
-    fossil_fatal("unable to listen");
-  }
+  zTempPrefix = mprintf("fossil_server_P%d_", iPort);
   printf("Listening for HTTP requests on TCP port %d\n", iPort);
   if( zBrowser ){
+    zBrowser = mprintf(zBrowser, iPort);
     printf("Launch webbrowser: %s\n", zBrowser);
     system(zBrowser);
   }
   printf("Type Ctrl-C to stop the HTTP server\n");