© Tyl Software Foundation 2019-2021
▶ NETWORKING MODULES
Working with Tyl Networking Client Module
Tyl Networking Client Module acts as the client, referred to as netclient
.
The initialization of
netclient
is the same as netserver
:
! ATTENTION: For this program to work, set a working
! network that has IP 127.0.0.1, start the
! network server netserver.start.single.tyl,
! found in the examples, and run the program.
localaddr '127.0.0.1'
port 10000
! declare netclient module
netclient localaddr port
msg 'connecting to address ' + netclient.address
msg ++ ' on port ' + netclient.port + newline
print msg
! connect to network server
netclient.connect
netclient.connected ?
print 'network client connected OK.'
\
print 'network connection fail!' + newline
print netclient.neterror
^
connecting to address 127.0.0.1 on port 10000
network client connected OK.
network client connected OK.
Here is an example of client program 'netclient.send.single.tyl', that
can be found in the examples. It shows how to send various types of commands,
as well as bytes streams, to the server:
! ATTENTION: For this program to work, set a working
! network that has IP 127.0.0.1, start the
! network server netserver.start.single.tyl,
! found in the examples, and run the program.
localaddr '127.0.0.1'
localport 10000
cr carriagereturn
OK '0'
ACK 'A' + cr
INFO 'I' + cr
RESET 'R' + cr
DATA 'D' + cr
netresult {}
! initializing non-text command bytes list (by logic)
nontexts
i 6 ~ nontexts <- math.floor 256 * random
! (alternatively) initializing non-text command bytes list (one byte)
!nontexts <- 255
! (alternatively) initializing non-text command bytes list (some bytes)
!nontexts 255 254 253
go:
netclient localaddr localport
netclient.connect
netclient.connected ?
! send an interactive command
print 'sending an interactive command...'
cmd ACK
netresult netclient.sendtext cmd
onsend cmd
! send request info command
print 'sending request info command...'
cmd INFO
netresult netclient.sendtext cmd
onsend cmd
! send action command (no response)
print 'sending action command...'
cmd RESET
netresult netclient.sendtext cmd 1
onsend cmd
! get data from server
print 'sending `data` command...'
cmd DATA
netresult netclient.sendtext cmd
onsend cmd 1
! send data to server
print 'sending data: ' + nontexts
cmd listinfo nontexts
netresult netclient.senddata nontexts 1
onsend cmd
\
print 'network connection fail!' + newline
print netclient.neterror
^
onsend cmd showbytelist:
showtext \t
showbytelist ? showtext \f
netresult 'ok' ?
cmd string.trim cmd
print 'command `' + cmd + '` sent successfully.'
showtext ?
text netresult 'text'
text ? print 'response: ' + text
\
print 'response: ' + netresult 'data'
^
\
print netclient.neterror
print
print 'send fail!'
exit
^
print
listinfo items:
itemslen len items
itemslast itemslen - 1
result '('
i itemslen ~
result ++ items i
i < itemslast ? result ++ ', '
^
result ++ ')'
sending an interactive command...
command 'A' sent successfully.
response: 0
sending request info command...
command 'I' sent successfully.
response: Tyl Networking Server Demonstration
sending action command...
command 'R' sent successfully.
sending 'data' command...
command 'D' sent successfully.
response: [ 0, 1, 2, 253, 254, 255 ]
sending data: [ 231, 212, 212, 236, 58, 249 ]
command '(231, 212, 212, 236, 58, 249)' sent successfully.
command 'A' sent successfully.
response: 0
sending request info command...
command 'I' sent successfully.
response: Tyl Networking Server Demonstration
sending action command...
command 'R' sent successfully.
sending 'data' command...
command 'D' sent successfully.
response: [ 0, 1, 2, 253, 254, 255 ]
sending data: [ 231, 212, 212, 236, 58, 249 ]
command '(231, 212, 212, 236, 58, 249)' sent successfully.
As mentioned in the last page, there is a connection mode that demands from the
client to reconnect each time a message is sent. To operate in this mode, you
should initialize
netclient
with a third scalar variable that its boolean
value will be TRUE.