Advertisement
melnikovmaxim

C++_TCP

Jan 2nd, 2020
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. // Получение IP адресов IPv4 & IPv6 по имени хоста.
  2. #include <winsock2.h>
  3. #include <stdio.h>
  4. #include <ws2tcpip.h>
  5. #include <iostream>
  6. #include <thread>
  7. #pragma comment(lib,"Ws2_32.lib")
  8.  
  9. using namespace std;
  10.  
  11. void getIPbyHostname();
  12. void connect();
  13. void listen();
  14. void printIPnv(struct addrinfo* p);
  15. void printOpenPorts();
  16.  
  17. bool isListen = false;
  18.  
  19. int main() {
  20.  
  21.     void (*pf)() = &listen;
  22.     //getIPbyHostname();
  23.     std::thread t(pf);
  24.     t.detach();
  25.     while (isListen == false) this_thread::sleep_for(std::chrono::seconds(1));
  26.     connect();
  27.  
  28.  
  29.     system("pause");
  30.     return 0;
  31. }
  32.  
  33. void getIPbyHostname()
  34. {
  35.     WSADATA wsaData;
  36.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  37.     {
  38.         cout << "WSAStartup Failed.";
  39.     }
  40.     else
  41.     {
  42.         string str_host;
  43.  
  44.         cout << "Enter hostname" << endl;
  45.         cin >> str_host;
  46.  
  47.         const char* host = str_host.c_str();
  48.  
  49.         struct addrinfo hints, * res, * p;
  50.         int status;
  51.         char ipstr[INET6_ADDRSTRLEN];
  52.  
  53.         memset(&hints, 0, sizeof hints);
  54.         hints.ai_family = AF_UNSPEC;
  55.         hints.ai_socktype = SOCK_STREAM;
  56.  
  57.         if ((status = getaddrinfo(host, NULL, &hints, &res)) != 0) {
  58.             cout << "error in status";
  59.             return;
  60.         }
  61.  
  62.         for (p = res; p != NULL; p = p->ai_next) {
  63.             void* addr;
  64.             char* ipver;
  65.  
  66.             printIPnv(p);
  67.         }
  68.  
  69.         freeaddrinfo(res);
  70.         WSACleanup();
  71.     }
  72. }
  73.  
  74. void connect()
  75. {
  76.     WSADATA wsaData;
  77.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  78.     {
  79.         cout << "WSAStartup Failed.";
  80.     }
  81.     else
  82.     {
  83.         string host, port;
  84.  
  85.         cout << "Enter hostname" << endl;
  86.         cin >> host;
  87.         cout << "Enter port" << endl;
  88.         cin >> port;
  89.  
  90.         struct addrinfo hints, * res;
  91.         int sockfd;
  92.  
  93.         memset(&hints, 0, sizeof hints);
  94.         hints.ai_family = AF_UNSPEC;
  95.         hints.ai_socktype = SOCK_STREAM;
  96.  
  97.         getaddrinfo(host.c_str(), port.c_str(), &hints, &res);
  98.  
  99.         sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  100.  
  101.         if (connect(sockfd, res->ai_addr, res->ai_addrlen) != -1)
  102.             cout << "Successfully connect" << endl;
  103.         else
  104.             cout << "Error connect, code: " << errno << endl;
  105.  
  106.         freeaddrinfo(res);
  107.         WSACleanup();
  108.  
  109.         //if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1)
  110.         //  cout << "Binding error, code: " << errno << endl;
  111.     }
  112.    
  113.  
  114. }
  115.  
  116. void listen()
  117. {
  118.     WSADATA wsaData;
  119.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  120.     {
  121.         cout << "WSAStartup Failed.";
  122.     }
  123.     else
  124.     {
  125.         struct sockaddr_storage their_addr;
  126.         socklen_t addr_size;
  127.         string port, backlog; //backlog - очередь ожидающих подключений
  128.  
  129.         cout << "Enter port" << endl;
  130.         cin >> port;
  131.         cout << "Enter backlog" << endl;
  132.         cin >> backlog;
  133.  
  134.         struct addrinfo hints, * res;
  135.         int sockfd, new_fd;
  136.  
  137.         memset(&hints, 0, sizeof hints);
  138.         hints.ai_family = AF_UNSPEC;
  139.         hints.ai_socktype = SOCK_STREAM;
  140.  
  141.         getaddrinfo("192.168.1.200", port.c_str(), &hints, &res);
  142.  
  143.         sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  144.        
  145.         if (bind(sockfd, res->ai_addr, res->ai_addrlen) != -1)
  146.             cout << "Success binding" << endl;
  147.         else
  148.             cout << "Binding error, code: " << errno << endl;
  149.  
  150.         if (listen(sockfd, atoi(backlog.c_str())) != -1) {
  151.             cout << "Success listening" << endl;
  152.             isListen = true;
  153.         }
  154.         else
  155.             cout << "Error listening, code: " << errno << endl;
  156.  
  157.         printIPnv(res);
  158.  
  159.         addr_size = sizeof their_addr;
  160.         new_fd = accept(sockfd, (struct sockaddr*) & their_addr, &addr_size);
  161.  
  162.         freeaddrinfo(res);
  163.         WSACleanup();
  164.     }
  165. }
  166.  
  167. void printIPnv(struct addrinfo *p)
  168. {
  169.     void* addr;
  170.     char* ipver;
  171.     char ipstr[INET6_ADDRSTRLEN];
  172.  
  173.     //IPv4
  174.     if (p->ai_family == AF_INET) {
  175.         struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr;
  176.         addr = &(ipv4->sin_addr);
  177.         ipver = (char*)"IPv4";
  178.     }
  179.     //IPv6
  180.     else {
  181.         struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)p->ai_addr;
  182.         addr = &(ipv6->sin6_addr);
  183.         ipver = (char*)"IPv6";
  184.     }
  185.  
  186.     inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
  187.     cout << ipver << ": " << ipstr << "\n";
  188. }
  189.  
  190. void printOpenPorts()
  191. {
  192.     WSADATA wsaData;
  193.     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
  194.     {
  195.         cout << "WSAStartup Failed.";
  196.     }
  197.     else
  198.     {
  199.         string str_host;
  200.  
  201.         cout << "Enter hostname" << endl;
  202.         str_host = "localhost";
  203.  
  204.         struct addrinfo hints, * res;
  205.         int sock;
  206.  
  207.         memset(&hints, 0, sizeof hints);
  208.         hints.ai_family = AF_UNSPEC;
  209.         hints.ai_socktype = SOCK_STREAM;
  210.  
  211.         getaddrinfo(str_host.c_str(), "5000", &hints, &res);
  212.  
  213.         sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  214.         if (connect(sock, res->ai_addr, res->ai_addrlen) != -1)
  215.             cout << "Port 0 is open" << endl;
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement