在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上,它似乎不起作用。当我建立连接时,它一直认为连接是关闭的。也就是说,read标志被设置,并且当建立连接时FIONREAD返回0右边。

最佳答案

似乎FIONREAD的定义不正确-它使用的是windows/ios常量值,而不是android常量值。

10-08 02:51