Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 选择非阻塞套接字中的函数_C++_Winsock - Fatal编程技术网

C++ 选择非阻塞套接字中的函数

C++ 选择非阻塞套接字中的函数,c++,winsock,C++,Winsock,我正在构建一个在线游戏客户端,当我尝试连接到一个离线服务器时,我的客户端会冻结,因此我想使用适合游戏的非阻塞套接字,因为连接到服务器时还需要执行其他任务 使用非阻塞套接字时,connect函数总是返回相同的值,而不管结果如何,因此这里的人建议使用select函数来查找连接请求的结果 (连接前设置非阻塞插座) (设置插座组) select函数总是返回-1,为什么?当select()返回-1(SOCKET\u ERROR)时,使用WSAGetLastError()找出失败的原因 如果当selec

我正在构建一个在线游戏客户端,当我尝试连接到一个离线服务器时,我的客户端会冻结,因此我想使用适合游戏的非阻塞套接字,因为连接到服务器时还需要执行其他任务

使用非阻塞套接字时,
connect
函数总是返回相同的值,而不管结果如何,因此这里的人建议使用
select
函数来查找连接请求的结果

(连接前设置非阻塞插座)


(设置插座组)


select
函数总是返回-1,为什么?

select()
返回-1(
SOCKET\u ERROR
)时,使用
WSAGetLastError()
找出失败的原因

如果当
select()
退出时套接字处于
Err
设置中,请使用
getsockopt(SOL\u socket,SO\u ERROR)
检索套接字错误代码,该代码告诉您为什么
connect()
失败

if(iResult)
对任何非零值(包括-1)计算为true。您需要改用
if(iResult>0)
,因为
iResult
将报告在任何
fd\u集合中发出信号的套接字数量,超时时为0,故障时为-1

请尝试类似以下内容:

u_long iMode = 1;
if (ioctlsocket(hSocket, FIONBIO, &iMode) == SOCKET_ERROR)
{
    int errCode = WSAGetLastError();
    // use errCode as needed...
    message_login("Error", "Can't set socket to non-blocking, error: ..."); // however you supply a variable value to your message...
}


写入与客户端相同。写入?Err与客户端相同。Err?hSocket来自哪里?如果iResult==0,则错误消息不正确。如果
iResult===-1
=>是,则还应检查
WSAGetLastError
以查找错误代码,因为它不在一个文件中,因此与我所说的相同,但它始终返回相同的内容您以前调用WSASTARTP的ID?
FD_ZERO(&Write);
FD_ZERO(&Err);
FD_SET(hSocket, &Write);
FD_SET(hSocket, &Err);
TIMEVAL Timeout;

int TimeoutSec = 10; // timeout after 10 seconds
Timeout.tv_sec = TimeoutSec;
Timeout.tv_usec = 0;
int iResult = select(0,     //ignored
                     NULL,      //read
                     &(client.Write),    //Write Check
                     &(client.Err),      //Error Check
                     &Timeout);

if(iResult)
{
}
else
{
    message_login("Error","Can't connect to the server");
}
u_long iMode = 1;
if (ioctlsocket(hSocket, FIONBIO, &iMode) == SOCKET_ERROR)
{
    int errCode = WSAGetLastError();
    // use errCode as needed...
    message_login("Error", "Can't set socket to non-blocking, error: ..."); // however you supply a variable value to your message...
}
if (connect(client.hSocket, ...) == SOCKET_ERROR)
{
    int errCode = WSAGetLastError();
    if (errCode != WSAEWOULDBLOCK)
    {
        // use errCode as needed...
        message_login("Error", "Can't connect to the server, error: ..."); // however you supply a variable value...
    }
    else
    {
        // only in this condition can you now use select() to wait for connect() to finish...
    }
}
TIMEVAL Timeout;

int TimeoutSec = 10; // timeout after 10 seconds
Timeout.tv_sec = TimeoutSec;
Timeout.tv_usec = 0;

int iResult = select(0,     //ignored
                     NULL,      //read
                     &(client.Write),    //Write Check
                     &(client.Err),      //Error Check
                     &Timeout);

if (iResult > 0)
{
    if (FD_ISSET(client.hSocket, &(client.Err)))
    {
        DWORD errCode = 0;
        int len = sizeof(errCode);
        if (getsockopt(client.hSocket, SOL_SOCKET, SO_ERROR, (char*)&errCode, &len) == 0)
        {
             // use errCode as needed...
             message_login("Error", "Can't connect to the server, error: ..."); // however you supply a variable value to your message...
        }
        else
            message_login("Error", "Can't connect to the server, unknown reason");
    }
    else
        message_login("Success", "Connected to the server");
}
else if (iResult == 0)
{
    message_login("Error", "Timeout connecting to the server");
}
else
{
    int errCode = WSAGetLastError();
    // use errCode as needed...
    message_login("Error", "Can't connect to the server, error: ..."); // however you supply a variable value to your message...
}