portability issue with net-receive-from

Q&A's, tips, howto's

portability issue with net-receive-from

Postby Astrobe » Tue Mar 18, 2014 10:43 am

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.
Astrobe
 
Posts: 39
Joined: Mon Jan 11, 2010 9:41 pm

Re: portability issue with net-receive-from

Postby Lutz » Tue Mar 18, 2014 3:38 pm

It 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:
Code: Select all
; 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:
Code: Select all
; 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/develo ... nprogress/
Lutz
 
Posts: 5276
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest