Till Networking Module is a system module, that is used for handling networking activities. With this module it is possible to make a communication (TCP/IP) between our computer and another computer or electronic device, send commands and data, and receive data.
For handling both sides of network communication, there are two networking modules “netserver” and “netclient” for the server and client respectively. Note that the server is merely used as a responder and does not do any functionality in response to received commands, but the client has all the capability of working with any device that is connected via network connectivity.
Module (server):
netserver
Properties:
- address – TCP/IP address
- port – MAC address
- disconnectanyway – mode of network connection (whether connection is resumed for each incoming request)
- started – connection state
- neterror – last network error
Functions:
- start – start server
- addcommand – get send and receive commands (each command is a list of bytes, system will respond to the send bytes with the receive bytes)
Module (client):
netclient
Properties:
- address – TCP/IP address
- port – MAC address
- connectanyway – mode of network connection (whether connection is resumed for each incoming request, the server and client must be synchronized on this property, if the server was started with “disconnectanyway” set to TRUE, then the client must connect with “connectanyway” set to TRUE)
- started – connection state
- neterror – last network error
Functions:
- connect – connect to server
- disconnect – close connection
- sendtext – get scalar variable, send its string value to the server, and get response
- senddata – get list of bytes, send to server, and get response
Program from reference “/networking/single/regular communication/netserver.start.single.till” that demonstrate how to start a server, add commands, and wait for incoming connection requests:
addr '127.0.0.1'
port 10000
! text commands
cr carriagereturn
OK '0'
ACK 'A' + cr
INFO 'I' + cr
DATA 'D' + cr
! declare commands lists
ackcmd infocmd datacmd okresp inforesp []
! ack command
ackcmd string.bytes ACK
! info command
infocmd string.bytes INFO
! data command
datacmd string.bytes DATA
! ok response
okresp string.bytes OK
! info response
inforesp string.bytes 'Till Networking Server Demonstration'
! data response
dataresp [ 0 1 2 253 254 255 ]
! declare netserver module
netserver addr port
! add all interactive commands
netserver.addcommand ackcmd okresp
netserver.addcommand infocmd inforesp
netserver.addcommand datacmd dataresp
! start network server
netserver.start
netserver.started ?
print 'net server started OK.'
pause 0.3
print 'waiting for connections...'
! put program on hold to enable networking task running
~ pause 1
\
print netserver.neterror
print
print 'net server start fail!'
ᐤ
Program from reference “/networking/single/regular communication/netclient.connect.single.till” that demonstrate how to connect to a server:
addr '127.0.0.1'
port 10000
! declare netclient module
netclient addr port
msg 'connecting to address ' + netclient.address
msg ++ ' on port ' + netclient.port
print msg
! connect to network server
netclient.connect
netclient.connected ?
print 'network client connected OK.'
\
print 'network connection fail!' + newline
print netclient.neterror
^
Program from reference “/networking/single/regular communication/netclient.send.single.till” that demonstrate how to send data to a server and handle the response:
addr '127.0.0.1'
port 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
!nontexts 255 254 253
! ATTENTION: For this program to work, run network server
! file 'netserver.start.single.till' from the
! examples
go:
netclient addr port
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 (no response)
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 ++ ')'