commit 716453901872eff2a0889d2a2eb73c0b16730968
parent ac40afb00baee79055558f057f830715cf7ee5d9
Author: solusipse <solus1ps3@gmail.com>
Date: Mon, 9 Sep 2013 03:47:25 +0200
parent ac40afb00baee79055558f057f830715cf7ee5d9
Author: solusipse <solus1ps3@gmail.com>
Date: Mon, 9 Sep 2013 03:47:25 +0200
moved address function call to thread
2 files changed, 22 insertions(+), 11 deletions(-)
diff --git a/fiche.c b/fiche.c @@ -49,14 +49,17 @@ int main(int argc, char **argv) void *thread_connection(void *args) { + int connection_socket = ((struct thread_arguments *) args ) -> connection_socket; + struct sockaddr_in client_address = ((struct thread_arguments *) args ) -> client_address; + + int n; char buffer[BUFSIZE]; - int n, client = *(int *)args; bzero(buffer, BUFSIZE); - - int status = recv(client, buffer, BUFSIZE, 0); + int status = recv(connection_socket, buffer, BUFSIZE, 0); if (status != -1) { + get_client_address(client_address); char slug[SLUG_SIZE]; generate_url(buffer, slug); @@ -64,17 +67,17 @@ void *thread_connection(void *args) strcpy(response, DOMAIN); strcat(response, slug); strcat(response, "/\n"); - write(client, response, strlen(response)); + write(connection_socket, response, strlen(response)); } else { + get_client_address(client_address); printf("Invalid connection.\n"); - write(client, "Use netcat.\n", 13); + write(connection_socket, "Use netcat.\n", 13); } - close(client); + close(connection_socket); pthread_exit(NULL); - return NULL; } void perform_connection(int listen_socket) @@ -84,9 +87,9 @@ void perform_connection(int listen_socket) struct sockaddr_in client_address; int address_lenght = sizeof(client_address); - int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, &address_lenght); + int connection_socket = accept(listen_socket, (struct sockaddr *) &client_address, (void *) &address_lenght); - struct timeval timeout; + struct timeval timeout; timeout.tv_sec = 10; timeout.tv_usec = 0; @@ -95,9 +98,11 @@ void perform_connection(int listen_socket) if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) error(); - get_client_address(client_address); + struct thread_arguments arguments; + arguments.connection_socket = connection_socket; + arguments.client_address = client_address; - if (pthread_create(&thread_id, NULL, &thread_connection, &connection_socket) != 0) + if (pthread_create(&thread_id, NULL, &thread_connection, &arguments) != 0) error(); else pthread_detach(thread_id);
diff --git a/fiche.h b/fiche.h @@ -65,4 +65,10 @@ void parse_parameters(int argc, char **argv); struct sockaddr_in set_address(struct sockaddr_in serveraddr); +struct thread_arguments +{ + int connection_socket; + struct sockaddr_in client_address; +}; + #endif \ No newline at end of file
: nply/fiche2