User`s guide
182
K
A
DAK
KwikNet TCP/IP Sockets
KwikNet
Sockets API
The KwikNet TCP sockets API is a subset of that available on UNIX systems. Examples
of networking code from other sockets-based systems can be expected to port easily.
Furthermore, most of the reference material in books and tutorials will also apply to the
KwikNet API.
The
KwikNet API has been designed for best use in embedded systems where execution
speed, code size and ease of use are of paramount importance. For these reasons, the
KwikNet sockets API differs from the Berkeley sockets API. However, a standard sockets
API is provided with
KwikNet and can be used if code compatibility is of utmost concern.
The API differences are minor. All of the procedure names in the
KwikNet TCP/IP Stack
are of the form kn_xxxxx(). For example, Berkeley procedure socket() is KwikNet
procedure kn_socket(). This naming convention has been adopted by KADAK to avoid
any conflicts with symbols in your application or your C run-time libraries.
The standard sockets API uses the procedure
close() to close a socket. A KwikNet
procedure with this name would conflict with procedure close() in the standard C
library. Consequently, the KwikNet procedure kn_close() must be used to close a socket.
The KwikNet sockets API is defined as a set of C macros which map the KwikNet API
names to the underlying standard sockets API in the Turbo Treck TCP/IP Stack. To use
the KwikNet sockets API, include the following statement in your source modules:
#include "KN_SOCK.H"
To use the Berkeley standard sockets API, review the macro definitions in header file
KN_SOCK.H and call the equivalent Treck sockets functions directly. The Treck functions
use the BSD function names but may require some casting of parameter types to match
the function prototypes. Be sure to use kn_close() and kn_errno() (or their Treck
equivalents) instead of the BSD close() and errno().
Socket Addresses
The endpoint of a socket connection is identified using a socket address. The
specification of a socket address is dependent upon the protocol used for communication.
The sockets API uses a generalized
sockaddr structure to specify a socket address.
When using the TCP or UDP protocols, it is convenient to cast each sockaddr pointer to
reference the following Internet specific socket address structure:
struct sockaddr_in {
unsigned short sin_family; /* Address family = AF_INET */
unsigned short sin_port; /* TCP, UDP: protocol port */
struct in_addr sin_addr; /* TCP, UDP: IP address */
char sin_zero[8]; /* TCP, UDP: unused (0) */
};
Member sin_family always specifies the Internet Protocol address family (AF_INET),
stored in host endian form. The protocol port number is stored in member sin_port in
net endian form. The protocol IP address is stored in member sin_addr.s_addr in net
endian form. The array
sin_zero[] is unused and is ignored by KwikNet.