The operating characteristics
of sockets can be controlled with the fcntl() call.
Note: COMMAND
values that are supported by the UNIX Systems Services fcntl() callable service are also supported.
#include <manifest.h>
#include <socket.h>
#include <bsdtypes.h>
#include <fcntl.h>
int fcntl(int s, int cmd, int arg)
- Parameter
- Description
- s
- Socket descriptor
- cmd
- Command to perform
- arg
- Data associated with cmd
The operations
to be controlled are determined by
cmd.
The
arg parameter is a variable, the meaning
of which depends on the value of the
cmd parameter. The following commands are valid fcntl() commands:
- Command
- Description
- F_SETFL
- Sets the status flags of socket descriptor s. (One flag, FNDELAY, can be set.)
- F_GETFL
- Returns the status flags of socket descriptor s. (One flag, FNDELAY, can be queried.)
The FNDELAY flag
marks s as being in nonblocking mode. If
data is not present on calls that can block [read(), readv(), and
recv()] the call returns with -1, and errno is set to EWOULDBLOCK.
Note: This function does not reject other values that
might be rejected downstream.
Return values
For the
F_GETFL command, the return value
is the flags, set as a bit mask. For the
F_SETFL command, the value 0 indicates success; the value -1 indicates an
error. Errno identifies the specific error.
- Errno
- Description
- EBADF
- The s parameter is not a valid socket
descriptor.
- EINVAL
- The arg parameter is not a valid flag,
or the command is not a valid command.
Example
int s;
int rc;
int flags;
⋮
/* Place the socket into nonblocking mode */
rc = fcntl(s, F_SETFL, FNDELAY);
/* See if asynchronous notification is set */
flags = fcntl(s, F_GETFL, 0);
if (flags & FNDELAY)
/* it is set */
else
/* it is not */
Related calls
ioctl(), getsockopt(), setsockopt(),
socket()