获取本机IP,我这样写为什么不行?
12 个解决方案
#1
void CASockView::OnIp()
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
#2
就是说gethostname(name,sizeof(name))得出来的结果是false
执行不到IF结构里去。
执行不到IF结构里去。
#3
用WSAGetLastError看看是什么错误
#4
你的name必须是服务器的名称啊,size不能用sizeof(name),有多少个字就用多少个。
如果已经连结上了,可以用
GetPeerName()来得到对方IP
GetSockName()得到本地IP
如果已经连结上了,可以用
GetPeerName()来得到对方IP
GetSockName()得到本地IP
#5
怎么用,??
具体一点好吗
具体一点好吗
#6
BOOL GetPeerName( CString& rPeerAddress, UINT& rPeerPort );
BOOL GetSockName( CString& rSocketAddress, UINT& rSocketPort );
Parameters
rSocketAddress
Reference to a CString object that receives a dotted number IP address.
rSocketPort
Reference to a UINT that stores a port.
BOOL GetSockName( CString& rSocketAddress, UINT& rSocketPort );
Parameters
rSocketAddress
Reference to a CString object that receives a dotted number IP address.
rSocketPort
Reference to a UINT that stores a port.
#7
我晕,MSDN里复制出来的
有没有现成的例子?
让我参考一下?
有没有现成的例子?
让我参考一下?
#8
本机地址:
char chHostName[256];
struct hostent *Host = NULL;
gethostname(chHostName, 256);
Host = gethostbyname(chHostName);
if (Host)
CopyMemory(&InternetAddr.sin_addr, Host->h_addr_list[0], Host->h_length);
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
char chHostName[256];
struct hostent *Host = NULL;
gethostname(chHostName, 256);
Host = gethostbyname(chHostName);
if (Host)
CopyMemory(&InternetAddr.sin_addr, Host->h_addr_list[0], Host->h_length);
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
#9
晕,少打了一行
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(ClientAddr);
Socket accepteSocket = accept(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(ClientAddr);
Socket accepteSocket = accept(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
#10
CopyMemory(&InternetAddr.sin_addr, Host->h_addr_list[0], Host->h_length);
是什么意思,能解释一下吗?
取本机地址,我这样写不行,因为什么?
void CASockView::OnIp()
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
是什么意思,能解释一下吗?
取本机地址,我这样写不行,因为什么?
void CASockView::OnIp()
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
#11
好的,
谢谢你们,
谢谢你们,
#12
学习学习!
多谢楼上各位!
多谢楼上各位!
#1
void CASockView::OnIp()
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
#2
就是说gethostname(name,sizeof(name))得出来的结果是false
执行不到IF结构里去。
执行不到IF结构里去。
#3
用WSAGetLastError看看是什么错误
#4
你的name必须是服务器的名称啊,size不能用sizeof(name),有多少个字就用多少个。
如果已经连结上了,可以用
GetPeerName()来得到对方IP
GetSockName()得到本地IP
如果已经连结上了,可以用
GetPeerName()来得到对方IP
GetSockName()得到本地IP
#5
怎么用,??
具体一点好吗
具体一点好吗
#6
BOOL GetPeerName( CString& rPeerAddress, UINT& rPeerPort );
BOOL GetSockName( CString& rSocketAddress, UINT& rSocketPort );
Parameters
rSocketAddress
Reference to a CString object that receives a dotted number IP address.
rSocketPort
Reference to a UINT that stores a port.
BOOL GetSockName( CString& rSocketAddress, UINT& rSocketPort );
Parameters
rSocketAddress
Reference to a CString object that receives a dotted number IP address.
rSocketPort
Reference to a UINT that stores a port.
#7
我晕,MSDN里复制出来的
有没有现成的例子?
让我参考一下?
有没有现成的例子?
让我参考一下?
#8
本机地址:
char chHostName[256];
struct hostent *Host = NULL;
gethostname(chHostName, 256);
Host = gethostbyname(chHostName);
if (Host)
CopyMemory(&InternetAddr.sin_addr, Host->h_addr_list[0], Host->h_length);
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
char chHostName[256];
struct hostent *Host = NULL;
gethostname(chHostName, 256);
Host = gethostbyname(chHostName);
if (Host)
CopyMemory(&InternetAddr.sin_addr, Host->h_addr_list[0], Host->h_length);
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
#9
晕,少打了一行
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(ClientAddr);
Socket accepteSocket = accept(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
客户端地址:
SOCKADDR_IN ClientAddr;
int addr_length = sizeof(ClientAddr);
Socket accepteSocket = accept(Socket,(SOCKADDR*)&ClientAddr,&adr_length);
LPCTSTR lpIp = inet_ntoa(ClientAddr.sin_addr);//Client IP
UINT nPort = ClientAddr.sin_port; //
#10
CopyMemory(&InternetAddr.sin_addr, Host->h_addr_list[0], Host->h_length);
是什么意思,能解释一下吗?
取本机地址,我这样写不行,因为什么?
void CASockView::OnIp()
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
是什么意思,能解释一下吗?
取本机地址,我这样写不行,因为什么?
void CASockView::OnIp()
{
// TODO: Add your command handler code here
char name[255];
PHOSTENT hostinfo;
CString ip;
if(gethostname(name,sizeof(name)))
{
if((hostinfo=gethostbyname(name))!=NULL)
{
LPCSTR ip=inet_ntoa(*(struct in_addr *)*hostinfo->h_addr_list);
MessageBox(ip);
}
}
}
#11
好的,
谢谢你们,
谢谢你们,
#12
学习学习!
多谢楼上各位!
多谢楼上各位!