From e67662908f23d954bd9e4c338c3b296641b448e8 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Thu, 27 Jan 2011 13:24:24 +0100 Subject: remove sleep(1) on client and try to connect immediately; also retry several times to connect in case server isn't started yet --- src/game.c | 16 +++++++++++++--- src/net/client.c | 9 +++------ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/game.c b/src/game.c index 6798f64..c82f746 100644 --- a/src/game.c +++ b/src/game.c @@ -137,12 +137,22 @@ void start_game(const bool servermode, const char* addr, const char* port, const { int sock; - sleep(1); // TODO make sure server process is listening - if(addr == NULL) addr = "localhost"; - sock = client_connect_server(addr, port); + for(int i=0; i<5; i++) // try to connect 5 times + { + sock = client_connect_server(addr, port); + if(sock != -1) + break; + usleep(300000); // wait 300 ms before retry + } + if(sock == -1) + { + fprintf(stderr, "Connection refused\n"); + exit(EXIT_FAILURE); + } + data_store_t *data = data_store(); net_send(sock, msg_type_hello_c, NULL); diff --git a/src/net/client.c b/src/net/client.c index 0d1686c..409de6a 100644 --- a/src/net/client.c +++ b/src/net/client.c @@ -15,7 +15,7 @@ * Client side function; connects to specified host:port * @param[in] host Hostname of server * @param[in] port Port of server - * @return Socket with open connection to server + * @return Socket with open connection to server or -1 on error */ int client_connect_server(const char* host, const char* port) { @@ -51,13 +51,10 @@ int client_connect_server(const char* host, const char* port) close(sock); } + freeaddrinfo(result); if(tmp == NULL) - { - printf("failed to connect\n"); - exit(EXIT_FAILURE); - } - freeaddrinfo(result); + return -1; return sock; } -- cgit v1.2.3