portability issue with net-receive-from

Started by Astrobe, March 18, 2014, 03:43:05 AM

Previous topic - Next topic

Astrobe

I used (net-receive-from) to make a little UDP proxy under linux. When I ran it under windows, it unexpectedly didn't work.



It seems that under Windows (XP), the address field which is returned is in the form ip-address:port.

Lutz

#1
QuoteIt seems that under Windows (XP), the address field which is returned is in the form ip-address:port.


Yes, this is correct and must be watched for when extracting host strings from received UDP messages.



A second thing to watch for is the fact that both, the receiver and the sender must issue net-listen commands. When using UDP, net-listen only binds the socket used, it does not really listen as when using the TCP/IP protocol. The following code will work on both, Unix and Windows.



Server:

; server - start first
(set 'socket (net-listen 10001 "localhost" "udp"))
(if socket (println "server listening on port " 10001)
           (println (net-error)))
(while (not (net-error))
(set 'msg (net-receive-from socket 255))
(println "->" msg)
(net-send-to
(first (parse (nth 1 msg) ":")) (nth 2 msg) (upper-case (first msg)) socket))


and for the client:

; client
(set 'socket (net-listen 10002 "" "udp"))
(if (not socket) (println (net-error)))
(while (not (net-error))
(print "enter something -> ")
(net-send-to  "127.0.0.1" 10001 (read-line) socket)
(net-receive socket buff 255)
(println "=> " buff))


This code can also be found in examples/udp-client.lsp and examples/udp-server.lsp. Only in 10.6.0 it has been corrected to also work on Windows, parsing out the host label.



http://www.newlisp.org/downloads/development/inprogress/">http://www.newlisp.org/downloads/develo ... nprogress/">http://www.newlisp.org/downloads/development/inprogress/