#include <afxinet.h>
void CLanChatDlg::GetNetIP()
{
SetDlgItemText(IDC_NET_IP,"正在获取外网IP");
CString strsource;
CString Address;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
Address="http://www.ip138.com/ip2city.asp";//ip138网页
myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//读取网络地址 while(myHttpFile->ReadString(strsource))
{ //循环读取下载来的网页文本
// AddToLog(strsource);
int begin=0;
begin=strsource.Find("[",0);
if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip
{ int end=strsource.Find("]");
m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外网ip
//m_internetip 存放IP的控件
SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角显示外网ip
}
}
}用ip138网站 轻松获取到外网IP 你修改一下 直接调用
void CLanChatDlg::GetNetIP()
{
SetDlgItemText(IDC_NET_IP,"正在获取外网IP");
CString strsource;
CString Address;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
Address="http://www.ip138.com/ip2city.asp";//ip138网页
myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//读取网络地址 while(myHttpFile->ReadString(strsource))
{ //循环读取下载来的网页文本
// AddToLog(strsource);
int begin=0;
begin=strsource.Find("[",0);
if(begin!=-1)//如果找到"[", 则找"]" 中括号内的文本则是 你的外网ip
{ int end=strsource.Find("]");
m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外网ip
//m_internetip 存放IP的控件
SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角显示外网ip
}
}
}用ip138网站 轻松获取到外网IP 你修改一下 直接调用
或者使用函数int getpeername(
SOCKET s,
struct sockaddr FAR *name,
int FAR *namelen
);
来在建立连接的socket上获取对方的地址信息。
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
ULONG ulOutBufLen;
pAdapterInfo=(PIP_ADAPTER_INFO)malloc(sizeof(IP_ADAPTER_INFO));
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
// 第一次调用GetAdapterInfo获取ulOutBufLen大小
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
{
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}
if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
if (pAdapter)
{
memcpy(pIPinfo,pAdapter->IpAddressList.IpAddress.String,strlen(pAdapter->IpAddressList.IpAddress.String));
strncat(pIPinfo,"\n",1);
strncat(pIPinfo,pAdapter->IpAddressList.IpMask.String,strlen(pAdapter->IpAddressList.IpMask.String));
strncat(pIPinfo,"\n",1);
strncat(pIPinfo,pAdapter->GatewayList.IpAddress.String,strlen(pAdapter->GatewayList.IpAddress.String));
}
}
else
{
printf("Call to GetAdaptersInfo failed.\n");
}
return TRUE;
}
直接给你封装好了,你要是只获取外网的IP,楼上上的就是了