1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
int main()
{
//mise en place socket
//début de la boucle infini
while(1)
{
evenement("attente d'une nouvelle connextion");
/*acceptation des clients et creation d'une nouvelle socket client */
socket_client=accept(sock,(struct sockaddr *)&adresse_client,&taille);
/*test d'erreur de la fonction newsock*/
if(socket_client<0)
{
perror("erreur de la fonction accept");
return 1;
}
s
//acceuil d'un nouveau client
/*un nouveau utilisateur viens de se connexter .on collecte ses information
* de connextion :adresse IP,son port,la nouvelle socket(newsock)*/
infos info_client;
pthread_mutex_lock(&mutex);
strcpy(info_client.IP,inet_ntoa (adresse_client.sin_addr));
info_client.socket=socket_client;
info_client.port=ntohs(adresse_client.sin_port);
/*preparation des thread clients*/
pthread_mutex_unlock(&mutex);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
pthread_attr_setscope(&attr,PTHREAD_SCOPE_SYSTEM);
/*lancement des thread proprement dit*/
if(pthread_create(&threadid,&attr,thread_user,(void *)&info_client)==0)
{
continue;
}
else
{
close(socket_client);
}
}
close(sock);
close(socket_client);
return 0;
}
//une partie du code de thread_user
void* thread_user (void *info_client)
{
int id_user;
char media[20];
infos *infoclient=(infos *)info_client;
/*on inscrit en local les information sur l'utilisateur
* son adresse ip,la socket de communication,le port */
infos info_local;
info_local.socket=infoclient->socket;
strcpy(info_local.IP,infoclient->IP);
info_local.port=infoclient->port;
//reste du code |
Partager