Create Dispatcher thread
This commit is contained in:
parent
75c916d6c2
commit
44f2df363a
1 changed files with 8 additions and 1 deletions
|
@ -22,7 +22,7 @@ int main(void) {
|
||||||
int message_pipe[2];
|
int message_pipe[2];
|
||||||
struct sockaddr_in server_socket;
|
struct sockaddr_in server_socket;
|
||||||
struct sockaddr_in client_socket;
|
struct sockaddr_in client_socket;
|
||||||
pthread_t clientlistener, clientsender;
|
pthread_t dispatcher, clientlistener, clientsender;
|
||||||
void *retval;
|
void *retval;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
long addr_len;
|
long addr_len;
|
||||||
|
@ -80,6 +80,11 @@ int main(void) {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pthread_create(&dispatcher, NULL, Dispatcher, NULL) != 0) {
|
||||||
|
fprintf(stderr, "pthread_create: error for Dispatcher\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
/* Loop to accept incoming connections */
|
/* Loop to accept incoming connections */
|
||||||
while (1) {
|
while (1) {
|
||||||
if ((client_sockid = accept(serv_sockid, (struct sockaddr *) &client_socket, (socklen_t *) &addr_len)) < 0) {
|
if ((client_sockid = accept(serv_sockid, (struct sockaddr *) &client_socket, (socklen_t *) &addr_len)) < 0) {
|
||||||
|
@ -116,12 +121,14 @@ int main(void) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: joining here prevents creating other clients, need to move the joins to some client_disconnect function
|
||||||
(void) pthread_join(clientsender, &retval);
|
(void) pthread_join(clientsender, &retval);
|
||||||
(void) pthread_join(clientlistener, &retval);
|
(void) pthread_join(clientlistener, &retval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Server end, closing connection\n");
|
printf("Server end, closing connection\n");
|
||||||
|
(void) pthread_join(dispatcher, &retval);
|
||||||
close(serv_sockid);
|
close(serv_sockid);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue