简体   繁体   English

FD_SET 问题/网络

[英]FD_SET Problem/Networking

hi right now im trying to know if a client or the server disconnected and then send an error but i cant seem to make it work and ive got no idea how to do this so i really need help plz嗨,现在我想知道客户端或服务器是否断开连接,然后发送错误,但我似乎无法让它工作,我不知道该怎么做,所以我真的需要帮助

here's my code这是我的代码

    #ifdef _WIN32 || _WIN64
            if(select(0,&fd_read,NULL,&fd_close,&time)==-1){
                printf("Error in Select()");
                return 0;
            }
    #else
            if(select(sockMax + 1,&fd_read,NULL,&fd_close,&time)==-1){
                printf("Error in Select()");
                return 0;
            }
    #endif

 if(FD_ISSET(socklisten,&fd_read)){

        }
        else
        {
            dopack(&pkt);
            send(socklisten, (char*)&pkt, sizeof(pack), 0);
        }


//this is where the error shows -----------
        if(FD_SET(socklisten,&fd_close))
        {
            backtoMenu = true;
        }

        FD_ZERO(&fd_leer);

        FD_SET(sockEscucha,&fd_leer);

The error says expected primary-expression before 'do' so yeah i've got no idea what that means and just in case this is how im declaring fd_read and fd_close该错误在“do”之前表示预期的主要表达式,所以是的,我不知道这意味着什么,以防万一我是这样声明 fd_read 和 fd_close

fd_set fd_read;       
fd_set fd_close;   

plz any help would be really appreciated tyvm请提供任何帮助将不胜感激 tyvm

What Richard said, in addition, the third fd_set passed to select() is not about closed FDs, but rather about some exceptional condition that requires special attention (which exactly, is defined by the underlying driver, for example, TCP sockets use it for "urgent" data.此外,Richard 所说的,第三个传递给select()fd_set不是关于关闭的 FD,而是关于一些需要特别注意的异常情况(确切地说,是由底层驱动程序定义的,例如 TCP sockets 将其用于“紧急”数据。

You detect a remote close by the return code from recv() , ie inside the handling for readable descriptors.您通过recv()的返回代码检测到远程关闭,即在可读描述符的处理中。 If recv() on a stream socket returns 0, the remote side has closed the connection (with TCP, you can still send data as only one direction is closed);如果 stream 套接字上的recv()返回 0,则远程端已关闭连接(使用 TCP,您仍然可以发送数据,因为只有一个方向关闭); if recv() returns -1, then errno has further information, for example ECONNRESET means that a TCP RST packet was received.如果recv()返回 -1,则errno有更多信息,例如 ECONNRESET 表示收到了 TCP RST 数据包。

You want to use if(FD_ISSET(...你想使用 if(FD_ISSET(...

To determine if a client has closed, you want to read from an active readfd and see if the read returns zero.要确定客户端是否已关闭,您需要从活动的 readfd 中读取并查看读取是否返回零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM