Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<sys/types.h>
  4. #include<sys/socket.h>
  5. #include<netdb.h>
  6. #include<arpa/inet.h>
  7. #include<netinet/in.h>
  8. #include<iostream>
  9. #include <unistd.h>
  10. #include<stdlib.h>
  11. using namespace std;
  12.  
  13. int main(int argc, char *argv[]) {
  14.   struct addrinfo hints, *res;
  15.   int status;
  16.   char ipstr[INET6_ADDRSTRLEN];
  17.  
  18.  
  19.  
  20.   memset(&hints, 0, sizeof hints);
  21.   hints.ai_family = AF_UNSPEC;
  22.   hints.ai_socktype = SOCK_STREAM;
  23.  
  24.   getaddrinfo("vortex.labs.overthewire.org", "5842", &hints, &res);
  25.  
  26.  
  27.   int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  28.  
  29.   connect(sockfd, res->ai_addr, res->ai_addrlen);
  30.  
  31.   if(sockfd != -1)
  32.     std::cout<<"Connessione riuscita"<<std::endl;
  33.   else
  34.     std::cout<<"Connessione rifiutata"<<std::endl;
  35.  
  36.   char *msg1, *msg2, *msg3, *msg4;
  37.  
  38.  
  39.  
  40.   int bytes_rec1 = recv(sockfd, msg1, 4, 0);
  41.   cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
  42.   unsigned long  int m1 = ntohl(*msg1);
  43.   cout<<"Primo intero letto = "<<m1<<endl;
  44.   bytes_rec1 = recv(sockfd, msg1, 4, 0);
  45.   unsigned long  int m2 = ntohl(*msg1);
  46.   cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
  47.   cout<<"Secondo intero letto = "<<m2<<endl;
  48.   bytes_rec1 = recv(sockfd, msg1, 4 , 0);
  49.   unsigned long  int m3 = ntohl(*msg1);
  50.   cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
  51.   cout<<"Terzo intero letto = "<<m3<<endl;
  52.   bytes_rec1 = recv(sockfd,msg1, 4, 0 );
  53.   cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
  54.   unsigned long  int m4 = ntohl(*msg1);
  55.   cout<<"Quarto intero letto = "<<m4<<endl;
  56.  
  57.   unsigned long  int sum = m1 + m2 + m3 + m4;
  58.  
  59.   cout<<"La somma è "<<sum<<endl;
  60.  
  61.   unsigned long int converted_sum = htonl(sum) ;
  62.  
  63.   int bytes_sent = write(sockfd, &converted_sum , 4);
  64.   cout<<"Bytes trasmessi = "<<bytes_sent<<endl;
  65.   bytes_rec1 = recv(sockfd, msg1, 1024, 0);
  66.  
  67.   cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
  68.  
  69.  
  70.   cout<<msg1<<endl;
  71.   close(sockfd);
  72.    
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement