Advertisement
nouamanelachhab11111

Untitled

Dec 6th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include<signal.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. int main()
  10. {
  11. //structure donnant les informations sur le serveur et sur le client
  12. struct sockaddr_in information_server;
  13. struct sockaddr_in information_client;
  14.  
  15. int socketID = socket(AF_INET, SOCK_STREAM, 0);
  16. int connexion = 0;
  17. int pid;
  18. int id;
  19. char msg[255];
  20.  
  21. id=0;
  22. socklen_t len = sizeof(struct sockaddr_in); //déclaration d' une variable du type socklen_t qui contiendra la taille de la structure
  23.  
  24. if (socketID == -1)
  25. {
  26. perror("socket");
  27. exit(-1);
  28. }
  29.  
  30. /*initialisation du protocole, TCP l'adresse de connection 127.0.0.1 (en local) et du port du serveur (1400)*/
  31. memset(&information_server, 0, sizeof(struct sockaddr_in));
  32. information_server.sin_port = htons(2500);
  33. information_server.sin_family = AF_INET;
  34.  
  35. /* création de la connexion*/
  36. if ((bind(socketID, (struct sockaddr *) &information_server, sizeof(struct sockaddr))) == -1)
  37. {
  38. perror("bind");
  39. exit(-1);
  40. }
  41.  
  42. /* le serveur écoute si un client cherche à se connecter*/
  43. if ((listen(socketID, 5)) == -1)
  44. {
  45. perror("listen");
  46. exit (-1);
  47. }
  48. while (1)
  49. {
  50.  
  51. memset(&information_client, 0, sizeof(struct sockaddr_in));
  52. connexion = accept(socketID, (struct sockaddr *) &information_client, &len); //le serveur accepte la connexion
  53.  
  54. if (connexion == -1)
  55. {
  56. perror("accept");
  57. exit(-1);
  58. }
  59. id+=1;
  60. /* Create child process */
  61. pid = fork();
  62.  
  63. if (pid < 0)
  64. {
  65. perror("ERROR on fork");
  66. exit(1);
  67. }
  68. if (pid == 0)
  69. {
  70. /* This is the client process */
  71. close(socketID);
  72. printf ("Connexion acceptée de : client %i\n",id);
  73. memset(msg, 0, 255);
  74. sprintf(msg,"bienvenu! client %i",id);
  75. send(connexion, msg, strlen(msg), 0);
  76. do
  77. {
  78. memset(msg, 0, 255);
  79. recv(connexion, msg, 255, 0);
  80.  
  81. if (strcmp(msg, "aurevoir") == 0) //si le client ecrit aurevoir il est deconnecté du chat
  82. {
  83. printf ("Connexion fermée pour le client %i\n",id);
  84. shutdown(socketID, SHUT_RDWR);
  85. exit (0);
  86. }
  87.  
  88. printf ("client %d : %s\n",id,msg);
  89. printf ("Réponse : ");
  90. fgets(msg, 255, stdin);
  91. msg[strlen(msg) - 1] = '\0';
  92. send(connexion, msg, strlen(msg), 0);
  93.  
  94. }
  95. while(1);
  96. }
  97. else
  98. {
  99. close(connexion);
  100.  
  101. }
  102.  
  103. }
  104. return 0;
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement