Blame view

UDP/Serveur/main.c 2.66 KB
0ae69087   pfrison   Ajout des fichiers
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
  #include <stdio.h>
  #include <stdlib.h> // pour exit()
  #include <string.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <netinet/ip.h>
  #include <netinet/udp.h>
  #include <netdb.h>
  
  #define BUFFER_SIZE 1000
  
  // Creation d'un serveur UDP
  // service est le numéro ou nom de port (ex: "80" ou "http")
  int initialisationServeurUDP(char *service){
      struct addrinfo precisions, *resultat, *origine;
      int statut;
      int s;
  
      /* Construction de la structure adresse */
      memset(&precisions, 0, sizeof precisions);
      precisions.ai_family = AF_UNSPEC;
      precisions.ai_socktype = SOCK_DGRAM;
      precisions.ai_flags = AI_PASSIVE;
      statut = getaddrinfo(NULL, service, &precisions, &origine);
      if(statut < 0){ perror("initialisationSocketUDP.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("initialisationSocketUDP.socket"); exit(EXIT_FAILURE); }
  
      /* Options utiles */
      int vrai = 1;
      if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &vrai, sizeof(vrai)) < 0){
          perror("initialisationServeurUDPgenerique.setsockopt (REUSEADDR)");
          exit(-1);
      }
  
      /* Specification de l'adresse de la socket */
      statut = bind(s, resultat->ai_addr, resultat->ai_addrlen);
      if(statut < 0) { perror("initialisationServeurUDP.bind"); exit(-1); }
  
      /* Liberation de la structure d'informations */
      freeaddrinfo(origine);
  
      return s;
  }
  
  // Reception des messages UDP et execute la fonction passee en argument
  // ATTENTION : non multi-threadé ! -> il faut un pthread_create dans traitement pour le multi-thread.
  int boucleServeurUDP(int s, void (*traitement)(unsigned char *, int)){
      while(1){
          struct sockaddr_storage adresse;
          struct sockaddr *padresse = (struct sockaddr *) &adresse;
          socklen_t taille = sizeof(adresse);
          unsigned char message[BUFFER_SIZE];
  
          int nboctets = recvfrom(s, message, BUFFER_SIZE, 0, (struct sockaddr *) padresse, &taille);
          if(nboctets < 0) return -1;
  
          message[nboctets] = '\0';
  
          traitement(message, nboctets);
      }
      return 0;
  }
  
  void exemple_traitement(unsigned char *message, int length){
      (void) length; // on utilise pas length ici
  
      printf("Message recu :\n\n%s\n", message);
  }
  
  int main(void){
      // init serveur
      char service[50] = "2030";
      int socket = initialisationServeurUDP(service);
  
      // lancement boucle infinie
      boucleServeurUDP(socket, exemple_traitement);
  
      return 0;
  }