moved address function call to thread

This commit is contained in:
solusipse
2013-09-09 03:47:25 +02:00
parent ac40afb00b
commit 7164539018
2 changed files with 23 additions and 12 deletions

27
fiche.c
View File

@@ -49,14 +49,17 @@ int main(int argc, char **argv)
void *thread_connection(void *args) void *thread_connection(void *args)
{ {
char buffer[BUFSIZE]; int connection_socket = ((struct thread_arguments *) args ) -> connection_socket;
int n, client = *(int *)args; struct sockaddr_in client_address = ((struct thread_arguments *) args ) -> client_address;
bzero(buffer, BUFSIZE);
int status = recv(client, buffer, BUFSIZE, 0); int n;
char buffer[BUFSIZE];
bzero(buffer, BUFSIZE);
int status = recv(connection_socket, buffer, BUFSIZE, 0);
if (status != -1) if (status != -1)
{ {
get_client_address(client_address);
char slug[SLUG_SIZE]; char slug[SLUG_SIZE];
generate_url(buffer, slug); generate_url(buffer, slug);
@@ -64,17 +67,17 @@ void *thread_connection(void *args)
strcpy(response, DOMAIN); strcpy(response, DOMAIN);
strcat(response, slug); strcat(response, slug);
strcat(response, "/\n"); strcat(response, "/\n");
write(client, response, strlen(response)); write(connection_socket, response, strlen(response));
} }
else else
{ {
get_client_address(client_address);
printf("Invalid connection.\n"); 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); pthread_exit(NULL);
return NULL;
} }
void perform_connection(int listen_socket) void perform_connection(int listen_socket)
@@ -84,7 +87,7 @@ void perform_connection(int listen_socket)
struct sockaddr_in client_address; struct sockaddr_in client_address;
int address_lenght = sizeof(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_sec = 10;
@@ -95,9 +98,11 @@ void perform_connection(int listen_socket)
if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) if (setsockopt (connection_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
error(); 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(); error();
else else
pthread_detach(thread_id); pthread_detach(thread_id);

View File

@@ -65,4 +65,10 @@ void parse_parameters(int argc, char **argv);
struct sockaddr_in set_address(struct sockaddr_in serveraddr); struct sockaddr_in set_address(struct sockaddr_in serveraddr);
struct thread_arguments
{
int connection_socket;
struct sockaddr_in client_address;
};
#endif #endif