diff options
Diffstat (limited to 'client.c')
-rw-r--r-- | client.c | 41 |
1 files changed, 35 insertions, 6 deletions
@@ -45,7 +45,7 @@ int main(int argc, char *argv[]) char s[INET6_ADDRSTRLEN]; if (argc != 4) { - fprintf(stderr,"<server IP> <server port> <listener port>\n"); + fprintf(stderr, "usage: <server IP> <server port> <listener port>\n"); exit(1); } @@ -92,7 +92,14 @@ int main(int argc, char *argv[]) pthread_t reply_thread; pthread_create(&reply_thread, NULL, reply_thread_routine, (void*)sockfd); - usleep(1000); + usleep(100); + + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = TIMEOUT; + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + perror("setsockopt"); + } struct Hello hello; hello.commandType = 0; @@ -123,6 +130,11 @@ int main(int argc, char *argv[]) // printf("Changing to station %d.\n", inputInt); // send the command to change the station + tv.tv_sec = 0; + tv.tv_usec = TIMEOUT; + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) { + perror("setsockopt"); + } struct SetStation setStation; setStation.commandType = 1; setStation.stationNumber = htons(inputInt); @@ -148,6 +160,8 @@ void *reply_thread_routine(void* args) { perror("recv"); exit(1); } + + fprintf(stderr, "reply_type: %d\n", reply_type); if (reply_type == 2) { // we have a welcome message // recv the message, check for errors too @@ -158,10 +172,16 @@ void *reply_thread_routine(void* args) { exit(1); } uint16_t num_stations = ntohs(buf_num_stations); - fflush(stdout); - printf("Click q to end stream.\n"); - printf("Welcome to Snowcast! The server has %u stations.\n", num_stations); - fflush(stdout); + fprintf(stdout, "Welcome to Snowcast! The server has %u stations.\n", num_stations); + fprintf(stderr, "Welcome to Snowcast! The server has %u stations.\n", num_stations); + // printf("Click q to end stream.\n"); + + struct timeval no_tv; + no_tv.tv_sec = 0; + no_tv.tv_usec = 0; + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &no_tv, sizeof(no_tv)) < 0) { + perror("setsockopt"); + } continue; } @@ -192,13 +212,22 @@ void *reply_thread_routine(void* args) { exit(1); } + printf("string size: %d\n", string_size); + char *message = malloc(string_size); if(message == NULL) { perror("malloc in message"); } if (recv_all(sockfd, message, &string_size) == -1) { perror("recv_all"); exit(1); } + struct timeval no_tv; + no_tv.tv_sec = 0; + no_tv.tv_usec = 0; + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &no_tv, sizeof(no_tv)) < 0) { + perror("setsockopt"); + } printf("Invalid protocol: %s. Exiting.\n", message); + fprintf(stderr, "Invalid protocol: %s. Exiting.\n", message); free(message); close(sockfd); exit(1); |