Commits

Anonymous committed 832a6f4

Made client keep trying to connect after it fails the first time instead of bailing out.

Comments (0)

Files changed (1)

 
 client_info handshake(const char* const IP) {
     c_info.width = c_info.height = 0;
-    int status = getaddrinfo(IP, PORT, &hints, &server_info);
-    if (status != 0) {
+    int status;
+    while(1) {
+      status = getaddrinfo(IP, PORT, &hints, &server_info);
+      if (status != 0) {
         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
-        return c_info; // Failure
-    }
+        continue;
+      }
 
-    /* Loop through all results and connect to the first one we can */
-    for (p= server_info; p != NULL; p = p->ai_next) {
+      /* Loop through all results and connect to the first one we can */
+      for (p= server_info; p != NULL; p = p->ai_next) {
         if ((connect_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
-            perror("Client: socket");
-            continue;
+          perror("Client: socket");
+          continue;
         }
 
         if (connect(connect_socket, p->ai_addr, p->ai_addrlen) == -1) {
-            closesocket(connect_socket);
-            perror("Client: connect");
-            continue;
+#ifdef WINDOWS
+          closesocket(connect_socket);
+#else
+          close(connect_socket);
+#endif
+          perror("Client: connect");
+          continue;
         }
         break;
-    }
+      }
 
-    if (p == NULL) { /* Failed to connect */
+      if (p == NULL) { /* Failed to connect */
         fprintf(stderr, "Client: failed to connect\n");
-        return c_info; // Failure
+        continue; /* Keep trying */
+      } else {
+        break;
+      }
     }
 
     freeaddrinfo(server_info);