Guest User

Untitled

a guest
May 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. //+++++++++
  2. //serveur.c
  3. //+++++++++
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <netdb.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #define p 8000
  14.  
  15.  
  16. struct spectacle
  17. {
  18. int num_spect;
  19. char *nom_spect;
  20. int date_spect;
  21. int nb_place;
  22. int tarif;
  23. };
  24. typedef struct spectacle spectacle;
  25.  
  26.  
  27. struct reservation
  28. {
  29. int num_res;
  30. int num_sp;
  31. int nb_p;
  32. };
  33. typedef struct reservation reservation;
  34.  
  35. spectacle t_spectacle[100];
  36.  
  37.  
  38. int nom_client(int sock);
  39.  
  40. int nom_client(int sock)
  41.  
  42. {
  43. struct sockaddr_in client;
  44. struct hostent* clt;
  45. int longueur = sizeof(client);
  46. if(getpeername(sock, (struct sockaddr*)&client, &longueur)<0)
  47. return(-1);
  48.  
  49. longueur = sizeof(client.sin_addr);
  50.  
  51. if((clt = gethostbyaddr((char*)&client.sin_addr, longueur, AF_INET)) == NULL)
  52. return(-1);
  53.  
  54. // noths : convertit un entier court dans l'ordre des octets réseau
  55. // inet_ntoa convertit une adresse internet dans l'ordre des octets
  56. // réseau en une chaîne de caractères.
  57.  
  58. printf("Client de nom %s et l'adresse %d sur socket %d\n", clt->h_name, inet_ntoa(client.sin_addr), ntohs(client.sin_port));
  59. }
  60.  
  61. int main()
  62. {
  63. char inscris;
  64. int sock, sock_com;
  65. struct sockaddr_in adresse;
  66. char *message[1024];
  67. u_short port = p;
  68. int options = -1;
  69.  
  70. //+++++++++++++++++++++++
  71. // Ouverture d'une socket
  72. //+++++++++++++++++++++++
  73.  
  74. sock=socket(AF_INET, SOCK_STREAM, 0);
  75.  
  76. if(sock<0)
  77. {
  78. perror("Erreur d'ouverture de la socket");
  79. return(-1);
  80. }
  81.  
  82.  
  83. //+++++++++++++++++++++++++++++++++++++++++++++++++
  84. // Constituer l'adresse conformément à la structure
  85. //+++++++++++++++++++++++++++++++++++++++++++++++++
  86.  
  87. adresse.sin_family = AF_INET;
  88. adresse.sin_port = htons((u_short)port);
  89. adresse.sin_addr.s_addr = INADDR_ANY;
  90.  
  91.  
  92. //+++++++++++++++++++++++++++++++++++++++++++++++++++
  93. // Permettre la réuitilisation d'une socket existante
  94. //+++++++++++++++++++++++++++++++++++++++++++++++++++
  95.  
  96. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(int));
  97.  
  98.  
  99. //++++++++++++++++++++++++++++++++++++++++++++++++++++
  100. // Phase d'association de la socket à l'adresse locale
  101. //++++++++++++++++++++++++++++++++++++++++++++++++++++
  102.  
  103. if(bind(sock, (struct sockaddr*)&adresse, sizeof(adresse))<0)
  104. {
  105. perror("Erreur d'association");
  106. return(-1);
  107. }
  108.  
  109.  
  110. //++++++++++++++++++++++++++++++++
  111. // Attente de la connexions client
  112. //++++++++++++++++++++++++++++++++
  113.  
  114. listen(sock, 5);
  115.  
  116.  
  117. //++++++++++++++++++++++++++++
  118. // Acceptation de la connexion
  119. //++++++++++++++++++++++++++++
  120.  
  121. sock_com=accept(sock, (struct sockaddr*)0, (int*)0);
  122.  
  123. if(sock_com == -1)
  124. {
  125. perror("ERREUR ACCEPT");
  126. return(-1);
  127. }
  128. else
  129. perror("Bravo: connexion établie !");
  130.  
  131. nom_client(sock_com);
  132.  
  133.  
  134. //++++++++++++++++
  135. // Phase d'échange
  136. //++++++++++++++++
  137. /*
  138. if(read(sock_com, message, 1024)==-1)
  139. {
  140. perror("Il y a un probleme dans la lecture de la socket\n");
  141. }
  142. */
  143. FILE *fichier = NULL;
  144.  
  145. //+++++++++++++++++++++++++++++++++++++
  146. // Lecture du fichier spectacle.txt
  147. //+++++++++++++++++++++++++++++++++++++
  148.  
  149.  
  150. fichier = fopen ("spectacle.txt", "r+");
  151.  
  152. if (fichier == NULL)
  153. {
  154. perror("Erreur lors de l'ouverture du fichier !");
  155. }
  156.  
  157.  
  158. int i=-1;
  159. int num, date, places, prix;
  160. char nom, crew;
  161.  
  162. while (fgetc(fichier) != EOF)
  163. {
  164. i++;
  165. fscanf(fichier,"%d %s %d %d %d", &num,&nom,&date,&places,&prix);
  166. t_spectacle[i].num_spect=num;
  167. t_spectacle[i].nom_spect=&nom;
  168. t_spectacle[i].date_spect=date;
  169. t_spectacle[i].nb_place=places;
  170. t_spectacle[i].tarif=prix;
  171. }
  172.  
  173. while(i>=0)
  174. {
  175. printf("%d,%s,%d,%d,%d\n", t_spectacle[i].num_spect,t_spectacle[i].nom_spect,t_spectacle[i].date_spect,t_spectacle[i].nb_place,t_spectacle[i].tarif);
  176. i--;
  177. }
  178.  
  179. read(sock_com, inscris, sizeof (inscris));
  180.  
  181. if (inscris=='o')
  182. {
  183. message[1024]="Loguez vous !";
  184. }
  185. else
  186. {
  187. message[1024]="Inscrivez vous !";
  188. }
  189.  
  190. if(write(sock, message, strlen(message)) == -1)
  191. {
  192. perror("Erreur d'émission ou d'écriture dans la socket");
  193. return(-1);
  194. }
  195.  
  196. /*else
  197. {
  198. fprintf(stderr, "Message reçu : %s\n", message);
  199. fprintf(stderr, "Message à envoyer : \n");
  200. scanf("%s", message);char testcar,mot;
  201.  
  202. if(write(sock_com, message, strlen(message))==-1)
  203. {
  204. perror("Il y a un problème dans l'envoi du message\n");
  205. return(-1);
  206. }
  207. else
  208. {
  209. fprintf(stderr, "\nLe message %s est correctement envoyé\n", message);
  210. }
  211.  
  212. close(sock_com);
  213. }*/
  214.  
  215. close(sock);
  216. }
Add Comment
Please, Sign In to add comment