Advertisement
nouamanelachhab11111

Untitled

Dec 6th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include<sys/types.h>
  8. int main()
  9. {
  10.  
  11. pid_t pid;
  12. int id;
  13. char msg[255];//variable qui contiendrat les messages
  14.  
  15. struct sockaddr_in informations; //structure donnant les informations sur le serveur
  16.  
  17. /*initialisation du protocole, TCP l'adresse de connection 127.0.0.1 (en local) et du port du serveur (1400)*/
  18. informations.sin_family = AF_INET;
  19. informations.sin_port = htons(2500);
  20. informations.sin_addr.s_addr = inet_addr("127.0.0.1");
  21.  
  22. int socketID = socket(AF_INET, SOCK_STREAM, 0); // creation du socket propre au client
  23.  
  24. if (socketID == -1) //test de création du socket
  25. {
  26. perror("socket");
  27. exit (-1);
  28. }
  29.  
  30. if ((connect(socketID, (struct sockaddr *) &informations, sizeof(struct sockaddr_in))) == -1) //connexion au serveur
  31. {
  32. perror("connect");
  33. exit (-1);
  34. }
  35.  
  36. if (strcmp(msg, "aurevoir") != 0)
  37. {
  38. memset(msg, 0, 255);
  39. recv(socketID, msg, 255, 0);
  40. printf ("%s\n", msg);
  41. }
  42.  
  43. do
  44. {
  45. id+=1;
  46. printf ("moi : ");
  47. fgets(msg, 255, stdin);// le client ecrit son message
  48. msg[strlen(msg) - 1] = '\0';
  49.  
  50. if ((send(socketID, msg, strlen(msg), 0)) == -1)
  51. perror("send");
  52. recv(socketID, msg, 255, 0);
  53. printf ("Phrase reçue : %s\n", msg);
  54.  
  55. }
  56. while (strcmp(msg, "aurevoir") != 0); // tant que le client n'envoie pas "aurevoir" la conversation n'est pas fini
  57.  
  58. shutdown(socketID, SHUT_RDWR);// fermeture du socket
  59.  
  60. return 0;
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement