Network Component
Version 7.19.0
MDK Middleware for IPv4 and IPv6 Networking
|
BSD Socket functions and communication flow. More...
Functions | |
int32_t | socket (int32_t family, int32_t type, int32_t protocol) |
Create a communication endpoint called socket. [thread-safe]. More... | |
int32_t | bind (int32_t sock, const SOCKADDR *addr, int32_t addrlen) |
Assign a local address and port to a socket. [thread-safe]. More... | |
int32_t | listen (int32_t sock, int32_t backlog) |
Set a socket in a listening mode. [thread-safe]. More... | |
int32_t | accept (int32_t sock, SOCKADDR *addr, int32_t *addrlen) |
Accept connect request for a listening socket. [thread-safe]. More... | |
int32_t | connect (int32_t sock, const SOCKADDR *addr, int32_t addrlen) |
Connect a socket to a remote host. [thread-safe]. More... | |
int32_t | send (int32_t sock, const char *buf, int32_t len, int32_t flags) |
Send data on already connected socket. [thread-safe]. More... | |
int32_t | sendto (int32_t sock, const char *buf, int32_t len, int32_t flags, const SOCKADDR *to, int32_t tolen) |
Send data to endpoint node. [thread-safe]. More... | |
int32_t | sendmsg (int32_t sock, const MSGHDR *msg, int32_t flags) |
Send a message to endpoint node. [thread-safe]. More... | |
int32_t | recv (int32_t sock, char *buf, int32_t len, int32_t flags) |
Receive data on already connected socket. [thread-safe]. More... | |
int32_t | recvfrom (int32_t sock, char *buf, int32_t len, int32_t flags, SOCKADDR *from, int32_t *fromlen) |
Receive data from endpoint node. [thread-safe]. More... | |
int32_t | recvmsg (int32_t sock, MSGHDR *msg, int32_t flags) |
Receive a message from a socket. [thread-safe]. More... | |
int32_t | closesocket (int32_t sock) |
Close socket and release socket descriptor. [thread-safe]. More... | |
int32_t | getpeername (int32_t sock, SOCKADDR *name, int32_t *namelen) |
Retrieve IP address and port number of the endpoint node. [thread-safe]. More... | |
int32_t | getsockname (int32_t sock, SOCKADDR *name, int32_t *namelen) |
Retrieve local IP address and port number. [thread-safe]. More... | |
int32_t | ioctlsocket (int32_t sock, long cmd, unsigned long *argp) |
Control IO mode of a socket. [thread-safe]. More... | |
int32_t | setsockopt (int32_t sock, int32_t level, int32_t optname, const char *optval, int32_t optlen) |
Manipulate options for the socket. [thread-safe]. More... | |
int32_t | getsockopt (int32_t sock, int32_t level, int32_t optname, char *optval, int32_t *optlen) |
Retrieve options for the socket. [thread-safe]. More... | |
int32_t | select (int32_t nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) |
Check the status of one or more sockets. [thread-safe]. More... | |
HOSTENT * | gethostbyname (const char *name, int32_t *err) |
Retrieve host IP address from host name. [thread-safe]. More... | |
IN_ADDR | inet_addr (const char *cp) |
Convert from text address to a network address. [thread-safe]. More... | |
int32_t | inet_aton (const char *cp, IN_ADDR *addr) |
Convert from text address to a network address. [thread-safe]. More... | |
const char * | inet_ntoa (IN_ADDR in) |
Convert from network address to a text string. [not_thread-safe]. More... | |
int32_t | inet_pton (int32_t af, const char *src, void *dst) |
Convert from text address to a binary network address. [thread-safe]. More... | |
const char * | inet_ntop (int32_t af, const void *src, char *dst, int32_t size) |
Convert from binary network address to a text string. [thread-safe]. More... | |
BSD Socket functions and communication flow.
The following table shows the available API functions for BSD sockets.
Function | Description |
---|---|
accept | Accepts a connection request queued for a listening socket. |
bind | Assigns a name (local address) to a socket. |
closesocket | Closes an existing socket and releases a socket descriptor. |
connect | Establishes connection between the endpoints on stream sockets. |
gethostbyname | Retrieves host address corresponding to a host name from a host database. |
getpeername | Retrieves the address of the peer to which a socket is connected. |
getsockopt | Retrieves options for the socket. |
getsockname | Retrieves the local address of the socket. |
ioctlsocket | Sets or retrieves some of the operating parameters on a socket. |
listen | Sets the socket in a listen mode. |
recv | Receives incoming data that has been queued for a socket. |
recvfrom | Receives incoming data on a datagram socket. |
recvmsg | Receives a message from a socket. |
select | Tests sockets if ready for reading, writing or have an error pending. |
send | Sends outgoing data on a socket. |
sendto | Sends outgoing data on a datagram socket to destination address. |
sendmsg | Sends a message to destination address. |
setsockopt | Manipulate options for the socket. |
socket | Creates a communication socket. |
inet_addr | Converts address from text address to a network address. |
inet_aton | Converts address from text address to a network address. |
inet_ntoa | Converts address from network address to a text string. |
inet_pton | Converts address from text address to a binary network address. |
inet_ntop | Converts address from binary network address to a text string. |
The image below explains the basic communication flow using BSD sockets with TCP.
The BSD server creates a socket, uses bind to attach that socket to a port, and configures it as a listening socket. This allows the server to receive incoming connection requests. Afterwards, accept is called, which will block the socket, until an incoming connection request is received. When accept returns, the SOCKADDR structure will have been filled out with the originating IP Address and port of the incoming connection. Then, accept creates a new socket, which is then used to receive data until the connection is closed by the other side.
Code Example
Dual-Stack sockets offer the ability to create a single IPv6 socket which can handle both IPv6 and IPv4 communication. For example, a TCP listening socket for IPv6 is created, put into dual-stack mode, and bound to port 1000. The dual-stack socket can accept connections from IPv6 TCP clients and IPv4 TCP clients that connect to port 1000.
The IPv4 address is encoded in the IPv4-mapped IPv6 address with the address prefix 0:0:0:0:0:FFFF, last four bytes of the address are the IPv4 address. The IPv4 address in this format can be used in a dual-stack socket communication. If the IPv4 address is converted to a string address with the function inet_ntop, the result is represented in a hybrid dotted decimal notation. For example, the address 192.168.0.100 is converted to a string:
Code Example
The BSD Client creates a socket calls connect, because TCP requires a negotiated connection. Afterwards, send is called to send the data to the server. Note that bind is never called, because the stack will pick a random port and an appropriate IP address. To finish the communication, the client calls closesocket.
Code Example
int32_t accept | ( | int32_t | sock, |
SOCKADDR * | addr, | ||
int32_t * | addrlen | ||
) |
Accept connect request for a listening socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[out] | addr | structure that will receive IP address and port number.
|
[in,out] | addrlen | length of SOCKADDR structure. |
The function accept accepts a connection request queued for a listening socket. If a connection request is pending, accept removes the request from the queue, and a new socket is created for the connection. The original listening socket remains open and continues to queue new connection requests. The socket sock must be a SOCK_STREAM type socket.
In blocking mode, which is enabled by default, this function waits for a connection request. In non blocking mode, you must call the accept function again if the error code BSD_EWOULDBLOCK
is returned.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument addr is a pointer to the SOCKADDR structure that will receive the connection node IP address and port number.
The argument addrlen is a pointer to the address length. It should initially contain the amount of space pointed to by addr. On return it contains the actual length of the address returned in bytes.
Code Example (see socket)
int32_t bind | ( | int32_t | sock, |
const SOCKADDR * | addr, | ||
int32_t | addrlen | ||
) |
Assign a local address and port to a socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | addr | structure containing local IP address and port. |
[in] | addrlen | length of SOCKADDR structure. |
The function bind assigns a name to an unnamed socket. The name represents the local address and port of the communication endpoint.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument addr is a pointer to the SOCKADDR structure containing the local address and port of the socket. If the port is specified as 0, the Network Core assigns a unique port from the dynamic client port range (ephemeral ports).
The argument addrlen specifies the length of the SOCKADDR structure.
Code Example (see socket)
int32_t closesocket | ( | int32_t | sock | ) |
Close socket and release socket descriptor. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
The function closesocket closes an existing socket and releases the socket descriptor. Further references to sock
fail with BSD_EINVAL
error code.
The argument sock specifies a socket descriptor returned from a previous call to socket.
In blocking mode, which is enabled by default, this function will wait until a socket is closed. In non blocking mode, you must call the closesocket function again if the error code BSD_EWOULDBLOCK
is returned.
Code Example (see socket)
int32_t connect | ( | int32_t | sock, |
const SOCKADDR * | addr, | ||
int32_t | addrlen | ||
) |
Connect a socket to a remote host. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | addr | structure containing remote IP address and port. |
[in] | addrlen | length of SOCKADDR structure. |
The function connect assigns the address of the peer communication endpoint. The function behaves differently according to the type of socket:
SOCK_STREAM: A connection is established between the endpoints.
In blocking mode, which is enabled by default, this function waits for a connection to be established.
In non blocking mode, the function returns the error code BSD_EINPROGRESS
and the connection is established asynchronously. Subsequent calls to connect for the same socket, before the connection is established, return the error code BSD_EALREADY
. When the connection is established, the call to connect returns the error code BSD_EISCONN
.
SOCK_DGRAM: An address filter is established between the endpoints.
The address filter is changed with another connect function call. If the socket is not yet bound, the system implicitly binds to a random dynamic port.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument addr is a pointer to the SOCKADDR structure that contains the endpoint node IP address and port number.
The argument addrlen specifies the length of SOCKADDR structure.
Code Example
HOSTENT * gethostbyname | ( | const char * | name, |
int32_t * | err | ||
) |
Retrieve host IP address from host name. [thread-safe].
[in] | name | host name. |
[out] | err | pointer to where to return error code (NULL for none):
|
The function gethostbyname retrieves host information corresponding to a host name from a host database.
The argument name is a pointer to the null-terminated name of the host to resolve.
The argument err is a pointer to the return error code.
Code Example
int32_t getpeername | ( | int32_t | sock, |
SOCKADDR * | name, | ||
int32_t * | namelen | ||
) |
Retrieve IP address and port number of the endpoint node. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[out] | name | structure that will receive IP address and port number. |
[in,out] | namelen | length of SOCKADDR structure. |
The function getpeername retrieves the address of the peer to which a socket is connected.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument name is a pointer to the SOCKADDR structure. If name is not NULL, the remote end IP address and port number will be filled in.
The argument namelen is a pointer to the address length. It should initially contain the amount of space pointed to by name. On return it contains the actual length of the address returned in bytes.
Code Example
int32_t getsockname | ( | int32_t | sock, |
SOCKADDR * | name, | ||
int32_t * | namelen | ||
) |
Retrieve local IP address and port number. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[out] | name | structure that will receive IP address and port number. |
[in,out] | namelen | length of SOCKADDR structure. |
The function getsockname retrieves the local address for a socket.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument name is a pointer to the SOCKADDR structure. If name is not NULL, the local IP address and port number will be filled in.
The argument namelen is a pointer to the address length. It should initially contain the amount of space pointed to by name. On return it contains the actual length of the address returned in bytes.
Code Example
int32_t getsockopt | ( | int32_t | sock, |
int32_t | level, | ||
int32_t | optname, | ||
char * | optval, | ||
int32_t * | optlen | ||
) |
Retrieve options for the socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | level | level at which the option is defined:
|
[in] | optname | socket option for which the value is to be retrieved:
|
[out] | optval | pointer to the buffer that will receive the option value. |
[in,out] | optlen | input length of buffer, return length of the data. |
The function getsockopt retrieves options for a socket. Options may exist at multiple protocol levels.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument level describes the level at which the option is defined (for example SOL_SOCKET).
The argument optname is the socket option for which the value is to be retrieved and must be defined within the specified level.
The argument optval points to the buffer that will receive the value of the optname.
The argument optlen contains the length of the buffer at the input and returns the length of the option information on the output.
Code Example
IN_ADDR inet_addr | ( | const char * | cp | ) |
Convert from text address to a network address. [thread-safe].
[in] | cp | text address in standard dotted-decimal notation. |
The function inet_addr converts the specified string to an integer value suitable for use as an Internet address.
The argument cp is a pointer to the null-terminated string in the standard IPv4 dotted decimal notation.
The function returns the converted Internet address on success, or INADDR_NONE if failed.
Code Example (see inet_aton)
int32_t inet_aton | ( | const char * | cp, |
IN_ADDR * | addr | ||
) |
Convert from text address to a network address. [thread-safe].
[in] | cp | text address in standard dotted-decimal notation. |
[out] | addr | buffer where the converted IPv4 address is to be stored. |
The function inet_aton converts the specified string to a network address, and stores the address in the structure provided.
The argument cp is a pointer to the null-terminated string in the standard IPv4 dotted decimal notation.
The argument addr is a pointer to the buffer, where the converted address is to be stored.
The function returns 1 if the address is successfully converted, or 0 if the conversion failed.
Code Example
const char * inet_ntoa | ( | IN_ADDR | in | ) |
Convert from network address to a text string. [not_thread-safe].
[in] | in | Internet IPv4 host address to convert. |
The function inet_ntoa converts the specified Internet host address to a string in the standard dotted decimal notation.
The argument in is the Internet host address to be converted.
The function returns a pointer to the null-terminated string in the standard IPv4 dotted decimal notation.
Code Example (see inet_aton)
const char * inet_ntop | ( | int32_t | af, |
const void * | src, | ||
char * | dst, | ||
int32_t | size | ||
) |
Convert from binary network address to a text string. [thread-safe].
[in] | af | address family:
|
[in] | src | binary address in network byte order to be converted. |
[out] | dst | buffer where the converted text address is to be stored. |
[in] | size | size of the buffer, at least:
|
The function inet_ntop converts an address from network format in network byte order to presentation format.
The argument af specifies the address family. Currently, only AF_INET and AF_INET6 are supported.
The argument src is a pointer to the network address. The format of the address is interpreted according to argument af.
The argument dst is a pointer to the buffer where the converted address is to be stored.
The argument size specifies the size of the buffer pointed to by dst. The size must be at least INET_ADDRSTRLEN bytes for an IPv4 address and INET6_ADDRSTRLEN bytes for an IPv6 address.
The function returns a pointer to dst if the address is successfully converted, or NULL if the conversion failed.
Code Example (see inet_pton)
int32_t inet_pton | ( | int32_t | af, |
const char * | src, | ||
void * | dst | ||
) |
Convert from text address to a binary network address. [thread-safe].
[in] | af | address family:
|
[in] | src | text address to be converted. |
[out] | dst | buffer where the converted address is to be stored. |
The function inet_pton converts an address from presentation format to network format in network byte order.
The argument af specifies the address family. Currently, only AF_INET and AF_INET6 are supported.
The argument src is a pointer to the null-terminated presentation format address. The format of the address is interpreted according to argument af.
The argument dst is a pointer to the buffer where the converted address is to be stored.
The function returns 1 if the address is successfully converted, or 0 if the conversion failed.
Code Example
int32_t ioctlsocket | ( | int32_t | sock, |
long | cmd, | ||
unsigned long * | argp | ||
) |
Control IO mode of a socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | cmd | command to perform:
|
[in] | argp | command's parameter. |
The function ioctlsocket controls the I/O mode of a socket. It can be used on any socket in any state to set or retrieve some operating parameters of the socket.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument cmd specifies a command to perform on a socket. The following commands are supported:
Command | Description |
---|---|
FIONBIO | Sets the blocking or non-blocking socket I/O mode |
FIO_DELAY_ACK | Sets the delay-ack or normal mode for the stream socket |
FIO_KEEP_ALIVE | Sets the keep-alive or timeout mode for the stream socket |
FIO_FLOW_CTRL | Sets the receive flow-control or normal mode for the stream socket |
The argument argp specifies a pointer to the command's parameter.
For the command FIONBIO the argument values are:
*argp value | I/O mode |
---|---|
0 | Blocking mode |
nonzero | Non-blocking mode |
For the command FIO_DELAY_ACK the argument values are:
*argp value | Socket mode | Description |
---|---|---|
0 | Normal mode | Waits for an ACK after each sending packet |
nonzero | Delay-ack mode | Eliminates the delayed acknowledge impact and improves the performance for applications sending large amount of data |
For the command FIO_KEEP_ALIVE the argument values are:
*argp value | Socket mode | Description |
---|---|---|
0 | Timeout mode | After a timeout a stream socket is disconnected |
nonzero | Keep-alive mode | Stream socket sends keep alive segments on timeout to keep a connection alive |
For the command FIO_FLOW_CTRL the argument values are:
*argp value | Socket mode | Description |
---|---|---|
0 | Normal mode | Stream socket dumps excess data, if the receiving buffer is full |
nonzero | Flow-control mode | Stream socket controls the window size and stops the transmitter, if the receiving buffer is full |
Code Example
int32_t listen | ( | int32_t | sock, |
int32_t | backlog | ||
) |
Set a socket in a listening mode. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | backlog | number of connection requests that can be accepted. |
The function listen sets the specified socket to listening mode. Before calling the listen function, the bind function must be called.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument backlog specifies a maximum number of connection requests that can be accepted.
This is different from the standard BSD listening function. The reason for this is that BSD sockets in the network component are just a layer on top of native TCP Socket. TCP sockets, however, are static, meaning that they are defined at compile time and are not dynamically allocated objects.
Code Example (see socket)
int32_t recv | ( | int32_t | sock, |
char * | buf, | ||
int32_t | len, | ||
int32_t | flags | ||
) |
Receive data on already connected socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[out] | buf | pointer to application data buffer to store the data to. |
[in] | len | size of application data buffer (in bytes). |
[in] | flags | message flags:
|
The function recv receives incoming data that has been queued for the socket. This function can be used with both SOCK_STREAM and SOCK_DGRAM type sockets. It reads as much information as currently available up to the size of the buffer specified. The excess data is treated differently according to the type of socket:
SOCK_STREAM: The excess data is buffered internally.
The application can retrieve all data by multiple calls of recv function.
SOCK_DGRAM: The excess message data is silently discarded.
The data is extracted from the first enqueued datagram. If the datagram or message is larger than the buffer specified, the buffer is filled with the first part of the datagram, the excess data of this datagram is silently discarded. All subsequent messages that are enqueued, are retained. The message size is currently limited to 2000 characters. You need to enable IP Fragmentation in the interface configuration so that you can receive 2000 byte datagram messages.
In blocking mode, which is enabled by default, this function waits for received data. In non blocking mode, you must call the recv function again if the error code BSD_EWOULDBLOCK
is returned.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument buf is a pointer to the application data buffer for storing the data to.
The argument len specifies the size of the application data buffer.
The argument flags specifies the message flags:
Flag | Description |
---|---|
MSG_DONTWAIT | The function returns with error code BSD_EWOULDBLOCK or number of bytes received instead of blocking the socket. |
MSG_PEEK | The function peeks at incoming data. The data is copied into the buffer, but is not removed from the input queue. |
Code Example (see socket)
int32_t recvfrom | ( | int32_t | sock, |
char * | buf, | ||
int32_t | len, | ||
int32_t | flags, | ||
SOCKADDR * | from, | ||
int32_t * | fromlen | ||
) |
Receive data from endpoint node. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[out] | buf | pointer to application data buffer to store the data to. |
[in] | len | size of application data buffer (in bytes). |
[in] | flags | message flags:
|
[out] | from | structure that will receive IP address and port number.
|
[in,out] | fromlen | length of SOCKADDR structure. |
The function recvfrom is used to receive data that has been queued for a socket. It is normally used to receive messages on SOCK_DGRAM socket type, but can also be used to receive a reliable, ordered stream of data on a connected SOCK_STREAM socket type. It reads as much information as currently available up to the size of the buffer specified. The excess data is treated differently according to the type of socket:
SOCK_STREAM: The excess data is buffered internally.
The application can retrieve all data by multiple calls of recvfrom function.
SOCK_DGRAM: The excess message data is silently discarded.
The data is extracted from the first enqueued datagram. If the datagram or message is larger than the buffer specified, the buffer is filled with the first part of the datagram, the excess data of this datagram is silently discarded. All subsequent messages that are enqueued, are retained. The message size is currently limited to 2000 characters. You need to enable IP Fragmentation in the interface configuration so that you can receive 2000 byte datagram messages.
In blocking mode, which is enabled by default, this function waits for received data. In non blocking mode, you must call the recvfrom function again if the error code BSD_EWOULDBLOCK
is returned.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument buf is a pointer to the application data buffer for storing the data to.
The argument len specifies the size of the application data buffer.
The argument flags specifies the message flags:
Flag | Description |
---|---|
MSG_DONTWAIT | The function returns with error code BSD_EWOULDBLOCK or number of bytes received instead of blocking the socket. |
MSG_PEEK | The function peeks at incoming data. The data is copied into the buffer, but is not removed from the input queue. |
The argument from is a pointer to the SOCKADDR structure. If from is not NULL, the source IP address and port number of the datagram will be filled in.
The argument fromlen is a pointer to the address length. It should initially contain the amount of space pointed to by from. On return it contains the actual length of the address returned in bytes.
If the socket type is SOCK_STREAM, arguments from and fromlen are ignored.
Code Example
int32_t recvmsg | ( | int32_t | sock, |
MSGHDR * | msg, | ||
int32_t | flags | ||
) |
Receive a message from a socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in,out] | msg | pointer to MSGHDR structure containing:
|
[in] | flags | message flags:
|
The function recvmsg is used to receive a message from a SOCK_STREAM or SOCK_DGRAM socket. It is normally used with SOCK_DGRAM sockets because it permits the application to retrieve the source address of received data.
In blocking mode, which is enabled by default, this function waits for received data. In non blocking mode, you must call the recvmsg function again if the error code BSD_EWOULDBLOCK
is returned.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument msg is a pointer to the msghdr structure, containing both the buffer to store the source address and the buffers for the incoming message.
Macro name | Description |
---|---|
CMSG_FIRSTHDR(mhdr) | Returns a pointer to the first CMSGHDR structure in the mhdr. |
CMSG_NXTHDR(mhdr,cmsg) | Returns a pointer to the CMSGHDR structure describing the next ancillary data object. |
CMSG_DATA(cmsg) | Returns a pointer to the data of ancillary data object. |
CMSG_ALIGN(len) | Rounds the argument up to the next even multiple of 4 bytes. |
CMSG_LEN(len) | Gives the length of an ancillary data object without padding. |
CMSG_SPACE(len) | Gives the length of an ancillary data object including the padding needed. |
Flag | Description |
---|---|
MSG_TRUNC | Normal data was truncated, excess bytes are discarded in case of SOCK_DGRAM sockets. |
MSG_CTRUNC | Control data was truncated, ancillary data buffer is too small. |
The argument flags specifies the message flags:
Flag | Description |
---|---|
MSG_DONTWAIT | The function returns with error code BSD_EWOULDBLOCK or number of bytes received instead of blocking the socket. |
MSG_PEEK | The function peeks at incoming data. The data is copied into the buffer, but is not removed from the input queue. |
Code Example
int32_t select | ( | int32_t | nfds, |
fd_set * | readfds, | ||
fd_set * | writefds, | ||
fd_set * | errorfds, | ||
struct timeval * | timeout | ||
) |
Check the status of one or more sockets. [thread-safe].
[in] | nfds | range of sockets to be checked. |
[in,out] | readfds | pointer to the set of sockets to check for read.
|
[in,out] | writefds | pointer to the set of sockets to check for write.
|
[in,out] | errorfds | pointer to the set of sockets to check for error.
|
[in] | timeout | pointer to maximum time for select to wait.
|
The function select indicates which of the specified sockets is ready for reading, ready for writing, or has an error condition pending. If the specified condition is false for all of the specified sockets, select blocks, up to the specified timeout interval, until the specified condition is true for at least one of the specified sockets.
The argument nfds specifies the range of sockets to be tested. The select function tests sockets in the range of 0 to nfds-1.
The argument readfds specifies the set of sockets to be checked for being ready to read on input, and on output indicates which sockets are ready to read. If the value of readfds is NULL, the sockets are not checked for being ready to read.
The argument writefds specifies the set of sockets to be checked for being ready to write on input, and on output indicates which sockets are ready to write. If the value of writefds is NULL, the sockets are not checked for being ready to write.
The argument errorfds specifies the set of sockets to be checked for error conditions pending on input, and on output indicates which sockets have error condition pending. If the value of errorfds is NULL, the sockets are not checked for error condition pending.
On exit, each set of sockets is modified in place to indicate which sockets actually changed status. Thus, if using select within a loop, the sets must be reinitialized before each call.
The argument timeout specifies a maximum interval to wait for the selection to complete. If the value of timeout is NULL, the select function blocks until an event occurs. If both members of the timeout structure are 0, select function does not block.
The set of sockets of type fd_set can be initialized and tested with the following macros:
Macro name | Description |
---|---|
FD_SET(fd, set) | Sets the bit for the socket fd in the set of sockets specified with set. |
FD_CLR(fd, set) | Clears the bit for the socket fd in the set of sockets specified with set. |
FD_ISSET(fd, set) | Returns a nonzero value if the bit for the socket fd is set in the set of sockets specified with set, and 0 otherwise. |
FD_ZERO(set) | Initialises the set of sockets set to have zero bits for all socket descriptors. |
Code Example
int32_t send | ( | int32_t | sock, |
const char * | buf, | ||
int32_t | len, | ||
int32_t | flags | ||
) |
Send data on already connected socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | buf | pointer to application data buffer to transmit. |
[in] | len | length of data (in bytes). |
[in] | flags | message flags:
|
The function send is used to send data on an already connected socket. This function is normally used to send a reliable, ordered stream of data bytes on a SOCK_STREAM socket type. It can also be used to send datagrams on a SOCK_DGRAM socket types.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument buf is a pointer to the application data buffer containing data to transmit. The buffer data length is not limited in size. If the data length is too large for one packet, the send function will fragment the data and send it in several successive data packets:
The argument len specifies the length of data in bytes. The argument flags specifies the message flags:
Flag | Description |
---|---|
MSG_DONTWAIT | The function returns with error code BSD_EWOULDBLOCK or number of bytes sent instead of blocking the socket |
Return value, when positive, represents the number of bytes sent, which can be less than len.
Code Example (see connect)
int32_t sendmsg | ( | int32_t | sock, |
const MSGHDR * | msg, | ||
int32_t | flags | ||
) |
Send a message to endpoint node. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | msg | pointer to MSGHDR structure containing:
|
[in] | flags | message flags:
|
The function sendmsg is used to send a message from a SOCK_STREAM or SOCK_DGRAM socket. It is normally used with SOCK_DGRAM sockets because it permits the application to specify the destination address where the message is to be sent to.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument msg is a pointer to the msghdr structure, containing both the destination address and the buffers for the outgoing message.
The argument flags specifies the message flags.
Code Example
int32_t sendto | ( | int32_t | sock, |
const char * | buf, | ||
int32_t | len, | ||
int32_t | flags, | ||
const SOCKADDR * | to, | ||
int32_t | tolen | ||
) |
Send data to endpoint node. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | buf | pointer to application data buffer to transmit. |
[in] | len | length of data (in bytes). |
[in] | flags | message flags:
|
[in] | to | structure containing remote IP address and port. |
[in] | tolen | length of SOCKADDR structure. |
The function sendto is used to send data. It is normally used to send messages on a SOCK_DGRAM socket type, but can also be used to send data on a connected SOCK_STREAM socket type.
If the socket type is SOCK_DGRAM and the socket is not yet bound, the system implicitly binds to a random dynamic port.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument buf is a pointer to the application data buffer containing data to transmit. The buffer data length is not limited in size. If the data length is too large for one packet, the sendto function will fragment the data and send it in several successive data packets:
The argument len specifies the length of data in bytes. The argument flags specifies the message flags:
Flag | Description |
---|---|
MSG_DONTWAIT | The function returns with error code BSD_EWOULDBLOCK or number of bytes sent instead of blocking the socket |
The argument to is a pointer to the SOCKADDR structure that contains the endpoint node IP address and port number.
The argument tolen specifies the length of SOCKADDR structure.
If the socket type is SOCK_STREAM, arguments to and tolen are ignored.
Return value, when positive, represents the number of bytes sent, which can be less than len.
Code Example
int32_t setsockopt | ( | int32_t | sock, |
int32_t | level, | ||
int32_t | optname, | ||
const char * | optval, | ||
int32_t | optlen | ||
) |
Manipulate options for the socket. [thread-safe].
[in] | sock | socket descriptor obtained with socket. |
[in] | level | level at which the option is defined:
|
[in] | optname | socket option for which the value is to be set:
|
[in] | optval | pointer to the buffer containing the option value. |
[in] | optlen | size of the buffer containing the option value. |
The function setsockopt sets options for a socket. Options may exist at multiple protocol levels.
The argument sock specifies a socket descriptor returned from a previous call to socket.
The argument level describes the level at which the option is defined (for example SOL_SOCKET).
The argument optname is the socket option for which the value is to be set. The optname argument must be a socket option defined within the specified level.
The argument optval points to the buffer containing the value of the optname.
The argument optlen tells the exact length of the option.
Code Example
int32_t socket | ( | int32_t | family, |
int32_t | type, | ||
int32_t | protocol | ||
) |
Create a communication endpoint called socket. [thread-safe].
[in] | family | address family:
|
[in] | type | connection type of a socket:
|
[in] | protocol | protocol type:
|
The function socket creates a communication endpoint called a socket.
The argument family specifies the address family. Currently, only AF_INET and AF_INET6 address families are supported. By default, the IPv4 socket operates over the IPv4 protocol, the IPv6 socket operates over the IPv6 protocol. To convert an IPv6 socket to a dual-stack socket, the setsockopt function must be called with the IPV6_V6ONLY socket option to set this value to zero before the socket is bound to an IP address. When communicating with a dual-stack socket, the socket can operate via both, IPv4 or IPv6.
The argument type specifies the communication semantics. The following are the currently supported types:
Type | Description |
---|---|
SOCK_STREAM | Provides a reliable connection based data stream that is full-duplex |
SOCK_DGRAM | Provides connectionless communication that is unreliable |
The argument protocol specifies the protocol that must be used with socket type:
Protocol | Description |
---|---|
IPPROTO_TCP | Must be used with SOCK_STREAM socket type |
IPPROTO_UDP | Must be used with SOCK_DGRAM socket type |
0 | The system selects a matching protocol for the socket type |
Code Example (Stream Socket)
Code Example (Datagram Socket)