Advertisement
Guest User

Socket Test

a guest
Apr 27th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <netdb.h>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <string>
  9. #include <cstring>
  10. #include <stdio.h>
  11. #include <chrono>
  12.  
  13. using namespace std;
  14.  
  15. std::chrono::high_resolution_clock::time_point now() {
  16.   return std::chrono::high_resolution_clock::now();
  17. }
  18.  
  19. void get_time(std::chrono::high_resolution_clock::time_point t_was, const char *text) {
  20.   std::chrono::high_resolution_clock::time_point t_now = now();
  21.   std::cout << text << " "
  22.     << chrono::duration_cast<std::chrono::microseconds>(t_now - t_was).count()
  23.     << " ("
  24.     << chrono::duration_cast<std::chrono::milliseconds>(t_now - t_was).count()
  25.     << ")"
  26.     << std::endl;
  27. }
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.     int sock;
  32.     int len = 1000000; char buf[len];
  33.     struct sockaddr_in addr;
  34.     struct hostent *hostname;  
  35.     std::chrono::high_resolution_clock::time_point xtime;
  36.  
  37.     if (argc < 2) { cout << "need host to connect"; return 228; }
  38.  
  39.  
  40.     xtime = now();
  41.     cout << "Get host by name..." << endl;
  42.     hostname = gethostbyname(argv[1]);
  43.     if (!hostname) { cout << "lookup error!"; return 322; }
  44.     get_time(xtime,"gethostbyname time -");
  45.  
  46.     char *ip =  inet_ntoa( (struct in_addr) *((struct in_addr *) hostname->h_addr_list[0]));
  47.  
  48.     xtime = now();
  49.     cout << "Opening the socket..." << endl;
  50.     sock = socket(AF_INET, SOCK_STREAM, 0);
  51.     if(sock < 0) { cout << "socket" << endl; return 1; }
  52.     get_time(xtime, "opening socket -");
  53.  
  54.     addr.sin_family = AF_INET;
  55.     addr.sin_port = htons(80); // аИаЛаИ аЛбŽаБаОаЙ аДб€бƒаГаОаЙ аПаОб€б‚...
  56.     addr.sin_addr.s_addr = inet_addr(ip);
  57.     cout << "Connecting to host..." << endl;
  58.     xtime = now();
  59.     if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { cout << "connect error" << endl; return 2; }
  60.     get_time(xtime, "socket connect -");
  61.  
  62.     std::stringstream stream_req;
  63.     stream_req << "GET / HTTP/1.1" << "\r\n" << "Host: " << argv[1] << "\r\n\r\n";
  64.     std::string request = stream_req.str();
  65.  
  66.     cout << "Sendint datia to socket" << endl;
  67.     xtime = now();
  68.     send(sock, request.c_str(), strlen(request.c_str()), 0);
  69.     get_time(xtime, "sending data -");
  70.  
  71.     cout << "Gettind data from socket" << endl;
  72.     xtime = now();
  73.     recv(sock, buf, sizeof(buf) , 0);
  74.     get_time(xtime, "getting data -");
  75.    
  76.     cout << "Bytes getted - " << strlen(buf) << std::endl;
  77.     close(sock);
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement