Advertisement
Benlahbib_Abdessamad

Untitled

May 21st, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <netinet/in.h>
  3. #include <arpa/inet.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9.  
  10. void printconnerror()
  11. {
  12. switch (errno) {
  13. case ETIMEDOUT : printf("Connection timed out.\n"); break;
  14. case ECONNREFUSED : printf("Connection refused.\n"); break;
  15. case EHOSTDOWN : printf("Host down.\n"); break;
  16. case EHOSTUNREACH : printf("No route to the host.\n"); break;
  17. case ENETUNREACH : printf("Network unreachable.\n"); break;
  18. default: printf("errno = %d\n", errno);
  19. }
  20. }
  21.  
  22.  
  23. int main()
  24. {
  25. int sockfd,rec_sock,socknew, len,i,choix;
  26. struct sockaddr_in addr, recaddr;
  27. char buf[100],buf1[100],ip[22];
  28. int port;
  29. FILE* fichier = NULL;
  30.  
  31.  
  32.  
  33. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  34. perror(": Can't get socket");
  35. exit(1);
  36. }
  37.  
  38. addr.sin_addr.s_addr = INADDR_ANY;
  39. addr.sin_family = AF_INET;
  40. addr.sin_port = htons(4450);
  41.  
  42. if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  43. perror(": bind");
  44. exit(1);
  45. }
  46.  
  47. printf("ip = %s, port = %d\n", inet_ntoa(addr.sin_addr), addr.sin_port);
  48.  
  49. if (listen(sockfd, 5) < 0) {
  50. perror(": bind");
  51. exit(1);
  52. }
  53.  
  54.  
  55. rec_sock = accept(sockfd, (struct sockaddr *)(&recaddr), &len);
  56.  
  57. if (rec_sock < 0) {
  58. perror(": accept");
  59. exit(1);
  60. }
  61.  
  62. /* print the remote socket information */
  63.  
  64. printf("remote machine = %s, port = %x, %x.\n", inet_ntoa(recaddr.sin_addr), recaddr.sin_port, ntohs(recaddr.sin_port));
  65.  
  66.  
  67.  
  68. write(rec_sock, "connecter", 20);
  69.  
  70. for (i=0; i<100; i++)
  71. buf[i] = '\0';
  72. read(rec_sock, buf, 99);
  73. printf("port aleatoire : %s\n", buf);
  74.  
  75. // put client info in a txt file
  76.  
  77. fichier = fopen("tmp.txt", "w");
  78. if (fichier != NULL)
  79. {
  80. fprintf(fichier,"Remote machine ip = %s Port = %s \n",inet_ntoa(recaddr.sin_addr),buf);
  81. fclose(fichier);
  82. strcpy(ip,inet_ntoa(recaddr.sin_addr));
  83. }
  84. printf("\n \n");
  85. port=atoi(buf);
  86.  
  87.  
  88. printf("%s \n",ip);
  89. printf("%d \n",port);
  90.  
  91.  
  92. printf("voulez vous se connecte au client\n");
  93. scanf("%d",&choix);
  94. if(choix==1)
  95. {
  96. if ((socknew = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  97. perror(": Can't get socket");
  98. exit(1);
  99. }
  100.  
  101.  
  102. memset(&recaddr, 0, sizeof(struct sockaddr_in));
  103.  
  104.  
  105. recaddr.sin_family = AF_INET;
  106. recaddr.sin_port = htons(port);
  107. recaddr.sin_addr.s_addr = inet_addr(ip);
  108.  
  109. //printf("%s \n",ntohs(recaddr.sin_port));
  110. //printf("%s \n",inet_ntoa(recaddr.sin_addr));
  111.  
  112. if (connect(socknew, (struct sockaddr *)&recaddr, sizeof(recaddr)) < 0) {
  113. printconnerror();
  114. perror(": connect");
  115. exit(1);
  116. }
  117.  
  118. for (i=0; i<100; i++)
  119. buf[i] = '\0';
  120. read(socknew, buf, 99);
  121. printf("Chaine = %s\n", buf);
  122. printf("Commande a executer \n");
  123. scanf("%s",&buf1);
  124. write(socknew,buf1,20);
  125.  
  126.  
  127. }
  128. sleep(20);
  129. exit(1);
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement