在代码中:
if ((host = (struct hostent*) gethostbyname(address) ) == 0) // address is a string
在 4.5.x gcc 上交叉编译(通用臂架构)时我收到警告:
(.text+0x1558): warning: gethostbyname is obsolescent, use getnameinfo() instead.
获取名称信息是:
int WSAAPI getnameinfo(
__in const struct sockaddr FAR *sa,
__in socklen_t salen,
__out char FAR *host,
__in DWORD hostlen,
__out char FAR *serv,
__in DWORD servlen,
__in int flags
);
它有更多参数......我对此感到困惑,我只需要它像 gethostbyname 一样工作。要传递什么参数以使其像使用 gethostbyname 一样简单愚蠢?
最后这是我的尝试:
struct sockaddr_in servAddr;
struct hostent *host; /* Structure containing host information */
/* open socket */
if ((handle = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
return LILI_ERROR;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(address.ptr());
servAddr.sin_port = htons(port);
char servInfo[NI_MAXSERV];
if ( ( host = (hostent*) getnameinfo(
(struct sockaddr *) &servAddr
,sizeof (struct sockaddr)
,address.ptr(), address.size()
,servInfo, NI_MAXSERV
,NI_NUMERICHOST | NI_NUMERICSERV ) ) == 0)
return LILI_ERROR;
if (::connect(handle, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
return LILI_ERROR;
它编译良好,启动时没有分段错误,但我无法将我的服务器与它连接:(