Advertisement
Expyvolts

cod server

May 13th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. /* Modul CLIENT TCP */
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <sys/types.h>
  6.  
  7. #include <sys/socket.h>
  8.  
  9. #include <netinet/in.h>
  10.  
  11. #include <errno.h>
  12.  
  13. #include <netdb.h>
  14.  
  15.  
  16.  
  17. main (int argc, char ** argv)
  18.  
  19. {
  20.  
  21. unsigned short port; /* portul client, acelasi ca cel specificat in modulul server */
  22.  
  23. char buffer[32];
  24.  
  25. struct sockaddr_in server; /*adresa server*/
  26.  
  27. struct hostent * hostnm; /*informatii server */
  28.  
  29. int s, rc;
  30.  
  31.  
  32.  
  33. if (argc != 3)
  34.  
  35. {
  36.  
  37. printf("Apel: %s hostname port \n", argv[0]);
  38.  
  39. exit(1);
  40.  
  41. }
  42.  
  43.  
  44.  
  45. /*preia numele calculatorului local */
  46.  
  47. if (!(hostnm=(struct hostent *) gethostbyname(argv[1])))
  48.  
  49.  
  50.  
  51. {
  52.  
  53. perror("Eroare la preluarea numelui serverului");
  54.  
  55. exit(3);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. printf("A preluat numele de server grupa 1080 Batrinac Sergiu client TCP.... \n");
  62.  
  63.  
  64.  
  65. port = (unsigned short)atoi(argv[2]);
  66.  
  67. strcpy(buffer, "Mesaj prin socket, TCP-gr1080");
  68.  
  69.  
  70.  
  71. /*preia informatiile despre server*/
  72.  
  73. server.sin_family = AF_INET; /*AF_UNIX, AF_INET*/
  74.  
  75. server.sin_port = htons(port);
  76.  
  77. server.sin_addr.s_addr = inet_addr("172.22.23.56"); /* aceeasi ca la server */
  78.  
  79.  
  80.  
  81. printf("A preluat inf. despre server....\n");
  82. printf("Am transmis mesaj pe TCP Batrinac Sergiu");
  83.  
  84.  
  85.  
  86. if ((s = socket(AF_INET,SOCK_STREAM,0)) < 0)
  87.  
  88. {
  89.  
  90. perror("Eroare la creare socket");
  91.  
  92. exit(4);
  93.  
  94. }
  95.  
  96.  
  97.  
  98. printf("A creat socket-ul....\n");
  99.  
  100. if (connect(s, &server, sizeof(server)) < 0)
  101.  
  102. {
  103.  
  104. perror("Eroare la obtinerea conexiunii");
  105.  
  106. exit(5);
  107.  
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. printf("A realizat conexiunea.....\n");
  115.  
  116.  
  117.  
  118. /* Transmite mesajul serverului */
  119.  
  120. if (send(s, buffer, strlen(buffer), 0) < 0)
  121.  
  122. {
  123.  
  124. perror("Eroare la transmisie");
  125.  
  126. exit(6);
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134. printf("A transmis mesajul.....\n");
  135.  
  136.  
  137.  
  138. /* Confirmare de la server */
  139.  
  140. if (recv(s, buffer, 32, 0) < 0)
  141.  
  142. {
  143.  
  144. perror("Eroare la receptie");
  145.  
  146. exit(7);
  147.  
  148. }
  149.  
  150.  
  151.  
  152. printf("Confirmarea de la server a fost facuta.... \n");
  153.  
  154.  
  155.  
  156. close(s);
  157.  
  158. exit(0);
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement