Create threads, remove arguments
This commit is contained in:
parent
c7c662aa0f
commit
e0ea0473b5
2 changed files with 25 additions and 17 deletions
36
src/server.c
36
src/server.c
|
@ -1,6 +1,7 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
|
@ -15,6 +16,8 @@ int main(void) {
|
|||
int serv_sockid, client_sockid;
|
||||
struct sockaddr_in server_socket;
|
||||
struct sockaddr_in client_socket;
|
||||
pthread_t clientlistener, clientsender;
|
||||
void *retval;
|
||||
pid_t pid;
|
||||
long addr_len;
|
||||
|
||||
|
@ -70,18 +73,17 @@ int main(void) {
|
|||
} else {
|
||||
printf("received connection from %s\n", ip4tostring(ntohl(client_socket.sin_addr.s_addr)));
|
||||
|
||||
/* Create a dedicated process for the client */
|
||||
if ((pid = fork()) == -1) {
|
||||
perror("fork");
|
||||
write(client_sockid, "server error\n", 14);
|
||||
} else {
|
||||
if (pid == 0) {
|
||||
service(client_sockid);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (pthread_create(&clientsender, NULL, ClientSender, NULL) != 0) {
|
||||
fprintf(stderr, "pthread_create: error for ClientSender\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
/* Let the child handle the file descriptor that was just created */
|
||||
close(client_sockid);
|
||||
if (pthread_create(&clientlistener, NULL, ClientListener, NULL) != 0) {
|
||||
fprintf(stderr, "pthread_create: error for ClientSender\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
(void) pthread_join(clientsender, &retval);
|
||||
(void) pthread_join(clientlistener, &retval);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -89,10 +91,12 @@ int main(void) {
|
|||
}
|
||||
|
||||
/* Thread, parses and handles messages */
|
||||
void Dispatcher(void) {}
|
||||
void *Dispatcher(void *arg) {
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
/* Thread, listens for incoming messages */
|
||||
void ClientListener(int client_sockid) {
|
||||
void *ClientListener(void *arg) {
|
||||
char buf[BUF_LEN];
|
||||
int n = 0;
|
||||
|
||||
|
@ -106,10 +110,14 @@ void ClientListener(int client_sockid) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
/* Thread, sends messages to the client */
|
||||
void ClientSender(int message_pipe, int client_sockid) {}
|
||||
void *ClientSender(void *arg) {
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
/* Handles a client after its connection */
|
||||
// TODO: remove this function
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
#define LISTEN_ALL // define to listen on 0.0.0.0, else loopback
|
||||
|
||||
/* Thread, parses and handles messages */
|
||||
void Dispatcher(void);
|
||||
void *Dispatcher(void*);
|
||||
|
||||
/* Thread, listens for incoming messages */
|
||||
void ClientListener(int);
|
||||
void *ClientListener(void*);
|
||||
|
||||
/* Thread, sends messages to the client */
|
||||
void ClientSender(int, int);
|
||||
void *ClientSender(void*);
|
||||
|
||||
/* Handles a client after its connection */
|
||||
void service(int);
|
||||
|
|
Loading…
Reference in a new issue