[UDP-HTTP] Init. TCP socket in a separate function
Initialise the TCP socket in the new function. This is to avoid exit() in the thread (memory leak).
This commit is contained in:
parent
a7bfa7eb07
commit
46e295dc09
|
@ -99,6 +99,11 @@ int main(int argc, char *argv[])
|
|||
/* Set up the semaphore */
|
||||
sem_init(&lock_results, 0, 1) ;
|
||||
|
||||
/* Prepare the TCP socket */
|
||||
ret = init_tcp_socket() ;
|
||||
if (ret)
|
||||
goto exit ;
|
||||
|
||||
/* Launch the TCP thread */
|
||||
ret = pthread_create(&tcp_server_thread, NULL,
|
||||
&tcp_server, NULL) ;
|
||||
|
@ -216,23 +221,18 @@ void store_result(owl_result *new_result)
|
|||
|
||||
|
||||
/*
|
||||
* Opens the TCP socket and wait for requests from HTTP clients.
|
||||
* Opens the TCP socket.
|
||||
* Returns a non-zero value in case of error.
|
||||
*/
|
||||
void* tcp_server(void *NULL_value)
|
||||
int init_tcp_socket()
|
||||
{
|
||||
int newsockfd ;
|
||||
socklen_t client_len ;
|
||||
struct sockaddr_in server_addr, client_addr ;
|
||||
ssize_t nbytes ; // recv/send return value
|
||||
char client_message[CLIENT_MESSAGE_STRLEN] ;
|
||||
char client_request[CLIENT_REQUEST_STRLEN] ;
|
||||
int request_id ;
|
||||
struct sockaddr_in server_addr ;
|
||||
|
||||
tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0) ;
|
||||
if (tcp_sockfd < 0)
|
||||
{
|
||||
perror("Error opening the TCP socket") ;
|
||||
exit(1) ;
|
||||
return tcp_sockfd ;
|
||||
}
|
||||
|
||||
bzero((char *) &server_addr, sizeof(server_addr)) ;
|
||||
|
@ -240,13 +240,31 @@ void* tcp_server(void *NULL_value)
|
|||
server_addr.sin_addr.s_addr = INADDR_ANY ;
|
||||
server_addr.sin_port = htons(TCP_PORT) ;
|
||||
|
||||
|
||||
if (bind(tcp_sockfd, (struct sockaddr *) &server_addr,
|
||||
sizeof(server_addr)) < 0)
|
||||
{
|
||||
perror("Error binding the TCP socket") ;
|
||||
exit(1) ;
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
return 0 ;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Waits for requests from HTTP clients.
|
||||
*/
|
||||
void* tcp_server(void *NULL_value)
|
||||
{
|
||||
int newsockfd ;
|
||||
socklen_t client_len ;
|
||||
struct sockaddr_in client_addr ;
|
||||
ssize_t nbytes ; // recv/send return value
|
||||
char client_message[CLIENT_MESSAGE_STRLEN] ;
|
||||
char client_request[CLIENT_REQUEST_STRLEN] ;
|
||||
int request_id ;
|
||||
|
||||
listen(tcp_sockfd, NB_CONNECTIONS) ;
|
||||
client_len = sizeof(client_addr) ;
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ typedef struct _results_list
|
|||
|
||||
int receive_udp(void) ;
|
||||
void store_result(owl_result *result) ;
|
||||
int init_tcp_socket(void) ;
|
||||
void* tcp_server(void *NULL_value) ;
|
||||
int
|
||||
extract_request_from_message(char client_request[CLIENT_REQUEST_STRLEN],
|
||||
|
|
Loading…
Reference in New Issue