UDP Server

UDP Server

Setting up a listening server in the printer can be accomplished with the
SERVERSOCKET
function. Then, to connect to incoming UDP packets, use the function
ACCEPT
. When starting your application, call
SERVERSOCKET
. This function will create a handle for this listening server.
Check for incoming packets at a regular interval with the
ACCEPT
function. If there are no pending sessions, the
ACCEPT
function will return with an error. Just handle the error using the
ON ERROR
command and continue looking for other sessions later. You will need to call
ACCEPT
for each incoming packet. When the accept is successful, all of the data will be available. Call
READ
with a
MAX
string size of 2000 and you will have the whole packet in your string. Close the port and wait for the next packet. You can only read in data using a UDP server.
Example
Here is an example of how to set up to receive UDP messages:
10 CLOSE ALL 20 LET ZPLPORT = 1 35 OPEN #ZPLPORT: NAME "ZPL" 40 LET SERVER_HANDLE = SERVERSOCKET("UDP",33333) 50 REM There are no connections yet: listening 60 REM Let’s loop until we get a connection 70 SLEEP 1 80 LET INPORT = ACCEPT(SERVER_HANDLE,CLIENT_INFO$) 90 IF INPORT = -1 THEN 92 GOTO 70 94 END IF 100 LET PACKET_SIZE = READ(INPORT,PACKET$,2000) 110 PRINT #ZPLPORT: "^XA^FO100,100^A0N,40,40^FDPACKET FROM:"; 115 PRINT #ZPLPORT: CLIENT_INFO$; "^FS" 120 PRINT #ZPLPORT: "^FO100,150^A0N,40,40^FDPACKET SIZE:"; 125 PRINT #ZPLPORT: PACKET_SIZE; "^FS" 130 PRINT #ZPLPORT: "^FO100,200^A0N,40,40^FDPACKET DATA:"; 135 PRINT #ZPLPORT: PACKET$; "^FS^XZ" 140 CLOSE #INPORT 150 GOTO 60 ! go look for the next connection 160 END