TYL PROGRAMMING LANGUAGE
▶ NETWORKING MODULES
Handling Multiple Communications Using Instance Modules
In this chapter we will conduct multiple communications from the same program by using instances of Tyl Networking Server.

Here is an example of multiple communications server program 'netserver.start.tyl':
! ATTENTION: For this program to work, set a working
!            network that has IP 192.168.0.1. Alternatively,
!            set remote address same as local address, and
!            change the port for the remote server
localaddr '127.0.0.1'
localport 10000
!remoteaddr '192.168.0.1'
!remoteport 10000
remoteaddr '127.0.0.1'
remoteport 10001
! text commands
cr carriagereturn
OK '0'
ACK 'A' + cr
INFO 'I' + cr
DATA 'D' + cr
! declare commands lists
ackcmd infocmd datacmd okresp inforesp remoteinforesp []
! initializing data bytes list
dataresp [ 0 1 2 253 254 255 ]
! ok response
okresp string.bytes OK
! info response
inforesp       string.bytes 'Tyl Networking Server Demonstration'
remoteinforesp string.bytes 'Tyl Networking Remote Server Demonstration'
! ack command
ackcmd string.bytes ACK
! info command
infocmd string.bytes INFO
! data command
datacmd string.bytes DATA
! assign localserver as netserver module
localserver netserver localaddr localport
! add all interactive commands
localserver.addcommand ackcmd  okresp
localserver.addcommand infocmd inforesp
localserver.addcommand datacmd dataresp
! assign remoteserver as netserver module
remoteserver netserver remoteaddr remoteport
! add all interactive commands
remoteserver.addcommand ackcmd  okresp
remoteserver.addcommand infocmd remoteinforesp
print 'starting local server...'
localserver.start
localstarted localserver.started
localstarted ?
print 'local net server started OK.'
\
print localserver.neterror
print
print 'local net server start fail!'
print
print 'starting remote server...'
remoteserver.start
remotestarted remoteserver.started
remotestarted ?
print 'remote net server started OK.'
\
print remoteserver.neterror
print
print 'remote net server start fail!'
! put program on hold to enable networking task running
localstarted + remotestarted ? ~ pause 1
localserver and remoteserver are instances of netserver, and run simultaneously.

The client version is program 'network.send.tyl':
! ATTENTION: For this program to work, set a working
!            network that has IP 192.168.0.1, start the
!            network server netserver.start.tyl, found in
!            the examples, and run the program from the
!            remote computer.
!            Alternatively, set remote address same as
!            local address, and change the port for the
!            remote server
localaddr '127.0.0.1'
localport 10000
!remoteaddr '192.168.0.1'
!remoteport 10000
remoteaddr '127.0.0.1'
remoteport 10001
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:
 ! use the second declaration if the program runs from
 ! different computer than the server
netclient localaddr localport
 !netclient remoteaddr remoteport
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 ++ ')'
While running the server, run this version to work against localserver. In order to work also against remoteserver, create a copy of this client program, swap commenting of lines netclient localaddr localport and netclient remoteaddr remoteport, and run it simultaneously with the first version.