TCP Server

TCP Server

Setting up a listening server in the printer can be accomplished with the
SERVERSOCKET
function. To connect to incoming TCP sessions, use the
ACCEPT
function.
When starting the application, call
SERVERSOCKET
. This function will create a handle for this listening server. Check for incoming connections at regular intervals with the
ACCEPT
function. If there are no pending sessions, the
ACCEPT
function will return with an error. Handle the error using the
ON ERROR
command and continue looking for other sessions later.
Depending on how the program is set up, it is possible to handle one or more sessions at a time. If the program is configured to allow only one session, the other connections will remain pending until they are shut down by the requesting client or the ZBI program connects them.
Example
Here is an example of the
SERVERSOCKET and ACCEPT
commands:
10 CLOSE ALL 20 LET SERVER_HANDLE = SERVERSOCKET("TCPX",19100) 30 REM There are no connections yet we are just listening for them 40 REM Lets loop until we get a connection 50 SLEEP 1 60 LET INPORT = ACCEPT(SERVER_HANDLE,CLIENT_INFO$) 70 ON ERROR GOTO 50 80 PRINT #INPORT: "You have successfully connected!" 90 PRINT #INPORT: "Login:"; 100 INPUT #INPORT: LOGIN$ 110 PRINT #INPORT: "Password:"; 120 INPUT #INPORT: PASSWORD$ 130 REM We will not be nice and reject the connection 130 PRINT #INPORT: "Login failed" 140 CLOSE #INPORT 150 GOTO 60 ! Go look for the next connection 160 END