//#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