TYL PROGRAMMING LANGUAGE
▶ NETWORKING MODULES
Working with Tyl Networking Server Module
Tyl Networking Modules are used to manage TCP/IP network communication. There are both server and client modules, that function as network server and network client respectively.

Tyl Networking Server Module acts as the server, referred to as netserver. Usually to conduct a communication it is needed to have an IP address and port:
netserver '127.0.0.1' 10000
netserver.start
netserver was initialized with address and port, and the server is started by calling start function.

To get an indication if netserver was started we use started property:
netserver '127.0.0.1' 10000
netserver.start
netserver.started ?
print 'net server started OK.'
 ! put program on hold to enable networking task running
~ pause 1
\
print netserver.neterror
print
print 'net server start fail!'
^
If an error occurred, it is in neterror variable.

Once started, we have to put the program on hold. This is done in the uncoditional looping statement '~ pause 1', which puts the program effectively on idle, to enable the internal network listener to listen and respond to network connections.

Network communication is done by exchanging byte streams between the server and the client, therefore prior to starting the server it is needed to define a set of commands, that netserver will have, so it can receive and respond to them.

Adding a command:
cr carriagereturn
OK '0'
ACK 'A' + cr
ackcmd okresp []
okresp string.bytes OK
ackcmd string.bytes ACK
netserver '127.0.0.1' 10000
netserver.addcommand ackcmd okresp
Command ACK and resonse message OK are turned into bytes streams by string.bytes system function, and assigned to ackcmd and okresp lists. addcommand relates input command to output command, so when ACK command will be received, it will be responded with OK.

Here is a full example of network server 'netserver.start.single.tyl', that can be found in the examples:
! ATTENTION: For this program to work, set a working
!            network that has IP 192.168.0.1, start the
!            program on main computer and run the network
!            client netclient.send.single.tyl, found in
!            the example, 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
! text commands
cr carriagereturn
OK '0'
ACK 'A' + cr
INFO 'I' + cr
DATA 'D' + cr
! declare commands lists
ackcmd infocmd datacmd okresp inforesp []
! 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'
! ack command
ackcmd string.bytes ACK
! info command
infocmd string.bytes INFO
! data command
datacmd string.bytes DATA
! declare netserver module
netserver localaddr localport
! 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.'
 ! put program on hold to enable networking task running
~ pause 1
\
print netserver.neterror
print
print 'net server start fail!'
^
Tyl Network Server Module has two modes of connectivity. One mode is to demand the client to connect only once, and the other is to connect again each time a command is sent, and will be referred as connect anyway. netserver has a disconnectanyway variable, that determines connectivity mode, and it is the third variable of netserver intialization. If omitted, the default is connect once, otherwise it should be set to a scalar that its boolean value is TRUE, in the initialization statement:
netserver '127.0.0.1' 10000 1
In this declaration, the server will disconnect the connection after each incoming message, thus forcing the client to reconnect.

Note that usually, networking is done against a physical device that get commands from a network client, and respond to them, so this module can act as a testing server for the client application, prior to working against the device itself.
PART 2