32c9271a
skhinach
modif
|
1
2
|
#include <stdlib.h>
#include <stdio.h>
|
fff6f037
skhinach
modifs
|
3
|
#include <unistd.h>
|
32c9271a
skhinach
modif
|
4
5
|
#include <getopt.h>
#include <string.h>
|
fff6f037
skhinach
modifs
|
6
|
#include <signal.h>
|
32c9271a
skhinach
modif
|
7
|
#include "libnet.h"
|
a69a94a7
skhinach
Ajout de gestion ...
|
8
|
#include <time.h>
|
32c9271a
skhinach
modif
|
9
|
|
fff6f037
skhinach
modifs
|
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
int sock_fd;
struct sigaction action;
void handler(int sig)
{
if(sig==SIGINT)
{
printf("\nINTERRUPTION SOCKET : %d\n\n", sock_fd);
close(sock_fd);
exit(1);
}
}
|
32c9271a
skhinach
modif
|
25
26
|
void argPortParsing(int argc, char* argv[], char* port)
{
|
a69a94a7
skhinach
Ajout de gestion ...
|
27
|
struct option port_arg={"port", 1, NULL, 'p'}; //
|
32c9271a
skhinach
modif
|
28
29
30
31
32
33
34
|
char opt;
int longindex;
while( (opt=getopt_long(argc, argv, "p:", &port_arg, &longindex)) !='p' && opt!=-1) {}
if(opt=='p')
{
|
a69a94a7
skhinach
Ajout de gestion ...
|
35
|
strcpy(port, optarg); //optarg est une variable instanciée dans la librairie getopt
|
32c9271a
skhinach
modif
|
36
37
38
39
40
41
42
43
44
45
46
|
printf("%s\n", optarg);
}
else
{
printf("La syntaxe doit être de la forme %s -p <port> ou %s --port <port>\n\n", argv[0], argv[0]);
}
}
int main(int argc, char* argv[])
{
|
a69a94a7
skhinach
Ajout de gestion ...
|
47
|
|
32c9271a
skhinach
modif
|
48
49
50
|
char port[10]="80";
argPortParsing(argc, argv, port);
|
fff6f037
skhinach
modifs
|
51
52
|
action.sa_handler=&handler;
sigaction(SIGINT, &action, NULL);
|
32c9271a
skhinach
modif
|
53
54
55
56
|
if( (sock_fd=initialisationServeur(port)) ==-1 ) { fprintf(stderr, "Initialisation du serveur impossible\n"); return -1; }
boucleServeur(sock_fd, (void*)&reponseConnexion);
|
32c9271a
skhinach
modif
|
57
|
|
32c9271a
skhinach
modif
|
58
59
|
return 0;
}
|