假设你有了名为My的对话框工程.有一个按钮并有响应的程序:如OnButton1(); BOOL CListCtrl1Dlg::OnInitDialog() { CDialog::OnInitDialog(); AfxSocketInit(NULL);//支持Socket.若在向导是没选Support Socket,这就的加.还要加#include <afxsock.h>在StdAfx.h中........ // Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control }void CListCtrl1Dlg::OnButton1() { WORD wVersionRequested; WSADATA wsaData; char name[255]; CString ip; PHOSTENT hostinfo; wVersionRequested = MAKEWORD( 2, 0 );if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) {if( gethostname ( name, sizeof(name)) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); } }WSACleanup( ); } AfxMessageBox(name);//name里是本机名 AfxMessageBox(ip); //ip中是本机IP }
#include <winsock2.h> Link with Wsock32.lib That's It. { WORD wVersionRequested; WSADATA wsaData; char name[255]; CString ip; PHOSTENT hostinfo; wVersionRequested = MAKEWORD( 2, 0 ); if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) { if( gethostname ( name, sizeof(name)) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); } }
WSACleanup( ); } }Following is a code that gets local machine IP addresses. The advantages over the article by Jeff Lundgren are that my code recognises ALL IP addresses and is ready for IPv6 ;-) char szHostName[128];if( gethostname(szHostName, 128) == 0 ){ // Get host adresses struct hostent * pHost; int i; pHost = gethostbyname(szHostName); for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) { CString str; int j; for( j = 0; j < pHost->h_length; j++ ) { CString addr; if( j > 0 ) str += "."; addr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]); str += addr; } // str now contains one local IP address - do whatever you want to do with it (probably add it to a list) }}
2、gethostbyname
2会得到一个联表,一般取第一个就可以了。
char szIP[16],szName[255];
memset(szIP,0,sizeof(szIP));
memset(szName,0,sizeof(szName)); PHOSTENT pstHostInfo;
char *pszTemp = NULL; struct in_addr addr[2];
for (int i=0; i< 2; i++)
memset(&addr[i],0,sizeof(in_addr));
if( gethostname (m_szHostName, 255) == 0)
{
if((pstHostInfo = gethostbyname(m_szHostName)) != NULL)
{ for (int i=0; pstHostInfo->h_addr_list[i]!=0 && i<8; i++)
{
memcpy(&addr[i],pstHostInfo->h_addr_list[i],sizeof(in_addr));
pszTemp = inet_ntoa(addr[i]);
memcpy(m_szIP[i],pszTemp,15);
m_uiTotalAdapters++;
}
}
else
return false;
}
else
return false;
return true;
BOOL CListCtrl1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
AfxSocketInit(NULL);//支持Socket.若在向导是没选Support Socket,这就的加.还要加#include <afxsock.h>在StdAfx.h中........
// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.// TODO: Add extra initialization herereturn TRUE; // return TRUE unless you set the focus to a control
}void CListCtrl1Dlg::OnButton1()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}WSACleanup( );
}
AfxMessageBox(name);//name里是本机名
AfxMessageBox(ip); //ip中是本机IP
}
#include <winsock2.h>
Link with Wsock32.lib
That's It.
{ WORD wVersionRequested; WSADATA wsaData; char name[255]; CString ip; PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 ) {
if( gethostname ( name, sizeof(name)) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); } }
WSACleanup( ); }
}Following is a code that gets local machine IP addresses. The advantages over the article by Jeff Lundgren are that my code recognises ALL IP addresses and is ready for IPv6 ;-) char szHostName[128];if( gethostname(szHostName, 128) == 0 ){ // Get host adresses struct hostent * pHost; int i; pHost = gethostbyname(szHostName); for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) { CString str; int j; for( j = 0; j < pHost->h_length; j++ ) { CString addr; if( j > 0 ) str += "."; addr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]); str += addr; } // str now contains one local IP address - do whatever you want to do with it (probably add it to a list) }}