Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<sys/types.h>
- #include<sys/socket.h>
- #include<netdb.h>
- #include<arpa/inet.h>
- #include<netinet/in.h>
- #include<iostream>
- #include <unistd.h>
- #include<stdlib.h>
- using namespace std;
- int main(int argc, char *argv[]) {
- struct addrinfo hints, *res;
- int status;
- char ipstr[INET6_ADDRSTRLEN];
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- getaddrinfo("vortex.labs.overthewire.org", "5842", &hints, &res);
- int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- connect(sockfd, res->ai_addr, res->ai_addrlen);
- if(sockfd != -1)
- std::cout<<"Connessione riuscita"<<std::endl;
- else
- std::cout<<"Connessione rifiutata"<<std::endl;
- char *msg1, *msg2, *msg3, *msg4;
- int bytes_rec1 = recv(sockfd, msg1, 4, 0);
- cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
- unsigned long int m1 = ntohl(*msg1);
- cout<<"Primo intero letto = "<<m1<<endl;
- bytes_rec1 = recv(sockfd, msg1, 4, 0);
- unsigned long int m2 = ntohl(*msg1);
- cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
- cout<<"Secondo intero letto = "<<m2<<endl;
- bytes_rec1 = recv(sockfd, msg1, 4 , 0);
- unsigned long int m3 = ntohl(*msg1);
- cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
- cout<<"Terzo intero letto = "<<m3<<endl;
- bytes_rec1 = recv(sockfd,msg1, 4, 0 );
- cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
- unsigned long int m4 = ntohl(*msg1);
- cout<<"Quarto intero letto = "<<m4<<endl;
- unsigned long int sum = m1 + m2 + m3 + m4;
- cout<<"La somma è "<<sum<<endl;
- unsigned long int converted_sum = htonl(sum) ;
- int bytes_sent = write(sockfd, &converted_sum , 4);
- cout<<"Bytes trasmessi = "<<bytes_sent<<endl;
- bytes_rec1 = recv(sockfd, msg1, 1024, 0);
- cout<<"Bytes ricevuti = "<<bytes_rec1<<endl;
- cout<<msg1<<endl;
- close(sockfd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement