IP
转载请注明来源:https://www.cnblogs.com/hookjc/
获取本机IP
//#include<winsock2.h>
//连接 ws2_32.lib
void CCWebClientDlg::InitSocket()
{
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
CString ip;
int err;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 1, 1 );//版本号1.1
//1.加载套接字库
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
//判断是否我们请求的winsocket版本,如果不是
//则调用WSACleanup终止winsocket的使用并返回
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
WSACleanup( );
return;
}///...if
if ( err == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
SetDlgItemText(IDC_EDIT_PORT,name);//name里是本机名
SetDlgItemText(IDC_IPADDRESS1,ip); //ip中是本机IP
获取局域网IP
Receiver.c源代码源代码
|
|
本实例对IP多播通信进行了探讨实例源代码由Sender和Receiver两部分组成Sender用户从控制台上输入多播发送数据Receiver端都要求加入同一个多播组完成接收Sender发送多播数据