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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <mqueue.h>
#include "main.h"
#define NB_CLIENT_MAX 5
#define TAILLE_MESSAGE 256
#define MYPORT "8001"
int clients_adresses[NB_CLIENT_MAX];/*Permet de stocker les clients actuellement connectes*/
pthread_t clients_threads_data[NB_CLIENT_MAX];/*Permet de stocker les threads Data*/
pthread_t clients_threads_maj_jdb[NB_CLIENT_MAX];/*Permet de stocker les threads maj jdb*/
/* Routine des Threads */
static void *serveurMajJdbThread (void *);
static void *serveurDataThread (void *);
void Erreur(const char *msg)
{
perror(msg);
}
void AjouterClient(int adresse)
{
int ajoute = 0;
int i = 0;
for(i = 0; i<NB_CLIENT_MAX; i++)
{
if(clients_adresses[i] == 0)
{/*Si ya une place dispo on ajoute le nouveau client*/
clients_adresses[i] = adresse;
pthread_t serveur_maj_jdb, serveur_data;
pthread_create (&serveur_maj_jdb, NULL, &serveurMajJdbThread, (void *) i);
pthread_create (&serveur_data, NULL, &serveurDataThread, (void *) i);
clients_threads_data[i] = serveur_data;
clients_threads_maj_jdb[i] = serveur_maj_jdb;
envoyerMessage(adresse, "connexion");
ajoute = 1;
}
}
if(!ajoute)
{
Erreur("Plus de place disponible pour toi Jack");
}
}
void CreationThreadsServeurs(int adresse)
{
/*Ajoute le client a la liste*/
AjouterClient(adresse);
}
static void threadDataDispatcher (char* buffer, int newsockfd);
static void executerThreadData(long index_client)
{
int newsockfd,n;
char buffer[256];
newsockfd = clients_adresses[index_client];
bzero(buffer,TAILLE_MESSAGE);
/*ecoute des messages du client*/
while((n = read(newsockfd,buffer,TAILLE_MESSAGE)))
{
if (n < 0)
{
Erreur("ERROR reading from socket");
}else{
printf("Message reçu: %s\n",buffer);
threadDataDispatcher(buffer, newsockfd);//On appelle la fonction qui s'occupe de déchiffrer ça
}
}
}
// --- MAIN --- //
void *ServeurConnexionThread (void * args)
{
printf("Demarrage du serveur en cours \n");
int i =0;
int sockfd, newsockfd,n;
int port_numero = NB_PORT_SOCKET_A;
socklen_t client_taille;
char buffer[256];
struct sockaddr_in serv_adresse, cli_addr;
/*Initialisation des clients */
for(i=0; i<NB_CLIENT_MAX; i++)
{
clients_adresses[i] = 0;
}
/*Configuration du serveur*/
int errcode;
socklen_t addr_size;
int* new_fd;
void*buf;
int taskid;
sleep(1);
struct addrinfo hints, *res, *p;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // fill in my IP for me
getaddrinfo(NULL, MYPORT, &hints, &res);
// make a socket:
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
// bind it to the port we passed in to getaddrinfo():
bind(sockfd, res->ai_addr, res->ai_addrlen);
// now accept an incoming connection:
listen(sockfd,NB_CLIENT_MAX);
client_taille = sizeof(cli_addr);
printf("Serveur : Attente de clients en cours \n");
/*Attente d'un nouveau client*/
while(newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&client_taille))
{
printf("Nouveau client détecté");
/*Nouveau client détecté*/
if (newsockfd < 0)
{
Erreur("ERROR on accept");
}else{
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0)
{
Erreur("ERROR reading from socket");
}else{
CreationThreadsServeurs(newsockfd);/*On a bien recu le message du client alors on cree le client*/
}
}
}
/*Fermeture du serveur*/
close(sockfd);
} |
Partager