Blame view

server.c 4.23 KB
b58103ff   achemin1   feat : now detect...
1
  #define _GNU_SOURCE
8d4694b6   achemin1   feat: server list...
2
3
4
5
6
7
8
9
10
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <stdlib.h>
  #include <stdio.h>
  #include <string.h>
  #include <netinet/in.h>
  #include <sys/un.h>
  #include <netdb.h>
  #include <netinet/tcp.h>
b82d369c   achemin1   feat: we can now ...
11
  #include <poll.h>
8d4694b6   achemin1   feat: server list...
12
  #include "server.h"
19fd5674   achemin1   feat: receive mes...
13
  #include <unistd.h>
8d4694b6   achemin1   feat: server list...
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
  
  #define MAX_LIGNE 512
  
  
  int initialisationServeur(char *service,int connexions){
  	struct addrinfo precisions;
  	struct addrinfo *resultat;
  	struct addrinfo *origine;
  	int statut;
  	int s;
  
  	/* Construction de la structure adresse */
  	memset(&precisions,0,sizeof precisions);
  	precisions.ai_family=AF_UNSPEC;
  	precisions.ai_socktype=SOCK_STREAM;
  	precisions.ai_flags=AI_PASSIVE;
  	statut=getaddrinfo(NULL,service,&precisions,&origine);
  	if(statut<0){ perror("initialisationServeur.getaddrinfo"); exit(EXIT_FAILURE); }
  	struct addrinfo *p;
  	for(p=origine,resultat=origine;p!=NULL;p=p->ai_next)
  	  if(p->ai_family==AF_INET6){ resultat=p; break; }
  
  	/* Creation d'une socket */
  	s=socket(resultat->ai_family,resultat->ai_socktype,resultat->ai_protocol);
  	if(s<0){ perror("initialisationServeur.socket"); exit(EXIT_FAILURE); }
  
  	/* Options utiles */
  	int vrai=1;
  	if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&vrai,sizeof(vrai))<0){
  	  perror("initialisationServeur.setsockopt (REUSEADDR)");
  	  exit(EXIT_FAILURE);
  	  }
  	if(setsockopt(s,IPPROTO_TCP,TCP_NODELAY,&vrai,sizeof(vrai))<0){
  	  perror("initialisationServeur.setsockopt (NODELAY)");
  	  exit(EXIT_FAILURE);
  	  }
  
  	/* Specification de l'adresse de la socket */
  	statut=bind(s,resultat->ai_addr,resultat->ai_addrlen);
  	if(statut<0) return -1;
  
  	/* Liberation de la structure d'informations */
  	freeaddrinfo(origine);
  
  	/* Taille de la queue d'attente */
  	statut=listen(s,connexions);
  	if(statut<0) return -1;
  
  	return s;
  
  }
  
  
  int boucleServeur(int ecoute,int (*traitement)(int))
  {
  	int dialogue;
  	while(1){
  
  	    /* Attente d'une connexion */
  	    if((dialogue=accept(ecoute,NULL,NULL))<0) return -1;
  
  	    /* Passage de la socket de dialogue a la fonction de traitement */
  	    if(traitement(dialogue)<0){ shutdown(ecoute,SHUT_RDWR); return 0;}
  
          }
  }
  
  
  int serv_printf(int s){
  	printf("Hello %i\n", s);
  	return 0;
  }
  
  
  int serv_gestionClient(int s){
  
  	/* Obtient une structure de fichier */
  	FILE *dialogue=fdopen(s,"a+");
  	if(dialogue==NULL){ perror("gestionClient.fdopen"); exit(EXIT_FAILURE); }
  
  	/* Echo */
  	char ligne[MAX_LIGNE];
  	while(fgets(ligne,MAX_LIGNE,dialogue)!=NULL){
  	  	#ifdef DEBUG
  			printf("> %s", ligne);
  		#endif
  		fprintf(dialogue,"> %s",ligne);
  	}
  
  	/* Termine la connexion */
  	fclose(dialogue);
  	return 0;
  }
  
b82d369c   achemin1   feat: we can now ...
108
  
b58103ff   achemin1   feat : now detect...
109
110
111
112
113
114
115
116
117
118
  void removeSocket(struct pollfd descr[], int i, int length){
  	for (int j=i;j<length-1;j++)
  	{
  		descr[j]=descr[j+1];	
  
  	}
  
  
  }
  
b82d369c   achemin1   feat: we can now ...
119
120
121
122
123
124
125
126
127
128
  
  int boucleServeur2(int ecoute)
  {
  	//initialisation tableau de socket
          //int dialogue;
  	struct pollfd descripteurs[1024];
  	descripteurs[0].fd=ecoute;
  	//initialiser POLLIN
  	for (int i=0;i<1024;i++)
  	{
b58103ff   achemin1   feat : now detect...
129
  		descripteurs[i].events=POLLIN|POLLRDHUP|POLLERR;
b82d369c   achemin1   feat: we can now ...
130
131
132
133
134
  	}
  	int LongueurPoll = 1;
  
          while(1){
  
b58103ff   achemin1   feat : now detect...
135
  
b82d369c   achemin1   feat: we can now ...
136
  		//poll
b58103ff   achemin1   feat : now detect...
137
  		int nb = poll(descripteurs, LongueurPoll, -1);
19fd5674   achemin1   feat: receive mes...
138
139
  
  		if(nb<0){ perror("main.poll"); exit(EXIT_FAILURE); }
b82d369c   achemin1   feat: we can now ...
140
  
19fd5674   achemin1   feat: receive mes...
141
  		if( (descripteurs[0].revents&POLLIN) !=0){//ecoute est active
b82d369c   achemin1   feat: we can now ...
142
143
144
145
146
147
  		        /* Attente d'une connexion */
  		        int dialogue=accept(ecoute,NULL,NULL);
  			if(dialogue<0) return -1;
  
  			//if longueurPoll <1024 //TODO A GERER
  			descripteurs[LongueurPoll].fd = dialogue;
629c100c   achemin1   feat: clients...
148
  			descripteurs[LongueurPoll].revents=0;
b82d369c   achemin1   feat: we can now ...
149
150
151
152
153
  			LongueurPoll ++;
  			printf("Nouveau LongueurPoll : %i\n", LongueurPoll);
  
  		        /* Passage de la socket de dialogue a la fonction de traitement */
  		        //if(traitement(dialogue)<0){ shutdown(ecoute,SHUT_RDWR); return 0;}
19fd5674   achemin1   feat: receive mes...
154
155
156
  		}
  
  		for(int i = 0; i < LongueurPoll-1; i++){
b58103ff   achemin1   feat : now detect...
157
158
159
160
161
162
163
164
  			if( (descripteurs[i+1].revents&POLLRDHUP) !=0){
  				printf("|%i$ Deconnection\n", i+1);
  
  				//FONCTION ENLEVER UN SOCKET
  				removeSocket(descripteurs,i+1,LongueurPoll );
  				LongueurPoll --;
  			}
  			else if( (descripteurs[i+1].revents&POLLIN) !=0){ //A lire
19fd5674   achemin1   feat: receive mes...
165
  				//printf("Gestion Client %i \n", i+1);
b58103ff   achemin1   feat : now detect...
166
  
19fd5674   achemin1   feat: receive mes...
167
168
169
170
171
  				FILE* f = fdopen(descripteurs[i+1].fd, "a+");
  
  				char ligne[MAX_LIGNE];
  				fgets(ligne,MAX_LIGNE,f);
  				printf(">%i$ %s\n", i+1, ligne);
19fd5674   achemin1   feat: receive mes...
172
  
62b4d247   achemin1   add : pong from t...
173
174
175
176
177
  				//renvoyer à tous les clients
  				for(int j = 0 ; j < LongueurPoll-1 ; j++){
  					if(j != i)
  						dprintf(descripteurs[j+1].fd, ":%s\n", ligne);
  				}
19fd5674   achemin1   feat: receive mes...
178
179
180
  			}
  		}
  
b82d369c   achemin1   feat: we can now ...
181
          }
19fd5674   achemin1   feat: receive mes...
182
  
b82d369c   achemin1   feat: we can now ...
183
  }