moved accept-loop to why2_accept_thread

This commit is contained in:
Václav Šmejkal 2023-02-21 19:34:40 +01:00
parent 89885de21e
commit 97e2d1d097
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
3 changed files with 22 additions and 13 deletions

View File

@ -23,5 +23,6 @@ void why2_send_socket(char *text, int socket); //send socket.... wtf did you exp
char *why2_read_socket(int socket); //read lol
void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD
void why2_register_connection(int socket); //ADD SOCKET TO LIST
void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS
#endif

View File

@ -23,7 +23,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
int main(void)
{
int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
int accepted;
pthread_t thread;
if (listen_socket < 0) why2_die("Failed creating socket.");
@ -42,15 +41,11 @@ int main(void)
printf("Server enabled.\n\n");
//LOOP ACCEPT
for (;;)
{
accepted = accept(listen_socket, (SA *) NULL, NULL); //ACCEPT NEW SOCKET //TODO: CLOSE (not only this one)
pthread_create(&thread, NULL, why2_accept_thread, NULL);
if (accepted == -1) continue;
//TODO: Add getline()
pthread_create(&thread, NULL, why2_communicate_thread, &accepted);
}
pthread_join(thread, NULL);
return 0;
}

View File

@ -18,13 +18,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/chat/misc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
#include <why2/chat/common.h>
#include <why2/memory.h>
//LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION)
@ -191,4 +188,20 @@ char *why2_read_socket(int socket)
void why2_register_connection(int socket)
{
push_to_list(socket); //SEND
}
void *why2_accept_thread(void *socket)
{
int accepted;
pthread_t thread;
//LOOP ACCEPT
for (;;)
{
accepted = accept(*((int*) socket), (SA *) NULL, NULL); //ACCEPT NEW SOCKET //TODO: CLOSE (not only this one)
if (accepted == -1) continue;
pthread_create(&thread, NULL, why2_communicate_thread, &accepted);
}
}