Advertisement
Guest User

getnameinfo

a guest
Feb 15th, 2011
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <winsock2.h>
  2. #include <ws2tcpip.h>
  3. #include <stdio.h>
  4.  
  5. // link with ws2_32.lib
  6. #pragma comment(lib, "Ws2_32.lib")
  7.  
  8. int __cdecl main(int argc, char **argv)
  9. {
  10.  
  11.     //-----------------------------------------
  12.     // Declare and initialize variables
  13.     WSADATA wsaData = {0};
  14.     int iResult = 0;
  15.  
  16.     DWORD dwRetval;
  17.  
  18.     struct sockaddr_in saGNI;
  19.     char hostname[NI_MAXHOST];
  20.     char servInfo[NI_MAXSERV];
  21.     u_short port = 27015;
  22.  
  23.     // Validate the parameters
  24.     if (argc != 2) {
  25.         printf("usage: %s IPv4 address\n", argv[0]);
  26.         printf("  to return hostname\n");
  27.         printf("       %s 127.0.0.1\n", argv[0]);
  28.         return 1;
  29.     }
  30.     // Initialize Winsock
  31.     iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  32.     if (iResult != 0) {
  33.         printf("WSAStartup failed: %d\n", iResult);
  34.         return 1;
  35.     }
  36.     //-----------------------------------------
  37.     // Set up sockaddr_in structure which is passed
  38.     // to the getnameinfo function
  39.     saGNI.sin_family = AF_INET;
  40.     saGNI.sin_addr.s_addr = inet_addr(argv[1]);
  41.     saGNI.sin_port = htons(port);
  42.  
  43.     //-----------------------------------------
  44.     // Call getnameinfo
  45.     dwRetval = getnameinfo((struct sockaddr *) &saGNI,
  46.                            sizeof (struct sockaddr),
  47.                            hostname,
  48.                            NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV);
  49.  
  50.     if (dwRetval != 0) {
  51.         printf("getnameinfo failed with error # %ld\n", WSAGetLastError());
  52.         return 1;
  53.     } else {
  54.         printf("getnameinfo returned hostname = %s\n", hostname);
  55.         return 0;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement