|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Mar 2014
Messaggi: 6
|
[C] chat P2P - Linux
Ciao a tutti!
Ho un problema, devo creare una chat p2p. E mi sono bloccato. (per semplificare intanto ho creato due file, server e client). Allora, andrebbe anche bene da un certo punto di vista fino qui, ma no Allora questo è uno stralcio di codice del "server", fa le sue operazioni di bind e listen. Poi entra in un while() per fare l'accept e parte un thread per la ricezione. Il thread ha un while che mi continua a ricevere quello che scrive l'utente. Codice:
#server part
//creo socketfd (la socket del srv)
//Bind
//Listen
listen(serverfd , 5);
//Accept and incoming connection
c =sizeof(struct sockaddr_in) ;
pthread_t thread_id;
while( (clientfd = accept(serverfd, (struct sockaddr *)&client_addr, (socklen_t*)&c)) ){
puts("Connection accepted");
pthread_create( &thread_id , NULL , connection_handler , (void*) &clientfd);
}//fine while
//Se scrivo qui non me lo legge, rimane nel while
return 0;
} //fine main
// funzione thread
void *connection_handler(void *serverfd)
{
int sock = *(int*)serverfd;
char msg[MSG_SIZE];
int read_size;
while( (read_size=recv(sock, msg, MSG_SIZE * sizeof(char), 0)) > 0 ){
printf("%s\n", msg);
bzero(&msg, MSG_SIZE * sizeof(char));
}//fine while
pthread_exit(NULL); //non sono sicuro serva qui
return 0;
}
IL PROBLEMA: Cosi i client scrivono e il server legge e basta. Ora vorrei fare in modo che il server possa rispondere ai client meglio se in maniera asincrona. (ovviamente in caso poi faro un thread ricezione anche nel client.) (ho salvato in una struct pure le porte dei client nel caso mi potessero servire). Quindi ho pensato Codice:
int i;
for(i=0;i<NCLIENT;i++){
client_addr.sin_port=utente[i].porta;
send(serverfd, msg, size,0);
}
Grazie a tutti quelli che mi daranno una mano! |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Sep 2001
Città: Pisa
Messaggi: 2213
|
ma se è peer to peer perchè c'è un client ed un server?
__________________
9800X3D | 32GB DDR5 6400C30@TUNED | RTX 4090 | LG 32GQ950-B | Fractal Torrent | NZXT C1200W | Iliad Fibra 5Gb |
|
|
|
|
|
#3 | |
|
Junior Member
Iscritto dal: Mar 2014
Messaggi: 6
|
Quote:
Ma ora volevo capire come poter mandare lo stesso messaggio a due utenti differenti. |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:50.




















