Download Code Patterns in newLISP®

Transcript
Code Patterns in newLISP
(while (not (net-error))
(set ‘connection (net-accept listen)) ; blocking here
(while (not (net-error))
(net-receive connection message-from-client max-bytes)
.... process message from client ...
.... configure message to client ...
(net-send connection message-to-client))
)
and the client:
; client connects to sender
(if (not (set ‘connection (net-connect “host.com” 123)))
(println (net-error)))
; maximum bytes to receive
(constant ‘max-bytes 1024)
; message send-receive loop
(while (not (net-error))
.... configure message to server ...
(net-send connection message-to-server)
(net-receive connection message-from-server max-bytes)
.... process message-from-server ...
)
Closed transaction
In this pattern the server closes the connection after each transaction exchange of messages.
; sender
(while (not (net-error))
(set ‘connection (net-accept listen)) ; blocking here
(net-receive connection message-from-client max-bytes)
.... process message from client ...
.... configure message to client ...
(net-send connection message-to-client)
(close connection)
)
and the client again tries to connect to the sender:
; client
(unless (set ‘connection (net-connect “host.com” 123))
(println (net-error))
(exit))
; maximum bytes to receive
(constant ‘max-bytes 1024)
.... configure message to server ...
(net-send connection message-to-server)
(net-receive connection message-from-server max-bytes)
.... process message-from-server ...
There are many different ways to set up a client/server connection, see also the examples in the
newLISP manual.
37