[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:
Matteo Cypriani 2011-08-22 20:40:16 +02:00
parent a7bfa7eb07
commit 46e295dc09
2 changed files with 30 additions and 11 deletions

View File

@ -99,6 +99,11 @@ int main(int argc, char *argv[])
/* Set up the semaphore */ /* Set up the semaphore */
sem_init(&lock_results, 0, 1) ; sem_init(&lock_results, 0, 1) ;
/* Prepare the TCP socket */
ret = init_tcp_socket() ;
if (ret)
goto exit ;
/* Launch the TCP thread */ /* Launch the TCP thread */
ret = pthread_create(&tcp_server_thread, NULL, ret = pthread_create(&tcp_server_thread, NULL,
&tcp_server, 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 ; struct sockaddr_in server_addr ;
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 ;
tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0) ; tcp_sockfd = socket(AF_INET, SOCK_STREAM, 0) ;
if (tcp_sockfd < 0) if (tcp_sockfd < 0)
{ {
perror("Error opening the TCP socket") ; perror("Error opening the TCP socket") ;
exit(1) ; return tcp_sockfd ;
} }
bzero((char *) &server_addr, sizeof(server_addr)) ; 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_addr.s_addr = INADDR_ANY ;
server_addr.sin_port = htons(TCP_PORT) ; server_addr.sin_port = htons(TCP_PORT) ;
if (bind(tcp_sockfd, (struct sockaddr *) &server_addr, if (bind(tcp_sockfd, (struct sockaddr *) &server_addr,
sizeof(server_addr)) < 0) sizeof(server_addr)) < 0)
{ {
perror("Error binding the TCP socket") ; 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) ; listen(tcp_sockfd, NB_CONNECTIONS) ;
client_len = sizeof(client_addr) ; client_len = sizeof(client_addr) ;

View File

@ -39,6 +39,7 @@ typedef struct _results_list
int receive_udp(void) ; int receive_udp(void) ;
void store_result(owl_result *result) ; void store_result(owl_result *result) ;
int init_tcp_socket(void) ;
void* tcp_server(void *NULL_value) ; void* tcp_server(void *NULL_value) ;
int int
extract_request_from_message(char client_request[CLIENT_REQUEST_STRLEN], extract_request_from_message(char client_request[CLIENT_REQUEST_STRLEN],