| 100 | } |
| 101 | |
| 102 | void |
| 103 | doConnect(int &connectSock, hostent *host, int port, const char *hostname) |
| 104 | { |
| 105 | sockaddr_in addr; |
| 106 | |
| 107 | memset(&addr, 0, sizeof(addr)); |
| 108 | addr.sin_family = AF_INET; |
| 109 | memcpy(&addr.sin_addr.s_addr, host->h_addr, host->h_length); |
| 110 | addr.sin_port = htons(port); |
| 111 | int fd = -1; |
| 112 | for (size_t i = 0; i < 20; i++) { |
| 113 | fd = connect(connectSock, (sockaddr *)&addr, sizeof(addr)); |
| 114 | if (fd != -1) { |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | // Sleep for 100 ms to allow the other process to do a bind. |
| 119 | struct timespec t; |
| 120 | t.tv_sec = 0; |
| 121 | t.tv_nsec = 100 * 1000 * 1000; |
| 122 | nanosleep(&t, NULL); |
| 123 | } |
| 124 | |
| 125 | // If connect fails even after 20 tries, give up. |
| 126 | assert(fd >= 0); |
| 127 | } |