envío de paquetes a través de la red 3G

I am trying to write a method that will send a message over a 3G network with a base station to the server. IM trying to send the message multiple times until I decide to stop. But when I tested this, it always stops after a short time and stops sending the message. Anyone know why?

private Runnable commRunnable = new Runnable() {
public void run() {
    try {
        String message = "Just saying hello!";
        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())), true);

        String startReceivingMessage = "Begin sending me data.";
        String stopReceivingMessage = "Stop sending me data.";

        startSend = false;
        stopSend = false;
        startReceive = false;
        stopReceive = false;

        while (!shouldDisconnect) {
            if (startSend) {
                sendData = true;
                startSend = false;
            }
            if (stopSend) {
                sendData = false;
                stopSend = false;
            }

            // Send a message that the server should start transmitting data
            // back to us. We only need to transmit this message once.
            if (startReceive) {
                out.println(startReceivingMessage);
                startReceive = false;
                receiveData = true;
                Thread receiveThread = new Thread(receiveRunnable);
                receiveThread.start();

            // Tell the server to stop transmitting data.
            } else if (stopReceive) {
                out.println(stopReceivingMessage);
                stopReceive = false;
                receiveData = false;
            }

            if (sendData) {
                out.println(message);
            }

            Thread.sleep(20);
        }                               
    } catch (Exception e) {
        Log.e("PowerMonitor", e.toString());
    } finally {
        try {
            socket.close();
            connected = false;
        } catch (Exception e) {
            Log.e("PowerMonitor", e.toString());
        }
    }
}

};

private Runnable receiveRunnable = new Runnable() {
    @Override
    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String reply = "";
            Log.d("PowerMonitor", "Starting to receive");
            while (receiveData) {
                Log.d("PowerMonitor", "Listening...");
                reply = in.readLine();
                Log.d("PowerMonitor", "Got message: " + reply);
            }
        } catch (Exception e) {
            Log.e("PowerMonitor", e.toString());
        }
    }
};

preguntado el 04 de julio de 12 a las 09:07

What do you mean 'stop' ? Exit or hang ? If it's hanging, have you done a thread dump (Ctrl+Break) ? -

I think we'll need to see more code to have any idea what's going on here. Can you post your receiveRunnable's code too? (Assuming the flag that starts it gets set, of course...) -

@Jules Here is the receive Runnable -

i added below the first part above thx!! -

is your application staying in the foreground? how are you preventing the device from sleeping? what shows up in the log? -

1 Respuestas

We need more of your code for better understanding of your question, but from the code you posted here its seems like the socket is being closed by the

socket.close();

llamar en el finally block. Also tell about any errors you are getting.

Respondido 04 Jul 12, 10:07

I am not getting any errors per say, its simply timing out. I am tracking the packets being sent using WireShark and they are being sent and then after about maybe 20-30sec they stop transmitting - user1465975

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.