Makenshi
Makenshi

Reputation: 1053

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

Upvotes: 0

Views: 468

Answers (2)

Richard Pennington
Richard Pennington

Reputation: 19965

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.

Upvotes: 0

Simon Richter
Simon Richter

Reputation: 29598

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(), i.e. 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.

Upvotes: 2

Related Questions