OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
About THostInfoManager

The THostInfoManager class (and its friend class THostInfoWindow) encapsulate the Winsock database functions gethostbyname(), gethostbyaddr(), and gethostname().

These blocking (gethostby...) and non-blocking (gethostname) functions return information about the host in a hostent structure.

The THostInfoManager supports direct equivalents to the blocking gethostbyaddr() and gethostbyname() Winsock functions. In Winsock 1.1, the gethostbyaddr() and gethostbyname() functions work only for Internet addresses. Thus, the THostInfoManager has the same restriction. However, the THostInfoManager functions accept a TSocketAddress as the address parameter instead of an TINetSocketAddress. This is to be forward-compatible with Winsock 2.0. Lets say you have a host name, "elvis@ms.com," and you want to get its THostInfo (hostent). In Winsock, you could say:

tempHostent = gethostbyname("elvis@ms.com");

Calling the THostInfoManager function GetHostInfo() has the same effect:

nError = myHostInfoMgr.GetHostInfo(tempHostent, "elvis@ms.com");

While the above example will get you a THostInfo (hostent) structure, it doesn't give you an IP address or TSocketAddress directly. Most often, you'll want to use the THostInfoManager to convert fully qualified names such as "elvis@ms.com" to IP addresses or TSocketAddresses. You can use the GetHostAddress(char* szHostAddress, const char* szHostName) or the GetHostAddress(SocketAddress& sAddress, const char* szHostName) to do this. Lets say you have a host name, "elvis@ms.com," and you want to get its equivalent IP address. In Winsock, you would say:

hostent* tempHostent;
char* szAddress;
tempHostent = gethostbyname("elvis@ms.com");
tempInAddr.s_addr = *((u_long*)tempHostEntry->h_addr);
szAddress = inet_ntoa(tempInAddr);

Calling the HostInfoManager function GetHostInfo() has the same effect, but is more concise, because it takes care of all the housekeeping:

nError = myHostInfoMgr.GetHostInfo(szDottedDecimalAddress, "elvis@ms.com");

THostInfoManager also supports asynchronous (non-blocking) Winsock host information lookups. These correspond to the WSAGetHostByName() and WSAGetHostByAddr() functions. To use these classes in Winsock, you need to pass an HWND to these functions and wait till Winsock calls you back with an answer. In Windows Socket Class, you have two choices:

  1. You can give the THostInfoManager a TWindow to notify upon completion using GetHostInfoAsync().
  2. You can let THostInfoManager get the notification itself, and you simply get the information from the THostInfoManager when the information is ready. This approach lets the THostInfoManager do all the work; you simply don't have to deal with HWNDs, wMsgs, wParams, etc. This option is very similar to the Overlapped I/O concept of Windows NT: Reads and Writes don't block, but the function returns as if the data was read or sent, and a notification will be posted when the read/write actually occurs. This is very useful for times when one thread handles multiple sockets at a time.

When the asynchronous call completes, the THostInfoManager:: SetHostRequestCompleted() function gets called, and the HostRequestCompleted member is set to true. You may at any time call (poll) THostInfoManager::GetHostRequestCompleted() to see if the last request has completed. If GetHostRequestCompleted() returns true, then you can examine the THostInfoManager::HostEntry member, or call THostInfoManager:: HostEntryToAddress()to get an address.

The Winsock host information database functions return a pointer to a read-only hostent structure.

THostInfoManager uses a subclass of the hostent class called the THostEntry class. Since THostEntry is a subclass of hostent, it can be used anywhere that Winsock or Windows Socket Class requires a hostent.