Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #include <WinSock2.h>
- #include <stdio.h>
- #pragma comment(lib,"ws2_32.lib")
- int main(int argc, char **argv)
- {
- //-----------------------------------------
- // Declare and initialize variables
- WSADATA wsaData;
- int iResult;
- DWORD dwError;
- struct hostent *remoteHost;
- char *host_name;
- struct in_addr addr;
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0)
- {
- printf("WSAStartup failed: %d\n", iResult);
- return 1;
- }
- host_name = "www.onet.pl";
- printf("Calling gethostbyname with %s\n", host_name);
- remoteHost = gethostbyname(host_name);
- if (remoteHost == NULL)
- {
- dwError = WSAGetLastError();
- if (dwError != 0) {
- if (dwError == WSAHOST_NOT_FOUND) {
- printf("Host not found\n");
- return 1;
- }
- else if (dwError == WSANO_DATA) {
- printf("No data record found\n");
- return 1;
- }
- else {
- printf("Function failed with error: %ld\n", dwError);
- return 1;
- }
- }
- }
- else
- {
- printf("Function returned:\n");
- printf("\tOfficial name: %s\n", remoteHost->h_name);
- printf("\tAlternate names: %s\n", remoteHost->h_aliases);
- printf("\tAddress type: ");
- switch (remoteHost->h_addrtype) {
- case AF_INET:
- printf("AF_INET\n");
- break;
- case AF_INET6:
- printf("AF_INET\n");
- break;
- case AF_NETBIOS:
- printf("AF_NETBIOS\n");
- break;
- default:
- printf(" %d\n", remoteHost->h_addrtype);
- break;
- }
- printf("\tAddress length: %d\n", remoteHost->h_length);
- addr.s_addr = *(u_long *)remoteHost->h_addr_list[0];
- printf("\tFirst IP Address: %s\n", inet_ntoa(addr));
- }
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement