确定Android上的套接字何时关闭

时间:2013-08-16 17:01:04

标签: android c++ linux sockets networking

在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

1 个答案:

答案 0 :(得分:-1)

似乎FIONREAD定义错误 - 它使用的是Windows / iOS常量值而不是Android值。