Domanda

Come faccio a disinserire una bandiera già impostato utilizzando fcntl?

Per esempio Posso impostare il socket in modalità non bloccante usando

fcntl(sockfd, F_SETFL, flags | O_NONBLOCK)

Ora, voglio disinserire la bandiera O_NONBLOCK.

Ho cercato fcntl (sockfd, F_SETFL, bandiere | ~ O_NONBLOCK). Mi ha dato l'errore EINVAL

È stato utile?

Soluzione

int oldfl;
oldfl = fcntl(sockfd, F_GETFL);
if (oldfl == -1) {
    /* handle error */
}
fcntl(sockfd, F_SETFL, oldfl & ~O_NONBLOCK);

Non testato, ma spero che questo aiuta. : -)

Altri suggerimenti

val = fcntl(fd, F_GETFL, 0);
flags = O_NONBLOCK;
val &= ~flags;
fcntl(fd,F_SETFL,val);

Se lo fai come questo, l'O_NONBLOCK già impostato verrà disinserito. qui, bandiere contiene i quali bandiere che si desidera disinserire. Dopo aver terminato l'operazione di AND (&), ancora una volta è necessario impostare il flag utilizzando il valore in val. Spero che questo vi aiuterà.

Il seguente codice disinserire una bandiera, per esempio, la bandiera O_NONBLOCK:

if ((flags = fcntl(fileno(sockfd), F_GETFL, 0)) < 0) {
    perror("error on F_GETFL");
}
else {
    flags &= ~O_NONBLOCK;
    if (fcntl(fileno(sockfd), F_SETFL, flags) < 0) {
        perror("error on F_SETFL");
    }
    else {
        /* O_NONBLOCK set without errors. continue from here */
        }
}

Saluti

Provato disinserimento tutte le bandiere:

fcntl(sockfd, F_SETFL, 0);

Anche O-ing le bandiere con ~O_NONBLOCK è di alcuna utilità, è necessario e che, dal momento che quello che vuoi è di disinserire il bit O_NONBLOCK (s).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top