Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Получение IP адресов IPv4 & IPv6 по имени хоста.
- #include <winsock2.h>
- #include <stdio.h>
- #include <ws2tcpip.h>
- #include <iostream>
- #include <thread>
- #pragma comment(lib,"Ws2_32.lib")
- using namespace std;
- void getIPbyHostname();
- void connect();
- void listen();
- void printIPnv(struct addrinfo* p);
- void printOpenPorts();
- bool isListen = false;
- int main() {
- void (*pf)() = &listen;
- //getIPbyHostname();
- std::thread t(pf);
- t.detach();
- while (isListen == false) this_thread::sleep_for(std::chrono::seconds(1));
- connect();
- system("pause");
- return 0;
- }
- void getIPbyHostname()
- {
- WSADATA wsaData;
- if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
- {
- cout << "WSAStartup Failed.";
- }
- else
- {
- string str_host;
- cout << "Enter hostname" << endl;
- cin >> str_host;
- const char* host = str_host.c_str();
- struct addrinfo hints, * res, * p;
- int status;
- char ipstr[INET6_ADDRSTRLEN];
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- if ((status = getaddrinfo(host, NULL, &hints, &res)) != 0) {
- cout << "error in status";
- return;
- }
- for (p = res; p != NULL; p = p->ai_next) {
- void* addr;
- char* ipver;
- printIPnv(p);
- }
- freeaddrinfo(res);
- WSACleanup();
- }
- }
- void connect()
- {
- WSADATA wsaData;
- if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
- {
- cout << "WSAStartup Failed.";
- }
- else
- {
- string host, port;
- cout << "Enter hostname" << endl;
- cin >> host;
- cout << "Enter port" << endl;
- cin >> port;
- struct addrinfo hints, * res;
- int sockfd;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- getaddrinfo(host.c_str(), port.c_str(), &hints, &res);
- sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if (connect(sockfd, res->ai_addr, res->ai_addrlen) != -1)
- cout << "Successfully connect" << endl;
- else
- cout << "Error connect, code: " << errno << endl;
- freeaddrinfo(res);
- WSACleanup();
- //if (bind(sockfd, res->ai_addr, res->ai_addrlen) == -1)
- // cout << "Binding error, code: " << errno << endl;
- }
- }
- void listen()
- {
- WSADATA wsaData;
- if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
- {
- cout << "WSAStartup Failed.";
- }
- else
- {
- struct sockaddr_storage their_addr;
- socklen_t addr_size;
- string port, backlog; //backlog - очередь ожидающих подключений
- cout << "Enter port" << endl;
- cin >> port;
- cout << "Enter backlog" << endl;
- cin >> backlog;
- struct addrinfo hints, * res;
- int sockfd, new_fd;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- getaddrinfo("192.168.1.200", port.c_str(), &hints, &res);
- sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if (bind(sockfd, res->ai_addr, res->ai_addrlen) != -1)
- cout << "Success binding" << endl;
- else
- cout << "Binding error, code: " << errno << endl;
- if (listen(sockfd, atoi(backlog.c_str())) != -1) {
- cout << "Success listening" << endl;
- isListen = true;
- }
- else
- cout << "Error listening, code: " << errno << endl;
- printIPnv(res);
- addr_size = sizeof their_addr;
- new_fd = accept(sockfd, (struct sockaddr*) & their_addr, &addr_size);
- freeaddrinfo(res);
- WSACleanup();
- }
- }
- void printIPnv(struct addrinfo *p)
- {
- void* addr;
- char* ipver;
- char ipstr[INET6_ADDRSTRLEN];
- //IPv4
- if (p->ai_family == AF_INET) {
- struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr;
- addr = &(ipv4->sin_addr);
- ipver = (char*)"IPv4";
- }
- //IPv6
- else {
- struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)p->ai_addr;
- addr = &(ipv6->sin6_addr);
- ipver = (char*)"IPv6";
- }
- inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
- cout << ipver << ": " << ipstr << "\n";
- }
- void printOpenPorts()
- {
- WSADATA wsaData;
- if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
- {
- cout << "WSAStartup Failed.";
- }
- else
- {
- string str_host;
- cout << "Enter hostname" << endl;
- str_host = "localhost";
- struct addrinfo hints, * res;
- int sock;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- getaddrinfo(str_host.c_str(), "5000", &hints, &res);
- sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
- if (connect(sock, res->ai_addr, res->ai_addrlen) != -1)
- cout << "Port 0 is open" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement