?菜鸟求救.急救.........

zdk187624 2001-08-04 08:01:15
你们好:
怎样才能编一个程序来显示本机的IP地址(主要用着了哪几个函数).
...全文
40 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
prog_st 2001-08-04
HOWTO: Retrieve the IP Address of the Remote PPP Peer

Q183988


--------------------------------------------------------------------------------
The information in this article applies to:

Microsoft Windows CE, version 2.0

--------------------------------------------------------------------------------


SUMMARY
There is a special host name called "ppp_peer" that serves as a proxy for the IP address of the remote PPP peer that the Windows CE device is connected to. This article demonstrates how to programmatically retrieve the IP address of the remote peer by using Windows Sockets gethostbyname() API with "ppp_peer".



MORE INFORMATION
The following sample code demonstrates how to programmatically retrieve the IP address of the remote peer by using Windows Sockets gethostbyname() API with "ppp_peer".

Sample Code

#include <windows.h>
#include <winsock.h>

#define MAXLEN 64

BOOL GetIpAddress(char * hostname);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdSHow)
{
WSADATA wsadata;
HOSTENT *lpHost=NULL;
char hostname[MAXLEN];
WCHAR msg[128];
int ret, addrlen;
SOCKET sock;
struct sockaddr_in dest, retaddr;


WSAStartup(0x0101, &wsadata);

if ((ret = gethostname(hostname, MAXLEN)) != 0)
{
wsprintf(msg, L"Error calling gethostname: %d", ret);
MessageBox(NULL, msg, NULL, MB_OK);
}
else
{
wsprintf(msg, L"Hostname is: %S", hostname);
MessageBox(NULL, msg, L"Hostname", MB_OK);
}

// Print the local IP address.
MessageBox(NULL, L"Local IP address is", L"Address", MB_OK);
GetIpAddress (hostname);
// Print the remote peer IP address.
MessageBox(NULL, L"Remote IP address is", L"Address", MB_OK);
GetIpAddress ("ppp_peer");

WSACleanup();

return 0;
}



BOOL GetIpAddress(char *hostname)
{
WCHAR msg[128];
HOSTENT *lpHost=NULL;
struct sockaddr_in dest;

lpHost = gethostbyname(hostname);
if (lpHost == NULL)
{
wsprintf(msg, L"gethostbyname failed: %d", WSAGetLastError());
MessageBox(NULL, msg, NULL, MB_OK);
}
else
{
for(int i=0; lpHost->h_addr_list[i] != NULL ;i++)
{
memcpy(&(dest.sin_addr), lpHost->h_addr_list[i],
lpHost->h_length);
wsprintf(msg, L"IP address is: '%S'",
inet_ntoa(dest.sin_addr));
MessageBox(NULL, msg, L"IP Address", MB_OK);
}

}
return 0;
}

Additional query words:

Keywords : kbnetwork kbIP kbSDKPlatform kbWinCE200 kbWinsock kbGrpNet
Issue type : kbhowto
Technology :


Last Reviewed: October 15, 1999
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.




--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
回复
xcex 2001-08-04
不过你其实可以先搜索一下的
回复
darkay 2001-08-04
先用gethostname得到本机器的名字,然后调用gethostbyname得到本机器的相关信息,函数具体怎么用的,你自己查MSDN啦。记得用WSAStartup哦
回复
xcex 2001-08-04
gethostbyname
回复
xcex 2001-08-04
gethostname
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2001-08-04 08:01
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……