portability issue with net-receive-from

Q&A's, tips, howto's
Locked
Astrobe
Posts: 43
Joined: Mon Jan 11, 2010 9:41 pm

portability issue with net-receive-from

Post by 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: portability issue with net-receive-from

Post by Lutz »

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/

Locked