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
|
/* -ed- compatibilité Windows, organisation, mise en forme */
/* server.c */
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
#if defined (WIN32)
#include <winsock2.h>
#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>
#include <unistd.h> /* close */
#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>
/* macros ============================================================== */
#define PORT 80
#define BACKLOG 5
#define MAXDATASIZE 2048
#define NB_THREADS 5
/* constants =========================================================== */
/* types =============================================================== */
/* structures ========================================================== */
/* private data ======================================================== */
/* private functions =================================================== */
char buff[MAXDATASIZE];
int i;
int sockfd, newsockfd;
int pid, sin_size, nbytes;
struct sockaddr_in serveraddr; /* l'adreese de la socket serveur */
struct sockaddr_in clientaddr; /* l'adresse de la socket client */
struct hostent *hostinfo;
char *a;
int count;
int x = 0, nthreads = 0;
pthread_t id[NB_THREADS];
static void *traitement(void*p_data)
{
(void)p_data;
return NULL;
}
void init_socket (void)
{
/*******CREATION DE SOCKET*******/
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
printf ("création de socket impossible\n");
exit (1);
}
printf ("socket créé\n");
/*******REMPLISSAGE DE LA SOCKET******/
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons (PORT);
serveraddr.sin_addr.s_addr = INADDR_ANY; /* remplissage automatique */
/********ATTACHEMANT DE LA SOCKET AU N°PORT********/
if (bind (sockfd, (struct sockaddr *) &serveraddr,
sizeof (struct sockaddr_in)) == 1)
{
exit (EXIT_FAILURE);
}
printf ("socket connecté au réseau\n");
/*******ECOUTE SUR LE RESEAU********/
if (listen (sockfd, BACKLOG) == -1)
{
exit (EXIT_FAILURE);
}
}
int connexion (void)
{
/* attente permanente */
while (1)
{
sin_size = sizeof (struct sockaddr);
if ((newsockfd = accept (sockfd,
(struct sockaddr *) &clientaddr,
&sin_size)) == -1)
{
exit (EXIT_FAILURE);
}
printf ("client connecté\n");
/*************/
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = 80;
memcpy ((struct sockaddr *) &clientaddr.sin_addr.s_addr,
hostinfo->h_addr, 4);
a = inet_ntoa (clientaddr.sin_addr);
printf (" adresse du client est: %s\n", a);
/********CREATION DES THREADS*******/
if (pthread_create (&id[x++], NULL, traitement, NULL) != 0)
{
printf ("création du thread impossible\n");
}
else
{
if (nthreads++ >= NB_THREADS)
break;
}
}
for (x = 0; x < nthreads; x++)
{
if (pthread_join (id[x], NULL) < 0)
{
printf ("erreur de join\n");
}
closesocket (newsockfd);
}
closesocket (sockfd);
return 0;
}
static int server (void)
{
init_socket ();
connexion ();
return 0;
}
/* entry point ========================================================= */
/* ---------------------------------------------------------------------
--------------------------------------------------------------------- */
int main (void)
{
int ret;
#if defined (WIN32)
WSADATA wsa_data;
int err = WSAStartup (MAKEWORD (2, 2), &wsa_data);
if (!err)
{
puts ("WIN: winsock2: OK");
#else
int err;
#endif
server ();
#if defined (WIN32)
WSACleanup ();
}
#endif
if (err)
{
ret = EXIT_FAILURE;
}
else
{
ret = EXIT_SUCCESS;
}
return ret;
} |
Partager