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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
|
/* 01.c */
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
#if defined (WIN32) || defined (_WIN32)
#include <winsock2.h>
typedef size_t socklen_t;
#elif defined (linux) || defined (_POSIX_VERSION) || defined (_POSIX2_C_VERSION)\
|| defined (_XOPEN_VERSION)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* close */
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close (s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#else
#error not defined for this platform
#endif
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "psleep/inc/psleep.h"
/* macros ============================================================== */
#define PORT 23
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private data ======================================================== */
/* private functions =================================================== */
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
#define BUFSIZE 1024
#define BIENVENUE "Connexion etablie avec le serveur\n"
struct cli
{
/* structure d'adresse qui contiendra les param reseaux de l'expediteur */
struct sockaddr_in sin;
SOCKET sock;
pthread_t thread;
struct cli *list;
size_t size;
};
static char *lire_client (int sock)
{
char *msg = malloc (BUFSIZE);
int nb_lu = recv (sock, msg, BUFSIZE - 1, 0);
if (nb_lu > 0)
{
msg[nb_lu] = 0;
printf ("SRV : Message: '%s'", msg);
fflush (stdout);
}
else
{
free (msg), msg = NULL;
}
return msg;
}
static void *serv_client (void *user)
{
if (user != NULL)
{
struct cli *p_cli = user;
printf ("SRV : Connexion d'un client depuis %s \n",
inet_ntoa (p_cli->sin.sin_addr));
/* Souhaiter la bienvenue au client */
send (p_cli->sock, BIENVENUE, strlen (BIENVENUE), 0);
/* Traiter le client */
while (p_cli->sock != INVALID_SOCKET)
{
char *message = lire_client (p_cli->sock);
if (message == NULL)
{
closesocket (p_cli->sock), p_cli->sock = INVALID_SOCKET;
fprintf (stderr, "SRV : -- perte d'un client ! --\n");
}
else
{
/* Envoyer le message a tous les clients */
size_t i;
for (i = 0; i < p_cli->size; i++)
{
SOCKET c = p_cli->list[i].sock;
if (c != INVALID_SOCKET)
{
send (c, message, strlen (message), 0);
free (message), message = NULL;
}
}
}
}
}
return NULL;
}
static void cli_clear (struct cli a[], size_t n)
{
size_t i;
for (i = 0; i < n; i++)
{
struct cli *p_cli = a + i;
{
static struct cli z = { 0 };
*p_cli = z;
}
p_cli->list = a;
p_cli->size = n;
p_cli->sock = INVALID_SOCKET;
}
}
struct cli *get_cli (struct cli a[], size_t n)
{
struct cli *p = NULL;
size_t i;
for (i = 0; p == NULL && i < n; i++)
{
struct cli *p_cli = a + i;
if (p_cli->sock == INVALID_SOCKET)
{
p = p_cli;
}
}
return p;
}
static void *server (void *user)
{
/* creation de la socket */
SOCKET serv_sock = socket (AF_INET, SOCK_STREAM, 0);
/* initialisation de la structure d'adresse du serveur (pg local) */
/* structure d'adresse qui contiendra les param reseaux du recepteur */
struct sockaddr_in serv_sin;
int port = PORT;
/* taille d'une structure sockaddr_in utile pour la fonction accept */
socklen_t sin_size = sizeof (struct sockaddr_in);
/* famille d'adresse */
serv_sin.sin_family = AF_INET;
/* recuperation du port du serveur */
serv_sin.sin_port = htons (port);
/* automatic IP address */
serv_sin.sin_addr.s_addr = htonl (INADDR_ANY);
/* association de la socket et des param reseaux du serveur */
if (bind (serv_sock, (struct sockaddr *) &serv_sin, sin_size) ==
SOCKET_ERROR)
{
perror ("SRV : Erreur lors de l'appel a bind -> ");
exit (EXIT_FAILURE);
}
/* indication de la limite MAX de la file d'attente des connexions entrantes */
if (listen (serv_sock, 5) != 0)
{
perror ("SRV : Erreur lors de l'appel a listen -> ");
exit (EXIT_FAILURE);
}
printf ("SRV : [OK]: Serveur en attente sur le port %d\n", port);
/* Initialisation des listes de sockets */
printf ("SRV : Initialisation complete\n");
/* Gestion des clients */
{
int end = 0;
#define NB_CLI 10
struct cli a_cli[NB_CLI];
cli_clear (a_cli, NB_CLI);
while (!end)
{
struct cli *p_cli = get_cli (a_cli, NB_CLI);
if (p_cli == NULL)
{
fprintf (stderr, "SRV : trop de clients\n");
exit (EXIT_FAILURE);
}
p_cli->sock =
accept (serv_sock, (struct sockaddr *) &p_cli->sin, &sin_size);
if (p_cli->sock == INVALID_SOCKET)
{
fprintf (stderr, "SRV : accept: connexion impossible\n");
}
pthread_create (&p_cli->thread, NULL, serv_client, p_cli);
}
}
(void) user;
return NULL;
}
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
#define IP "127.0.0.1"
static void fclean (char *line, FILE * fp)
{
char *p = strchr (line, '\n');
if (p != NULL)
{
*p = 0;
}
else
{
int c;
while ((c = fgetc (fp)) != '\n' && c != EOF)
{
}
}
}
struct data
{
SOCKET sock;
};
static void *cli_receive (void *user)
{
if (user != NULL)
{
struct data *p_data = user;
while (p_data->sock != INVALID_SOCKET)
{
char buf[1024];
int n = recv (p_data->sock, buf, sizeof buf - 1, 0);
if (n > 0)
{
buf[n] = 0;
printf (buf);
}
else
{
fprintf (stderr,
"CLI : read: lecture impossible sur la socket \n");
closesocket (p_data->sock), p_data->sock = INVALID_SOCKET;
}
}
}
return NULL;
}
static void *client (void *user)
{
/* verification du nombre d'arguments sur la ligne de commande */
/* creation de la socket */
struct data data;
data.sock = socket (PF_INET, SOCK_STREAM, 0);
/* initialisation de la structure d'adresse du serveur : */
/* structure d'adresse qui contiendra les param reseaux du serveur */
struct sockaddr_in serveur;
/* famille d'adresse */
serveur.sin_family = AF_INET;
serveur.sin_addr.s_addr = inet_addr (IP);
/* recuperation du port du serveur */
serveur.sin_port = htons(PORT);
printf ("CLI : Tentative de connexion\n");
/* tentative de connexion */
if (connect (data.sock, (struct sockaddr *) &serveur, sizeof (serveur)) ==
SOCKET_ERROR)
{
fprintf (stderr, "CLI : Erreur de connexion\n");
}
else
/* il s'agit de communiquer avec le serveur. */
{
int end = 0;
pthread_t thread_receive;
pthread_create (&thread_receive, NULL, cli_receive, &data);
while (!end)
{
char buf[1024];
fgets (buf, sizeof buf, stdin);
/* fclean (buf, stdin);*/
if (strcmp (buf, "quit\n") != 0)
{
/* Emission vers le serveur */
if (send (data.sock, buf, strlen (buf), 0) == 0)
{
fprintf (stderr, "CLI : socket write error \n");
exit (EXIT_FAILURE);
}
}
else
{
end = 1;
}
}
}
closesocket (data.sock);
puts ("client off");
(void) user;
return NULL;
}
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
static void app (void)
{
pthread_t t_server;
pthread_t t_client;
pthread_create (&t_server, NULL, server, NULL);
msleep (1000);
pthread_create (&t_client, NULL, client, NULL);
pthread_join (t_server, NULL);
pthread_join (t_client, NULL);
}
/* entry point ========================================================= */
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
int main (void)
{
int ret;
#if defined (WIN32) || defined (_WIN32)
WSADATA wsa_data;
int err = WSAStartup (MAKEWORD (2, 2), &wsa_data);
if (!err)
{
puts ("WIN: winsock2: OK");
#else
int err;
#endif
app ();
#if defined (WIN32) || defined (_WIN32)
WSACleanup ();
}
#endif
if (err)
{
ret = EXIT_FAILURE;
}
else
{
ret = EXIT_SUCCESS;
}
return ret;
} |
Partager