32
K
DAK
KwikNet Overview
Client - Server Using UDP Sockets
This example illustrates the use of KwikNet's UDP socket interface to deliver UDP
datagrams between two end points. This data transfer mechanism is not considered
reliable. Furthermore, since TCP is not used, a logical connection between the end points
does not exist. Although the end points happen to be running on the same host computer,
the actions required by each are still the same as would be required if they resided on
separate hosts interconnected by a real network.
The server's second scenario is embodied in function
server2(). The corresponding
function executed by the client is
client2().
The server calls kn_socket() to create a connectionless datagram socket. The server
calls kn_setsockopt() to force its socket to be non-blocking. It then calls kn_bind() to
bind itself to the socket, identifying itself as port 5001, but allowing
KwikNet to assign its
IP address.
KwikNet assigns IP address 192.168.1.73, the IP address of the only network
present in the sample configuration. Once the bind is complete, the server can
immediately receive data directed to IP address 192.168.1.73.
The server then uses procedure kn_select() to wait until some data from a client is
available for reading. The sample illustrates the proper use of the macros FD_ZERO,
FD_SET and FD_ISSET for manipulating socket sets.
Once data from the client is available, the server uses kn_recvfrom() to learn the client's
network address and to receive a 4 byte message, a long value, from the client. The
server then uses kn_connect() to identify the client to which it must send its response.
The server then uses procedure kn_select() to wait until the server's socket is ready to
accept data for transmission. The value received from the client is incremented by two
and echoed to the client using the kn_send() procedure to send the 4 byte long value.
Once the value has been echoed to the client, the server breaks its association with the
client using kn_connect() to identify its peer as port 0 and IP address 0. The server then
uses kn_shutdown() to terminate all send and receive operations on its socket. The
socket is then closed using kn_close().
The client calls
kn_socket() to create a connectionless datagram socket. It then calls
kn_bind() to bind itself to the socket, allowing KwikNet to assign a port number and IP
address.
The client then uses procedure kn_select() to wait until the client's socket is ready to
accept data for transmission. The arbitrary value 8 is sent to the server using the
kn_sendto() procedure to send the 4 byte long value. The value is sent to the server
with port number 5001 and IP address 192.168.1.73.
Note that the client has to know the port number of the server to which it is trying to
connect. The IP address of the server happens to be the IP address assigned to the
Ethernet network predefined in the KwikNet Library. The sample illustrates the use of
procedure kn_inet_addr() to convert an IP address in dotted decimal form to an
equivalent network compatible form.
Comments to this Manuals