久久首页
免费软件
网络时尚
网络学堂
专题讨论
网络资源
网络游戏
网络文档
网络产品
网站导航: 首页 > 网络编程 > VC/C++ 学堂讨论 | 推荐文章

获得本机主机名和IP地址


2004-05-27 航一 
 
假设你有了名为My的对话框工程.有一个按钮并有响应的程序:如OnButton1();
BOOL CListCtrl1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
AfxSocketInit(NULL);//支持Socket.若在向导是没选Support Socket,这就的加.还要加#include <afxsock.h>在StdAfx.h中.

.......
// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CListCtrl1Dlg::OnButton1()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );

if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{

if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}

WSACleanup( );
}
AfxMessageBox(name);//name里是本机名
AfxMessageBox(ip); //ip中是本机IP
}

 
发表评论 推荐文章 关闭窗口
 相关内容
- 如何得到网卡编号(MAC地址)? 2004-05-27
- 如何列出所有网上邻居? 2004-05-27
- Socket中如何设置连接超时 2004-05-27
- IP包过滤 2004-05-27
- VC网络程序设计二则 2004-05-27
 
 学堂分类
网络知识操作系统
网络应用网络服务
网页开发网络编程
数据存储网络设备
 文章搜索
 
网站简介 | 广告服务 | 编程支持 | 设计服务 | 诚邀合作 | 访客留言
Copyright © 1999-2004 99NET. All Rights Reserved
版权所有 久久网络工作室
Email:webmaster@99net.net