在 Windows 和 iOS 上,我目前正在执行以下操作:
//sock is defined as a valid socket.
fd_set socketsRead;
fd_set socketsWrite;
fd_set socketsExcept;
FD_ZERO(&socketsRead);
FD_ADD(sock, &socketsRead);
FD_ZERO(&socketsWrite);
FD_ADD(sock, &socketsWrite);
FD_ZERO(&socketsExcept);
FD_ADD(sock, &socketsExcept);
timeval timeOut;
timeOut.tv_sec = 0;
timeOut.tv_usec = 100;
bool read, write, except;
if (select(sock + 1, &socketsRead, &socketsWrite, &socketsExcept, &timeOut) == SOCKET_ERROR) {
read = false;
write = false;
except = true;
} else {
read = FD_ISSET(sock, &socketsRead);
write = FD_ISSET(sock, &socketsWrite);
except = FD_ISSET(sock, &socketsExcept);
}
if (read) {
long availableData;
ioctlsocket(sock, FIONREAD, &availableData);
if (availableData == 0) {
//Connection is closed.
}
}
在 Android 上,它似乎不起作用。当我建立连接时,它一直认为连接已关闭。也就是说,读取标志被设置,并在建立连接时FIONREAD
返回。0