Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include <stdio.h>
- #include <conio.h>
- #include <vector>
- #include <winsock2.h>
- using namespace std;
- #pragma comment (lib, "Ws2_32.lib")
- int main() {
- WSADATA wsaData;
- int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != NO_ERROR)
- printf("Error at WSAStartup().\n");
- else
- printf("WSAStartup() is OK.\n");
- hostent* remoteHost;
- char* ip;
- char* host_name;
- unsigned int addr;
- printf("Input name/IP of host: ");
- host_name = (char*)malloc(sizeof(char*) * 128);
- fgets(host_name, 128, stdin);
- if (isalpha(host_name[0])) {
- printf("\nUsing gethostbyname()...\n");
- host_name[strlen(host_name) - 1] = '\0';
- remoteHost = gethostbyname(host_name);
- vector<string> tab;
- int i = 0;
- while (remoteHost->h_addr_list[i]) { //podsumowujÄ…c:
- if (remoteHost->h_addrtype == AF_INET) { //char* w wektorze, push_back i fgets/gets
- tab.push_back( string(inet_ntoa(*((in_addr*)remoteHost->h_addr_list[i]))) );
- i++;
- }
- }
- //ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list);
- //printf("IP address is: %s.\n", ip);
- //printf("Address type: %i.\n\n", remoteHost->h_addrtype);
- for (int a = 0; a < tab.size(); a++) {
- printf("IP address is: %s\n", tab[a].c_str());
- //printf("Address type: %i.\n\n", remoteHost->h_addrtype[a]);
- }
- }
- else {
- printf("\nUsing gethostbyaddr()...\n");
- addr = inet_addr(host_name);
- remoteHost = gethostbyaddr((char *)&addr, 4, AF_INET);
- printf("Hostname is: %s.\n", remoteHost->h_name);
- printf("Address type: %i.\n\n", remoteHost->h_addrtype);
- }
- if (WSAGetLastError() != 0) {
- if (WSAGetLastError() == 11001)
- printf("Host not found...\nExiting.\n");
- }
- else
- printf("error #: %ld\n", WSAGetLastError());
- _getch();
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement