Start converting server to threading

This commit is contained in:
flyingscorpio@clevo 2022-10-21 09:43:04 +02:00
parent e348410a3f
commit 0a1738cc94
2 changed files with 27 additions and 0 deletions

View file

@ -63,6 +63,7 @@ int main(void) {
} }
/* Loop to accept incoming connections */ /* Loop to accept incoming connections */
// TODO: make threads instead of fork()
while (RUN) { while (RUN) {
if ((child_sockid = accept(serv_sockid, (struct sockaddr *) &child_socket, (socklen_t *) &addr_len)) < 0) { if ((child_sockid = accept(serv_sockid, (struct sockaddr *) &child_socket, (socklen_t *) &addr_len)) < 0) {
perror("accept"); perror("accept");
@ -87,6 +88,26 @@ int main(void) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
/* Thread, listens for incoming messages */
void ClientListener(int sockid) {
char buf[BUF_LEN];
int n = 0;
while (1) {
/* Read n bytes at a time, because we don't know how long the message will be */
while ((n = read(sockid, buf, BUF_LEN)) > 0) {
// TODO: do somethig with the stream (push to global stack?)
}
if (n < 0) {
fprintf(stderr, "ERROR: read\n");
break;
}
}
}
/* Thread, sends messages to the client */
void ClientSender(int sockid) {}
/* Handles a client after its connection */ /* Handles a client after its connection */
void service(int sockid) { void service(int sockid) {
int i, n, t; int i, n, t;

View file

@ -3,6 +3,12 @@
#define LISTEN_ALL // define to listen on 0.0.0.0, else loopback #define LISTEN_ALL // define to listen on 0.0.0.0, else loopback
/* Thread, listens for incoming messages */
void ClientListener(int);
/* Thread, sends messages to the client */
void ClientSender(int);
/* Handles a client after its connection */ /* Handles a client after its connection */
void service(int); void service(int);