Advertisement
Logorants

client_inet1.c

Jan 18th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/un.h>
  5. #include <pthread.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10.  
  11.  
  12. /* MAIN */
  13.  
  14. int main() {
  15.  
  16.     /* VARS */
  17.     int sock_fd;
  18.     struct sockaddr_in socket_address;
  19.     char buffer[256];
  20.  
  21.     /* BODY */
  22.  
  23.     sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  24.  
  25.     if(sock_fd == -1){
  26.  
  27.         perror("Errore sys call socket()");
  28.         exit(EXIT_FAILURE);
  29.  
  30.     }
  31.  
  32.     socket_address.sin_family = AF_INET;
  33.     socket_address.sin_port = htons(2020);
  34.     socket_address.sin_addr.s_addr = inet_addr("79.52.89.92");
  35.  
  36.     if(connect(sock_fd, (struct sockaddr*)&socket_address, sizeof(socket_address)) == -1){
  37.  
  38.         perror("Errore sys call connect()");
  39.         exit(EXIT_FAILURE);
  40.  
  41.     }
  42.  
  43.     while(1){
  44.  
  45.         bzero(buffer, strlen(buffer));
  46.         printf("\nInserisci un numero intero di cui vuoi il quadrato > ");
  47.         fgets(buffer, 255, stdin);
  48.  
  49.         write(sock_fd, buffer, strlen(buffer));
  50.  
  51.         if(strncmp(buffer, "end", 3) == 0) break;
  52.  
  53.         read(sock_fd, buffer, 256);
  54.  
  55.         printf("%s", buffer);
  56.  
  57.     }
  58.  
  59.     close(sock_fd);
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement