获取本机所有IP地址
int CMachine :: GetLocalIPs ( std:: vector< CString> & vIPValue)
{ vIPValue. clear ( ) ; int IpNum = 0 ; WSADATA wsaData; int ret = WSAStartup ( MAKEWORD ( 2 , 2 ) , & wsaData) ; if ( ret != 0 ) { return - 1 ; } char hostname[ 256 ] ; ret = gethostname ( hostname, sizeof ( hostname) ) ; if ( ret == SOCKET_ERROR) { return - 1 ; } HOSTENT* host = gethostbyname ( hostname) ; if ( host == NULL ) { return - 1 ; } IpNum = host-> h_length; for ( int i = 0 ; i < IpNum; i++ ) { char ip[ 30 ] ; CString strtmp; in_addr* addr = ( in_addr* ) * host-> h_addr_list; strcpy ( ip, inet_ntoa ( addr[ i] ) ) ; strtmp = ip; vIPValue. push_back ( strtmp) ; } WSACleanup ( ) ; return IpNum;
}
获取局域网内所有IP地址
int CMachine :: GetLanIPs ( std:: vector< CString> & vIPValue)
{ vIPValue. clear ( ) ; int IpNum = 0 ; WSADATA wsaData; int ret = WSAStartup ( MAKEWORD ( 1 , 1 ) , & wsaData) ; if ( ret != 0 ) { return - 1 ; } char hostname[ 256 ] ; gethostname ( hostname, sizeof ( hostname) ) ; CString strHostName; strHostName = hostname; DWORD dwScope = RESOURCE_CONTEXT; NETRESOURCE * NetResource = NULL ; HANDLE hEnum; WNetOpenEnum ( dwScope, NULL , NULL , NULL , & hEnum) ; if ( hEnum) { DWORD Count = 0xFFFFFFFF ; DWORD BufferSize = 2048 ; LPVOID Buffer = new char [ 2048 ] ; WNetEnumResource ( hEnum, & Count, Buffer, & BufferSize) ; NetResource = ( NETRESOURCE * ) Buffer; for ( unsigned int i= 0 ; i< Count; i++ , NetResource++ ) { if ( NetResource-> dwUsage == RESOURCEUSAGE_CONTAINER && NetResource-> dwType == RESOURCETYPE_ANY) { if ( NetResource-> lpRemoteName) { CString strFullName = NetResource-> lpRemoteName; if ( 0 == strFullName. Left ( 2 ) . Compare ( _T ( "\\\\" ) ) ) strFullName = strFullName. Right ( strFullName. GetLength ( ) - 2 ) ; if ( strFullName == strHostName) { continue ; } const char * cchostname = CString2ConstChar ( strFullName) ; HOSTENT* host = gethostbyname ( cchostname) ; delete [ ] cchostname; if ( host != NULL ) { CString strtmp; struct in_addr * ptr; ptr = ( struct in_addr * ) host-> h_addr_list[ 0 ] ; int a = ptr-> S_un. S_un_b. s_b1; int b = ptr-> S_un. S_un_b. s_b2; int c = ptr-> S_un. S_un_b. s_b3; int d = ptr-> S_un. S_un_b. s_b4; strtmp. Format ( _T ( "%d.%d.%d.%d" ) , a, b, c, d) ; vIPValue. push_back ( strtmp) ; IpNum++ ; } } } } } WSACleanup ( ) ; return IpNum;
}
搜索本地可连通IP
int CMachine :: SearchEnableIP ( )
{ std:: vector< CString> IPValue; int IPNum = GetLocalIPs ( IPValue) ; if ( IPNum <= 0 ) { CString strtmp = _T ( "127.0.0.1" ) ; IPValue. push_back ( strtmp) ; IPNum = 1 ; } for ( int i = 0 ; i < IPNum; i++ ) { const char * ccIP = CString2ConstChar ( IPValue. at ( i) ) ; if ( is_access_remote_host ( ccIP) != 0 ) { IPValue. erase ( IPValue. begin ( ) + i) ; IPNum = IPValue. size ( ) ; } delete [ ] ccIP; } if ( IPNum <= 0 ) { return 1 ; } for ( int i = 0 ; i < IPNum; i++ ) { disconnect_redis_server ( ) ; const char * ccIP = CString2ConstChar ( IPValue. at ( i) ) ; if ( ! connect_redis_server ( ccIP, 6379 , 5 ) ) { IPValue. erase ( IPValue. begin ( ) + i) ; IPNum = IPValue. size ( ) ; i-- ; } else { bool bConnected = false ; get_master_dsp_connect_status ( bConnected) ; if ( ! bConnected) { IPValue. erase ( IPValue. begin ( ) + i) ; IPNum = IPValue. size ( ) ; i-- ; } } delete [ ] ccIP; } if ( IPNum <= 0 ) { return 2 ; } for ( int i = 0 ; i < IPNum; i++ ) { const char * ccIP = CString2ConstChar ( IPValue. at ( i) ) ; if ( ! create_master_cmd_client ( ccIP) ) { IPValue. erase ( IPValue. begin ( ) + i) ; IPNum = IPValue. size ( ) ; i-- ; } delete [ ] ccIP; } if ( IPNum <= 0 ) { return 3 ; } for ( int i = 0 ; i < IPNum; i++ ) { const char * ccIP = CString2ConstChar ( IPValue. at ( i) ) ; if ( ! setup_master_subcriber ( ccIP) ) { IPValue. erase ( IPValue. begin ( ) + i) ; IPNum = IPValue. size ( ) ; i-- ; } delete [ ] ccIP; } if ( IPNum <= 0 ) { return 4 ; } notice_exit ( ) ; destory_master_cmd_client ( ) ; disconnect_redis_server ( ) ; char buf[ 30 ] = "" ; if ( ! WritePrivateProfileString ( _T ( "CutterServer" ) , _T ( "IP" ) , IPValue. at ( 0 ) , m_ConfigPath) ) { return 5 ; } return 0 ;
}
搜索局域网内可连通IP
int CMachine :: SearchLanEnableIP ( )
{ std:: vector< CString> IPValue; int IPNum = GetLanIPs ( IPValue) ; if ( IPNum <= 0 ) { return - 1 ; } for ( int i = 0 ; i < IPNum; i++ ) { const char * ccIP = CString2ConstChar ( IPValue. at ( i) ) ; if ( is_access_remote_host ( ccIP) != 0 ) { IPValue. erase ( IPValue. begin ( ) + i) ; IPNum = IPValue. size ( ) ; } delete [ ] ccIP; } if ( IPNum <= 0 ) { return 1 ; } for ( int i = 0 ; i < IPNum; i++ ) { const char * ccIP = CString2ConstChar ( IPValue. at ( i) ) ; if ( ! create_slave_cmd_client ( ccIP) ) { IPValue. erase ( IPValue. begin ( ) + i) ; IPNum = IPValue. size ( ) ; i-- ; } delete [ ] ccIP; } if ( IPNum <= 0 ) { return 2 ; } destory_slave_cmd_client ( ) ; char buf[ 30 ] = "" ; if ( ! WritePrivateProfileString ( _T ( "CutterServer" ) , _T ( "IP2" ) , IPValue. at ( 0 ) , m_ConfigPath) ) { return 3 ; } return 0 ;
}