User`s guide
KwikNet TCP/IP Sockets
K
A
DAK
183
Non-Blocking Sockets
When operations are performed using a socket, the caller requesting the action is usually
forced to wait until the operation completes. The socket is said to be in blocking mode.
KwikNet offers an alternative mode of operation in which the socket action is allowed to
proceed without blocking the caller. In this case, the socket is said to be in non-blocking
mode.
When a socket is created it is placed in blocking mode. Thereafter, your application can
call the sockets procedure kn_setsockopt() to alter the operating mode of the socket.
KwikNet socket option SO_NONBLOCK can be used to place the socket in non-blocking
mode. The same option can be used to restore the socket to blocking mode. Since
SO_NONBLOCK is a unique KwikNet option, it is flagged as non-standard in the summary of
sockets options presented in Chapter 5.3.
KwikNet Error Codes
KwikNet socket procedures return a positive value or 0 if the call is successful or -1 if the
call fails. Procedures which return a socket descriptor return a non-negative value if the
call is successful.
Standard sockets procedures also return additional error information in the UNIX global
variable errno. This is the same variable which many standard C libraries use to save
error results. However, KwikNet must coexist with these libraries. Furthermore, since
KwikNet must operate in multitasking environments, it cannot use errno since the results
of operations by different tasks would be indeterminate.
For these reasons, global variable errno is not altered by KwikNet. Instead, the
completion status of each socket operation is recorded within the socket descriptor upon
completion of the operation. This error status is then made available to the caller through
the kn_errno() procedure. Be sure to use kn_errno() (or its Treck equivalent) instead
of the BSD errno() function.
Unfortunately, procedures kn_socket() and kn_select() have no socket descriptor in
which to save the error code. Hence, you cannot use kn_errno() to retrieve error
information after calls to these procedures.
Error codes returned by kn_errno() are integer values which are a subset of the standard
Berkeley error codes. These error codes are summarized in Appendix B.