UDP library receive event blocked
in
Contributed Library Questions
•
2 months ago
For my project, I am using the UDP library to communicate with 3 Arduinos via Wifly modules. Everything is working fine, except receive listener seems to be blocked after a couple of minutes. If a udp message is not sent to an arduino after a while, processing will stop receiving messages from the address. I've been using Wireshark to view the network activity and there haven't been any dropped packets.
Anyone else have the same issue? Below is how I have setup the udp communication. I could just send a random message if nothing has been sent for a certain amount of time, but I was wondering if there was an easy way to turn off the blocking.
- //works with udp test for send/receive, 2 way comm.
- // import UDP library
- import hypermedia.net.*;
- UDP udp; // define the UDP object
- boolean sendMsg = false;
- boolean send = false;
- int currentTime = 0;
- int prevTime = 0;
- int interval = 2000; //no buffer overrun on arduino
- /**
- * init
- */
- void setup() {
- // create a new datagram connection on port 4001
- // and wait for incoming message
- udp = new UDP( this, 4001 );
- //udp.log( true ); // <-- printout the connection activity
- udp.listen( true );
- }
- //process events
- void draw() {
- currentTime = millis();
- if((currentTime - prevTime) > interval)
- {
- prevTime = currentTime;
- if(sendMsg == true)
- {
- if(send == true)
- {
- String ip = "172.16.2.4"; // the remote IP address
- int port = 4000; // the destination port
- String message = "225;255;255;h;@";
- udp.send( message, ip, port );
- println( "send: \""+message+"\" to "+ip+" on port "+port );
- String ip2 = "172.16.2.3"; // the remote IP address
- udp.send( message, ip2, port );
- println( "send: \""+message+"\" to "+ip2+" on port "+port );
- send = false;
- }
- else
- {
- String ip = "172.16.2.4"; // the remote IP address
- int port = 4000; // the destination port
- String message = "0;0;0;l;@";
- udp.send( message, ip, port );
- println( "send: \""+message+"\" to "+ip+" on port "+port );
- String ip2 = "172.16.2.3"; // the remote IP address
- udp.send( message, ip2, port );
- println( "send: \""+message+"\" to "+ip2+" on port "+port );
- send = true;
- }
- }
- }
- }
- /**
- * on key pressed event:
- * send the current key value over the network
- */
- void keyPressed() {
- String message = str( key ); // the message to send
- String ip = "172.16.2.4"; // the remote IP address
- int port = 4000; // the destination port
- println( "send: \""+message+"\" to "+ip+" on port "+port );
- // formats the message for Pd
- message = "225;255;255;" + message + ";@";
- //message = "hello";
- // send the message
- udp.send( message, ip, port );
- }
- /**
- * To perform any action on datagram reception, you need to implement this
- * handler in your code. This method will be automatically called by the UDP
- * object each time he receive a nonnull message.
- * By default, this method have just one argument (the received message as
- * byte[] array), but in addition, two arguments (representing in order the
- * sender IP address and his port) can be set like below.
- */
- // void receive( byte[] data ) { // <-- default handler
- void receive( byte[] data, String ip, int port ) { // <-- extended handler
- // get the "real" message =
- // forget the ";\n" at the end <-- !!! only for a communication with Pd !!!
- data = subset(data, 0, data.length-2);
- String message = new String( data );
- // print the result
- println( "receive: \""+message+"\" from "+ip+" on port "+port );
- }
1