我遇到了 inet_ntop 失败并将 errno 设置为“协议不支持的地址系列”的问题。
接受代码:
//Wait for connection from client
Network.RemoteSock = accept(Network.LocalSock, (struct sockaddr *) &Network.RemoteAddr, &Network.RemoteAddr_Size);
if(Network.RemoteSock == -1) {
Die("accept Failed In Webserver: %s\n", strerror(errno));
}
//Set socket send timeout
if(setsockopt(Network.RemoteSock, SOL_SOCKET, SO_SNDTIMEO, (char *)&Settings.Send_Timeout, sizeof(Settings.Send_Timeout)) == -1) {
Die("setsockopt Failed In Webserver: %s\n", strerror(errno));
}
//Set socket recv timeout
if(setsockopt(Network.RemoteSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&Settings.Recv_Timeout, sizeof(Settings.Recv_Timeout)) == -1) {
Die("setsockopt Failed In Webserver: %s\n", strerror(errno));
}
//Get remote address
if(inet_ntop(Network.RemoteAddr.ss_family, get_in_addr((struct sockaddr *) &Network.RemoteAddr), Address, sizeof(Address)) == NULL) {
Die("inet_ntop Failed In Webserver: %s\n", strerror(errno));
}
网络定义:
struct _Network {
int LocalSock, RemoteSock;
struct sockaddr_storage RemoteAddr;
socklen_t RemoteAddr_Size;
} Network;
检查 GDB,我看到 Network.RemoteAddr.ss_family 在调用 inet_ntop 之前为 0:
(gdb) print Network.RemoteAddr
$1 = {ss_family = 0, __ss_align = 0, __ss_padding = '\000' <repeats 111 times>}
不知道 ss_family 的有效值是什么,我不知道这是否意味着接受调用失败 - 但我怀疑这就是问题所在。
这可能是某个地方的一些愚蠢问题,但我就是看不到它,有什么想法吗?
在此先感谢 - 亚历克斯