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 char *why2_read_socket(int socket); //read lol
void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD void *why2_communicate_thread(void *arg); //COMMUNICATION THREAD
void why2_register_connection(int socket); //ADD SOCKET TO LIST void why2_register_connection(int socket); //ADD SOCKET TO LIST
void *why2_accept_thread(void *socket); //LOOP ACCEPTING CONNECTIONS
#endif #endif

View File

@ -23,7 +23,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
int main(void) int main(void)
{ {
int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET int listen_socket = socket(AF_INET, SOCK_STREAM, 0); //CREATE SERVER SOCKET
int accepted;
pthread_t thread; pthread_t thread;
if (listen_socket < 0) why2_die("Failed creating socket."); if (listen_socket < 0) why2_die("Failed creating socket.");
@ -42,15 +41,11 @@ int main(void)
printf("Server enabled.\n\n"); printf("Server enabled.\n\n");
//LOOP ACCEPT pthread_create(&thread, NULL, why2_accept_thread, NULL);
for (;;)
{
accepted = accept(listen_socket, (SA *) NULL, NULL); //ACCEPT NEW SOCKET //TODO: CLOSE (not only this one)
if (accepted == -1) continue; //TODO: Add getline()
pthread_create(&thread, NULL, why2_communicate_thread, &accepted); pthread_join(thread, NULL);
}
return 0; 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 <why2/chat/misc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <why2/chat/common.h>
#include <why2/memory.h> #include <why2/memory.h>
//LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION) //LINKED LIST STUFF (BIT CHANGED memory.c'S VERSION)
@ -192,3 +189,19 @@ void why2_register_connection(int socket)
{ {
push_to_list(socket); //SEND 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);
}
}