User`s guide

184
K
A
DAK
KwikNet TCP/IP Sockets
5.2 Socket Types
Procedure kn_socket() is used to create a socket. The KwikNet TCP/IP Stack only
supports sockets for use in the communication domain which uses the protocol family
known as the ARPA Internet Protocol, identified as PF_INET .
Two socket types are supported: type SOCK_STREAM for use with the TCP protocol and
type SOCK_DGRAM for use with the UDP protocol.
Stream Socket (for TCP)
A socket of type SOCK_STREAM provides a sequenced, reliable, full duplex connection
between two end points. The local end point is identified using a kn_bind() call. A
stream socket must be in a connected state before any data can be sent or received using
it. A connection is created with a kn_connect() call which identifies the remote end of
the connection. Once connected, data may be transferred using procedures kn_send()
and kn_recv(). Out-of-band data can be transferred using the MSG_OOB option in either
of these calls. Finally, when a session has been completed and the local socket is no
longer required, procedure kn_close() can be used to delete the socket.
The communications protocols used to implement a SOCK_STREAM ensure that data is not
lost or duplicated. If a piece of data accepted by the protocol stack cannot be
successfully delivered within a reasonable length of time, then the connection is
considered broken. The kn_send() or kn_recv() procedures will fail and the socket
will report an error code when subsequently interrogated with a kn_errno() call. Some
protocols include options to keep sockets warm by forcing transmissions roughly every
minute in the absence of other activity. An error is then indicated if no response can be
elicited on an otherwise idle connection for an extended period of time.
Functions kn_sendto(), kn_writev(), kn_recvfrom() and kn_readv() can also be
used to send and receive data once a connection has been established. Since the end
points of a connected socket are known, there is no need to provide the destination
address or storage for the source address when using these functions.
Datagram Socket (for UDP)
A socket of type
SOCK_DGRAM provides a connectionless, unreliable method for delivering
messages of a fixed, usually small, maximum length. The messages are called
datagrams. Although the local end point can be identified using a
kn_bind() call, it is
not necessary to bind the socket before using it. A SOCK_DGRAM socket allows a datagram
to be sent to a correspondent named in the
kn_sendto() call. Datagrams are received
using the kn_recvfrom() procedure which identifies the address from which the data is
received. Finally, when a session has been completed and the local socket is no longer
required, procedure
kn_close() can be used to delete the socket.
Although a socket of type SOCK_DGRAM is connectionless, the kn_connect() procedure
can still be used to identify the specific peer with whom a conversation is to be held. The
connection defines the address to which datagrams are to be sent and the only address
from which datagrams are to be received. Furthermore, kn_connect() can be called at
any time to change the connection address.