User`s guide

30
K
A
DAK
KwikNet Overview
Client - Server Using TCP Sockets
This example illustrates the use of KwikNet's TCP/IP socket interface to establish a
connection between two end points for the reliable transfer of data. Although the end
points happen to be tasks 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 first scenario is embodied in function server1(). The corresponding
function executed by the client is client1().
The server calls kn_socket() to create a streaming socket. The server calls
kn_setsockopt() to revise the socket's attributes to permit the eventual reuse of the
unique server port number of 5001. The server calls
kn_setsockopt() again 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. The server then calls kn_listen() to prime the socket to listen for
incoming connection requests. Finally, the server calls kn_accept() to wait for such a
connection to be established. KwikNet gives the server a new socket which corresponds to
the server's end of the connection to its client.
Once the connection with the client has been made, the server uses procedure kn_recv()
to receive a 4 byte message, a long value which defines the length of a block of data
which the client intends to send to the server.
The server uses kn_send() to echo the 4 byte value back to the client as an
acknowledgement that the server is prepared to accept that amount of data from the
client. Then the server calls kn_recv() to acquire the data block from the client and
kn_send() to echo the data block back to the client.
Once the data has been echoed to the client, the server uses kn_shutdown() to terminate
all send operations on its connected socket and then repetitively calls kn_recv() until all
unexpected data, if any, from the client has been discarded. The connected socket and
the socket used for listening are then closed using
kn_close().