User`s guide
KwikNet TCP/IP Sockets
K
A
DAK
185
Functions kn_writev() and kn_readv() can also be used to send and receive data. If
you have provided a destination address with a kn_connect() call, you can use function
kn_send() to send data to that destination. And you can always use function kn_recv()
to receive data, as long as you do not need to identify the source.
The maximum size of a UDP datagram that KwikNet can send or receive is dictated by a
number of factors including memory availability, buffering capability at the IP protocol
layer, IP fragmentation limitations, network interface restrictions on datagram size and
routing effects as the UDP datagram finds its way to the intended destination. If you
send a UDP datagram which exceeds 576 bytes (512 bytes of UDP data), it becomes your
responsibility to ensure that the receiving host and intervening routers can handle such
UDP datagrams. When receiving, your application must be able to accept the largest
expected UDP datagram or risk loss of data.
Using UDP Sockets
Special care must be taken when using a socket of type SOCK_DGRAM with a
connectionless protocol such as UDP. When a UDP socket is created, it is considered
bound to local IP address 0 and port 0. The newly created socket is idle, unable to
receive data from any foreign host.
The UDP socket remains idle, bound to local IP address 0 and port 0, until you explicitly
bind it otherwise using kn_bind() or until you send data. When data is sent using a UDP
socket whose local port is still 0, a unique non-zero port number is automatically bound
to the socket to identify the source of the datagram. The local non-zero port number
identifies the source in all subsequent transmissions. Once the port number is known, the
socket can receive datagrams directed to that port.
Reception is governed by the local IP address and port to which the UDP socket is bound.
As long as the socket is bound to IP address 0, the socket can receive datagrams arriving
on any of the local interfaces. Once bound to a specific IP address, the socket can only
receive datagrams directed to that address. In either case, the datagrams will not be
delivered to the socket unless they are destined for the port to which the socket is bound.
Transmission is governed by the destination to which the UDP socket is connected. A
socket can be connected to a particular foreign address and port using function
kn_connect(). Once connected, any of the socket send functions, including kn_send(),
can be used to send a datagram to the connected host without having to explicitly identify
the destination. However, a socket does not have to be connected prior to sending a
datagram. Whether connected or not, functions
kn_sendto() and kn_writev() can
always be used to send a datagram to a specific foreign host.
The UDP socket connection also restricts the datagrams which the socket can receive.
Once the socket has been connected to a specific foreign host, the socket will only
receive datagrams directed to it from that foreign host.
Unless you specifically bind a UDP socket to a particular port, a unique port number will
be automatically assigned to the socket the first time the socket is connected or a
datagram is sent via the socket. Normally,
KwikNet will not allow you to bind a socket to
a particular port number if that port number is already used by another UDP channel. To
overcome this restriction, you can set the socket
SO_REUSEADDR option to allow the port
number to be reused.