I am using the hypermedia.net library for UDP. I have 6 UDP ports reading from 6 different devices (each device is on its own port). I am trying to have each port processed in its own thread.
Currently I am testing using just 2 ports. I have multiple threads running well, and the first thread works as expected but it seems the second thread isn't calling the "reveive1(byte...)" method (which should be called when the UDP receives a message). Does anyone have any suggestions for this?
The output on the terminal fro the log of a message is as follows:
Starting thread started on Port 8889
-- UDP session started at Fri Jan 20 15:28:48 EST 2012 --
Below is the code from my second thread to see if anyone can spot any obvious errors:
import hypermedia.net.*; UDP udp1; // define the UDP object
byte[] inBuffer1 = new byte[500];
int port1 = 8889; boolean myServerRunning1 = true;
class SecondThread extends Thread {
boolean running; // Is the thread running? Yes or no? int wait; // How many milliseconds should we wait in between executions? String id; // Thread name int count; // counter
// Constructor, create the thread // It is not running by default SecondThread (int w, String s) { wait = w; running = false; id = s; count = 0; }
//If using UDP use the following block: udp1 = new UDP( this, port1 ); // create a new datagram connection on port 8889 udp1.setReceiveHandler("receive1"); udp1.log( true ); // <-- printout the connection activity
udp1.listen( true ); // and wait for incoming message
// Do whatever start does in Thread, don't forget this! super.start(); }
// We must implement run, this gets triggered by start() void run () { while(running) { } println("Second Thread is ending"); }
private void receive1( byte[] byteBuffer1, String ip, int port ) { println("RECEIVE CALLED"); String inputString1 = new String(byteBuffer1); // Display the string println("Second Thread "+inputString1); } //end receive
// Our method that quits the thread void quit() { System.out.println("Quitting."); running = false; // Setting running to false ends the loop in run() // In case the thread is waiting. . . interrupt(); } }