Added timeout for connection

This commit is contained in:
lucic71 2020-05-11 18:21:06 +03:00
parent eafb860400
commit 7029299b2d
2 changed files with 17 additions and 0 deletions

View File

@ -7,6 +7,7 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <stdbool.h>
#include <time.h>
#include "connection.h"
#include "error.h"
@ -40,6 +41,16 @@ int open_connection(char *host_ip, int portno, int ip_type, int socket_type, int
serv_addr.sin_port = htons(portno);
inet_aton(host_ip, &serv_addr.sin_addr);
/*
* Set timeout.
*
*/
struct timeval tv;
tv.tv_sec = CONNECTION_TIMEOUT;
tv.tv_usec = 0;
setsockopt(sockfd, IPPROTO_TCP, SO_RCVTIMEO, (const char *) &tv, sizeof(tv));
/*
* Connect to server.
*

View File

@ -10,6 +10,12 @@
#define CONTENT_LENGTH "Content-Length: "
#define CONTENT_LENGTH_SIZE (sizeof(CONTENT_LENGTH) - 1)
/*
* Waits 5 second to connect to server.
*
*/
#define CONNECTION_TIMEOUT 5
/*
* Adds a line to a string message.
*