简体   繁体   中英

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

fd_set fd_read;       
fd_set fd_close;   

plz any help would be really appreciated 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.

You detect a remote close by the return code from recv() , ie inside the handling for readable descriptors. 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); if recv() returns -1, then errno has further information, for example ECONNRESET means that a TCP RST packet was received.

You want to use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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