Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <cstring>
- #include <stdio.h>
- #include <chrono>
- using namespace std;
- std::chrono::high_resolution_clock::time_point now() {
- return std::chrono::high_resolution_clock::now();
- }
- void get_time(std::chrono::high_resolution_clock::time_point t_was, const char *text) {
- std::chrono::high_resolution_clock::time_point t_now = now();
- std::cout << text << " "
- << chrono::duration_cast<std::chrono::microseconds>(t_now - t_was).count()
- << " ("
- << chrono::duration_cast<std::chrono::milliseconds>(t_now - t_was).count()
- << ")"
- << std::endl;
- }
- int main(int argc, char **argv)
- {
- int sock;
- int len = 1000000; char buf[len];
- struct sockaddr_in addr;
- struct hostent *hostname;
- std::chrono::high_resolution_clock::time_point xtime;
- if (argc < 2) { cout << "need host to connect"; return 228; }
- xtime = now();
- cout << "Get host by name..." << endl;
- hostname = gethostbyname(argv[1]);
- if (!hostname) { cout << "lookup error!"; return 322; }
- get_time(xtime,"gethostbyname time -");
- char *ip = inet_ntoa( (struct in_addr) *((struct in_addr *) hostname->h_addr_list[0]));
- xtime = now();
- cout << "Opening the socket..." << endl;
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if(sock < 0) { cout << "socket" << endl; return 1; }
- get_time(xtime, "opening socket -");
- addr.sin_family = AF_INET;
- addr.sin_port = htons(80); // аИаЛаИ аЛбаБаОаЙ аДббаГаОаЙ аПаОбб...
- addr.sin_addr.s_addr = inet_addr(ip);
- cout << "Connecting to host..." << endl;
- xtime = now();
- if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { cout << "connect error" << endl; return 2; }
- get_time(xtime, "socket connect -");
- std::stringstream stream_req;
- stream_req << "GET / HTTP/1.1" << "\r\n" << "Host: " << argv[1] << "\r\n\r\n";
- std::string request = stream_req.str();
- cout << "Sendint datia to socket" << endl;
- xtime = now();
- send(sock, request.c_str(), strlen(request.c_str()), 0);
- get_time(xtime, "sending data -");
- cout << "Gettind data from socket" << endl;
- xtime = now();
- recv(sock, buf, sizeof(buf) , 0);
- get_time(xtime, "getting data -");
- cout << "Bytes getted - " << strlen(buf) << std::endl;
- close(sock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement