楼主好懒
void ReadInternetIP(char *OutBuf)
{
SOCKET ClientSocket;
char* lpbuffer;
BOOL bResult = FALSE;
DWORD nbytes;
char Buf[1024] = {0};
char IPBuf[1024] = {0};
char IPNextBuf[1024] = {0};
char InternetIP[100] = {0};
char *p = NULL;
__try{
// //使用wininetAPI,win7 64位下取到IP之后,还继续保持连接,**,这点不好,直接用socket操作算了
// hSession = InternetOpenA("HttpOpen", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
// if (hSession != NULL)
// {
// hRequest = InternetOpenUrlA(hSession, "http://iframe.ip138.com/ic.asp", NULL, 0, INTERNET_FLAG_RELOAD, 0);
// if (hRequest != NULL)
// {
// if (InternetReadFile(hRequest, Buf,MAX_PATH, &dwBytesRead))
// {
// bResult = TRUE;
// }
// InternetCloseHandle(hRequest);
// }
// InternetCloseHandle(hSession);
// }
char request[1024] = "GET /ic.asp HTTP/1.1\r\nHost:iframe.ip138.com\r\nConnection: close\r\n\r\n"; //Connection: close 要求http服务器,接收完数据之后,客户端断开服务器也要断开
HOSTENT *host_entry = gethostbyname("iframe.ip138.com");
if (host_entry)
{
lpbuffer = inet_ntoa(*((struct in_addr *)host_entry->h_addr)) ;
sockaddr_in servAddr;
servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(80);
servAddr.sin_addr.S_un.S_addr=inet_addr(lpbuffer);
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ClientSocket != INVALID_SOCKET )
{
if(connect(ClientSocket,(struct sockaddr*)&servAddr, sizeof(servAddr))!= -1)
{
if(send(ClientSocket, request, (int)strlen(request), 0) != -1)
{
if((nbytes=recv(ClientSocket,Buf,1024,0)) != -1)
{
bResult = TRUE;
}
}
}
//断开连接
closesocket(ClientSocket);
}
}
if (strlen(Buf) && bResult)
{
p = strstr(Buf,"[");
if (p)
{
sprintf(IPBuf,"%s",p+1);
p = strstr(Buf,"]");
if (p)
{
sprintf(IPNextBuf,"%s",p);
if (strlen(IPBuf) > strlen(IPNextBuf))
{
memcpy(OutBuf,IPBuf,strlen(IPBuf) - strlen(IPNextBuf));
}
}
}
}
}__except(1){
}
}