If anyone here has ever used the function gethostbyaddr or gethostbyname can you pleas tell me how to use them
If anyone here has ever used the function gethostbyaddr or gethostbyname can you pleas tell me how to use them
Here is a MFC snippet. I just made it in a hurry, to show you the usage of gethostbyname.
The whole thing is in the OnOK event, as you see.
The dialog contains a ListBox(multiline, control variable m_ListC, to display the data ) and an EditBox (control var m_EditV) to enter host name.
void CGethostDlg::OnOK()
{
// TODO: Add extra validation here
hostent* aHostenPtr;
CString aHostName;
UpdateData( TRUE );
if ( m_EditV.IsEmpty() ) // if no data entered, warn
{
MessageBox( "Enter host name, please!","No host name entered", MB_OK );
return;
}
aHostName = m_EditV;
const char* aHostNameChr = (LPCSTR)aHostName;
aHostenPtr = gethostbyname( aHostNameChr );
if ( !aHostenPtr )
{
MessageBox( "gethostbyname failed!", "Warning", MB_OK );
return;
}
m_ListC.AddString( (LPCTSTR) aHostenPtr->h_aliases );
m_ListC.AddString( (LPCTSTR) aHostenPtr->h_name );
m_ListC.AddString( (LPCTSTR) aHostenPtr->h_addr_list[0] );
UpdateData( FALSE );
}
Whether with or without MFC, the function needs a hostent* as return type, and char* as param.
That's all, hope this helps.
[R]evolution!
Programming related articles, downloads, demos