Pregunta

¿Cómo no se ha establecido una bandera ya establecido usando fcntl?

Por ejemplo, Puedo fijar el zócalo para el modo sin bloqueo utilizando

fcntl(sockfd, F_SETFL, flags | O_NONBLOCK)

Ahora, quiero desarmar el indicador O_NONBLOCK.

He intentado fcntl (sockfd, F_SETFL, banderas | ~ O_NONBLOCK). Me dio error EINVAL

¿Fue útil?

Solución

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

No probado, pero espero que esto ayude. : -)

Otros consejos

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

Si lo hace así, el O_NONBLOCK ya establecido se desarmará. aquí, banderas contiene las banderas que desea desarmar. Después de terminar la operación AND (Y), de nuevo hay que establecer el indicador utilizando el valor en val. Espero que esto le ayudará.

El siguiente código desarmar una bandera, por ejemplo, la bandera 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 */
        }
}

Regards

Probado desarmar todas las banderas:

fcntl(sockfd, F_SETFL, 0);

También O-ción con las banderas ~O_NONBLOCK no sirve de nada, es necesario Y él, ya que lo que queremos es desarmar el bit (s) O_NONBLOCK.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top