Timeout para recepção de pacotes UDP em Java ME

Olá pessoal

Sou novo neste portal e tb iniciei o meu aprendizado em java a pouco tempo.

Estou trabalhando em uma aplicação onde recebo um “trem” de pacotes UDP da seguinte maneira:

–***–

onde:

  • pacote udp
    – gap entre os pacotes.

Eu tenho que ficar fazendo a recepção desses pacotes até que o numero de pacotes chegar ao fim ou então se nenhum pacote chegar em x segundos, tenho que parar a recepção e dar sequencia no programa.

Estou utilizando as classes Connector, Datagram e DatagramConnection do J2ME , mas ao contrário do J2SE não há possibilidade de setar este tempo de timeout, á unica opção que eu tenho é na hora que eu crio a conexão (método Connector. open()) de setar uma opção para receber exceções de timeout, mas não tenho como determinar este tempo e eu preciso fazer isto.

Já procurei por tudo não achei nada de concreto, se alguem puder me dar uma diga fico muito agradecido.

Grato
Michel

Ve se te ajuda, UDP

 import javax.microedition.lcdui.*;
 import javax.microedition.midlet.*;
 import javax.microedition.io.*;
 import java.util.*;
 
public class UDPTest extends MIDlet implements CommandListener {

    static final String hostname = "203.123.12.201";
    static final int serverport = 9000;
    private Form mainScreen = null;
    static final int sendport = 9000;
    static final int receiveport = 9001;
    private Form resultScreen = null;
    static boolean isOver = false;
    TextField message=null; 

    private Command okCommand = new Command("OK", Command.OK, 1);
    private Command backCommand = new Command("Voltar", Command.BACK, 2);
    private Command exitCommand = new Command("Sair", Command.EXIT, 2);

    private Display myDisplay;

    public UDPTest() {   
        myDisplay = Display.getDisplay(this);
        resultScreen = new Form("Result");
        resultScreen.addCommand(okCommand);
        resultScreen.setCommandListener(this);
        mainScreen = new Form("UDP Testing");
        message=new TextField("Message:"," ",90,TextField.ANY);
        //  mainScreen = new List("UDP Test", List.IMPLICIT);	
    }     

    public void startApp()
        throws MIDletStateChangeException {
        mainScreen.append(message);
        mainScreen.addCommand(okCommand);
        mainScreen.addCommand(exitCommand);
        mainScreen.setCommandListener(this);
        myDisplay.setCurrent(mainScreen);

    }

    public void pauseApp() { }

    public void destroyApp(boolean unconditional) { }

    public void commandAction(Command c, Displayable d) {
        String res = null;
        if ((c == okCommand) && (d == mainScreen)) {
          // int selected = mainScreen.getSelectedIndex();
          try {
            //switch (selected) {
            // case 0:  
               res = sendRequest(message.getString());
               resultScreen.append (res);
               myDisplay.setCurrent(resultScreen);       
               // break;
             // }
            }
            catch (Exception e) {
                System.err.println(e);
            }
        }
        if ((c == exitCommand) && (d == mainScreen)) {
            notifyDestroyed();
        }
        if ((c == okCommand) && (d == resultScreen)) {
            myDisplay.setCurrent(mainScreen);
        }  
    }

    String sendRequest(String request) {
        DatagramListener dgl = new DatagramListener("datagrama://:" + receiveport);
        dgl.start();
        doSend("datagrama://" + hostname + ":" + serverport, request);
        int counter = 0;
        outloop:while (!isOver) {
           if (counter++ > 5) break outloop;
           try {
             Thread.sleep(1000);
           } 
           catch (Exception e) {
             System.out.println("Acorda Neo!");
           }
         }

         isOver = false;
         return dgl.data;
         return "Sent";
    }
  
    void doSend(String destAddr, String msg) {
        int length = msg.length();
        byte[] message = new byte[length];
        DatagramConnection dc = null;
        
        System.arraycopy(msg.getBytes(), 0, message, 0, length);
        String localhost = "datagrama://localhost:" + sendport;

        try {
            dc = (DatagramConnection)Connector.open(localhost);
            Datagram dobject = dc.newDatagram(message, length, destAddr);         
            dc.send(dobject);
        } 
        catch (Exception e) {
            System.out.println("Failed to send message: " + e);
        }
        finally {
            if (dc != null) {
                try {
                    dc.close();
                } catch (Exception f) {
                    System.out.println("Flha ao fechar conector: " + f);   
                }
            }
        }
    }
    
    class DatagramListener extends Thread {
        DatagramConnection dc = null;
        Datagram dobject;
        String data = "error";

        DatagramListener(String recvAddr) {
           try {  
               dc = (DatagramConnection)Connector.open(recvAddr);
               dobject = dc.newDatagram(dc.getMaximumLength());
           }
           catch (Exception e) {
               System.out.println("Falha ao finalizar o conector: " + e);
           }
        }

        public void run() {
           try {
              dc.receive(dobject);
              data = new String(dobject.getData(), 0, dobject.getLength());
              isOver = true; 
           } 
           catch (Exception e) {
              System.err.println("Falha ao receber msg:" + e);
           } 
           finally {
              if (dc != null) {
                try {           
                  dc.close();
                } catch (Exception f) {
                  System.err.println("Flaha ao fechar conexão: " + f);   
                }
              }
           }
        }
    }
}