We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › UDP.listen() only gets 1 msg then stops listning
Page Index Toggle Pages: 1
UDP.listen() only gets 1 msg then stops listning (Read 1216 times)
UDP.listen() only gets 1 msg then stops listning
Jun 15th, 2009, 1:18pm
 
Hi,

I installed the UPD library (cannot post link)

I found the link in the "official" listing of contributions here at processing dot org

According to its documentation and example,
(I wanted to post the links but there is this ridiculous restriction that doesn't let me do it as if this was to help avoiding spam - shame)
the following code put into the setup() method:
   udpserver=new UDP(this, 3002);
   udpserver.listen();
is supposed to open a UDP server socket and call the method
receive()
of the main application EVERY TIME an UDP packet is received.

Well, I did this, and the received() method only gets called once, when the first packet is received.
Then the socket stops listening.

I have verified it not only by putting a "print" in the receive function, but also by adding the following line to the draw() method:
 print(udpserver.isListening());
which prints "false" after the first packet has been received.

This is not the way it is supposed to work.
I even tried by calling the listen() method again after receiving a packet, that is:
 void receive (byte[] data) {
     //.... process received data
     udpserver.listen()
 }
but this way, the applet window never appears!!!
(I am receiving about 10 packets per second which is not much)

I can call the listen() method at every frame in the draw() method, but this will only ensure I will get one packet per frame (I tried it), so I will loose packets.

Can anybody explain me what's wrong and how to fix it???

Is there any alternative to this library in order to create a UDP receiving socket?

By the way, "udpserver" is obviously a variable I have declared at the beginning of my pde file as follows:
 UDP udpserver


Thanks in advance
m.
SOLVED - listen(true)
Reply #1 - Jun 15th, 2009, 3:27pm
 
I got the answer directly from Stephane.

The documentation is not clear about this, but I had to use listen(true) instead of listen().
listen() with no parameter is NOT a synonim of listen(true): it blocks untill a packet is received and only waits for one packet; this _is_ the way it is meant to work, even if the documentation is not explicit about it.

Here it is with Stephane's own words:
"""
This is not really explicit in the doc. I never took the time to make a real and complete documentation… sorry.
You forgot to pass the listening status "listen(on)" to listen constantly for incoming data. Call the listen() method without argument only to wait for incoming data (and call the appropriate handlers while a message is received) by forcing the current thread to be ceased for a temporary period. This is used while you want to figure out network messages only one time (or more of course by calling the listen() method again).
"""

Now replacing listen() with listen(true) it works fine.
Re: UDP.listen() only gets 1 msg then stops listning
Reply #2 - Jun 16th, 2009, 2:36am
 
Thanks for sharing the tip!
Page Index Toggle Pages: 1