vc 获取本机IP
charhostname[50];structhostent*pHost;gethostname(hostname,50);pHost=gethostbyname(hos...
char hostname[50];
struct hostent * pHost;
gethostname(hostname,50);
pHost = gethostbyname(hostname);
帮我看看错在哪里,pHost一直就空 展开
struct hostent * pHost;
gethostname(hostname,50);
pHost = gethostbyname(hostname);
帮我看看错在哪里,pHost一直就空 展开
3个回答
展开全部
vc 获取本机IP
bool GetPublicIP(char* szBuffer)
{
if (NULL == szBuffer)
{
return false;
}
const int nBufSize = 256;
char chBuf[nBufSize];
ZeroMemory(chBuf, nBufSize);
WSAData wsaDat;
short int Port = 80;
WSAStartup(MAKEWORD(2, 2), &wsaDat);
SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sd)
{
return false;
}
SOCKADDR_IN sockAddr;
memset((void*)&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(Port);
sockAddr.sin_addr.s_addr = inet_addr(m_szTargetAddress[0]);
if ((connect(sd, (LPSOCKADDR)&sockAddr, sizeof(sockAddr))) == SOCKET_ERROR)
{
return false;
}
char sendstr[] = "GET /ip2city.asp HTTP/1.1\r\nAccept: */*\r\nReferer: ******\r\nUser-Agent: Mozilla/4.0\r\nHost: www.ip138.com\r\nConnection: close\r\n\r\n\0";
char* p = sendstr;
int iLen = strlen(sendstr);
while (iLen)
{
int iRet = send(sd, p, iLen, 0);
if (iRet > 0)
{
p += iRet;
iLen -= iRet;
continue;
}
return false;
}
char recvstr[1024];
memset(recvstr, 0, 1024);
p = recvstr;
char* iIndex_Head;
iLen = 1024;
int need_len = -1;
while (iLen)
{
int iRet = recv(sd, p, iLen, 0);
if (iRet <= 0)
{
return false;
}
p += iRet;
iLen -= iRet;
if ((iIndex_Head = strstr(recvstr, "\r\n\r\n")) && (need_len == -1))
{
char* iIndex = strstr(recvstr, "Content-Length:");
if (!iIndex)
{
continue;
}
char *p1 = iIndex + lstrlen("Content-Length:");
need_len = iIndex_Head - recvstr + 2 + atol(p1);
}
if (1024 - iLen >= need_len)
{
break;
}
}
char *p1 = strstr(iIndex_Head, "[");
char *p0 = strstr(p1, "]");
*p0 = '\0';
closesocket(sd);
WSACleanup();
lstrcpy(szBuffer, p1 + 1);
return true;
}
bool GetPublicIP(char* szBuffer)
{
if (NULL == szBuffer)
{
return false;
}
const int nBufSize = 256;
char chBuf[nBufSize];
ZeroMemory(chBuf, nBufSize);
WSAData wsaDat;
short int Port = 80;
WSAStartup(MAKEWORD(2, 2), &wsaDat);
SOCKET sd = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sd)
{
return false;
}
SOCKADDR_IN sockAddr;
memset((void*)&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(Port);
sockAddr.sin_addr.s_addr = inet_addr(m_szTargetAddress[0]);
if ((connect(sd, (LPSOCKADDR)&sockAddr, sizeof(sockAddr))) == SOCKET_ERROR)
{
return false;
}
char sendstr[] = "GET /ip2city.asp HTTP/1.1\r\nAccept: */*\r\nReferer: ******\r\nUser-Agent: Mozilla/4.0\r\nHost: www.ip138.com\r\nConnection: close\r\n\r\n\0";
char* p = sendstr;
int iLen = strlen(sendstr);
while (iLen)
{
int iRet = send(sd, p, iLen, 0);
if (iRet > 0)
{
p += iRet;
iLen -= iRet;
continue;
}
return false;
}
char recvstr[1024];
memset(recvstr, 0, 1024);
p = recvstr;
char* iIndex_Head;
iLen = 1024;
int need_len = -1;
while (iLen)
{
int iRet = recv(sd, p, iLen, 0);
if (iRet <= 0)
{
return false;
}
p += iRet;
iLen -= iRet;
if ((iIndex_Head = strstr(recvstr, "\r\n\r\n")) && (need_len == -1))
{
char* iIndex = strstr(recvstr, "Content-Length:");
if (!iIndex)
{
continue;
}
char *p1 = iIndex + lstrlen("Content-Length:");
need_len = iIndex_Head - recvstr + 2 + atol(p1);
}
if (1024 - iLen >= need_len)
{
break;
}
}
char *p1 = strstr(iIndex_Head, "[");
char *p0 = strstr(p1, "]");
*p0 = '\0';
closesocket(sd);
WSACleanup();
lstrcpy(szBuffer, p1 + 1);
return true;
}
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
展开全部
给你一个能用的
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA wsadata;
WORD dwVersionRequested;
int err;
err=WSAStartup(dwVersionRequested,&wsadata);
char hostname[128];
if(gethostname(hostname,128)==0)
{
printf("%s\n",hostname);//计算机名字
}
struct hostent *pHost = gethostbyname(hostname);
for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++)
{
printf("%s\n",inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
}
WSACleanup();
}
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
WSADATA wsadata;
WORD dwVersionRequested;
int err;
err=WSAStartup(dwVersionRequested,&wsadata);
char hostname[128];
if(gethostname(hostname,128)==0)
{
printf("%s\n",hostname);//计算机名字
}
struct hostent *pHost = gethostbyname(hostname);
for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++)
{
printf("%s\n",inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
}
WSACleanup();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2019-12-02
展开全部
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
WSADATA wsadata;
WORD dwVersionRequested;
int err;
dwVersionRequested = MAKEWORD(1,1);
err=WSAStartup(dwVersionRequested,&wsadata);
char hostname[128];
if(gethostname(hostname,128)==0)
{
printf("%s\n",hostname);//计算机名字
}
struct hostent *pHost = gethostbyname(hostname);
//CString strIPAddr;
for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++)
{
//printf("%s\n",inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
CString strIPAddr(inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));//把ip换成字符串形式
}
WSACleanup();
#pragma comment(lib,"ws2_32.lib")
WSADATA wsadata;
WORD dwVersionRequested;
int err;
dwVersionRequested = MAKEWORD(1,1);
err=WSAStartup(dwVersionRequested,&wsadata);
char hostname[128];
if(gethostname(hostname,128)==0)
{
printf("%s\n",hostname);//计算机名字
}
struct hostent *pHost = gethostbyname(hostname);
//CString strIPAddr;
for (int i = 0; pHost != NULL && pHost->h_addr_list[i] != NULL; i++)
{
//printf("%s\n",inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));
CString strIPAddr(inet_ntoa(*(struct in_addr *)pHost->h_addr_list[i]));//把ip换成字符串形式
}
WSACleanup();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |