Skip to content

Commit

Permalink
funziona un po' di più
Browse files Browse the repository at this point in the history
  • Loading branch information
NdA994 committed Mar 29, 2020
1 parent 5f7c006 commit 6a41045
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void client_handler() {
if(strcmp(string, "quit")==0){
printf("QUIT SIIIIII\n");
stop = 1;
break;
}
printf("Message sent\n");
}
Expand Down
35 changes: 33 additions & 2 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,28 @@ void send_to_all_clients(ClientList *np, char tmp_buffer[]) {
}

void client_handler(void *p_client) {
ClientList *np = (ClientList *)p_client;

while(1){
char buffer[1024] = {0};
ClientList *np = (ClientList *)p_client;
send(np->data , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
int valread = read(np->data , buffer, 1024);
printf("%s\n",buffer );
send_to_all_clients(np, buffer);

if(strcmp(buffer, "quit")==0){
break;
}
}
close(np->data);
if (np == now) { // remove an edge node
now = np->prev;
now->link = NULL;
} else { // remove a middle node
np->prev->link = np->link;
np->link->prev = np->prev;
}
free(np);
}

int main(int argc, char const *argv[]) {
Expand Down Expand Up @@ -231,6 +243,7 @@ int main(int argc, char const *argv[]) {


while(1){
/*
int new_socket;
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
perror("accept");
Expand All @@ -248,6 +261,24 @@ int main(int argc, char const *argv[]) {
perror("Create pthread error!\n");
exit(EXIT_FAILURE);
}
*/
int new_socket;
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
perror("accept");
exit(EXIT_FAILURE);
}

ClientList *c = newNode(new_socket);
c->data = new_socket;
c->prev = now;
now->link = c;
now = c;

pthread_t id;
if (pthread_create(&id, NULL, (void *)client_handler, (void *)c) != 0) {
perror("Create pthread error!\n");
exit(EXIT_FAILURE);
}

}

Expand Down

0 comments on commit 6a41045

Please sign in to comment.