- int setHost(const char * host)
- {
- int isIp = 0; //return 0 is ip or domain
- int n = sizeof(host);
- for( int i = 0 ;i<sizeof( host ); ++i )
- {
- if( '.' == host[i] )
- continue;
- if( 0 != isalpha(host[i]))
- {
- isIp = 1;
- break;
- }
- }
- if( 0 == isIp )
- {
- strncpy(g_host,host,sizeof(g_host));
- }
- else
- {
- struct hostent *hptr;
- if( NULL == ( hptr = gethostbyname(host) ) )
- {
- printf(" gethostbyname error for host:%s\\n",host);
- return 0;
- }
- char **pptr;
- pptr=hptr->h_addr_list;
- if( NULL != * pptr )
- inet_ntop( hptr->h_addrtype, *pptr, g_host, sizeof(g_host) );
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2707201614890.html
来源: http://www.codesnippet.cn/detail/2707201614890.html