在31-33次传输后,Android停止接收来自微控制器的TCP传输

时间:2017-03-31 04:35:53

标签: java android tcp arduino tcpserver

我的Android设备有一个移动热点,通过TCP无线连接到粒子光子,后者从LIDAR传感器发回距离测量结果。

Photon不断查询传感器,从我的终端,串行输出连续显示实时距离。

然而,在31到33次传输之后,我的Android上的TCP客户端停止显示实时信息。它无法接收Photon发送的传输。无论在1 ms还是1000 ms的传输之间存在延迟,它似乎在31次传输后挂起。

为什么会发生这种情况,我该如何防止这种情况发生?

请在下面找到我的Java代码(Android)和我的接线代码(Photon)。

机器人:

ajax()

光子:

import android.util.Log;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;


public class TCPClient {
    private int i = 0;
    private String serverMessage;
    public static String buttonPushed;
    public static final String SERVERIP = "xxx.xxx.xxx.xxx"; //your Photon IP address
    public static final int SERVERPORT = 23;
    private OnMessageReceived mMessageListener = null;
    private boolean mRun = false;

    PrintWriter out;
    BufferedWriter out1;
    OutputStreamWriter out2;
    OutputStream out3;
    BufferedReader in;

    /**
     *  Constructor of the class. OnMessagedReceived listens for the messages received from server
     */
    public TCPClient(OnMessageReceived listener) {
        mMessageListener = listener;
    }

    /**
     * Sends the message entered by client to the server
     * @param message text entered by client
     */
    public void sendMessage(String message){
        if (out != null && !out.checkError()) {
            out.println(message);
            Log.d("TCP Client", "Message: " + message);
            out.flush();
        }
    }

    public void stopClient(){
        mRun = false;
    }

    public void run() {

        sendMessage(buttonPushed);
        if (buttonPushed == "Disconnect") {
            stopClient();
            return;
        }

        mRun = true;

        try {
            sendMessage(buttonPushed);
            if (buttonPushed == "Disconnect") {
                stopClient();
                return;
            }
            //here you must put your computer's IP address.
            InetAddress serverAddr = InetAddress.getByName(SERVERIP);

            Log.e("TCP Client", "C: Connecting...");

            //create a socket to make the connection with the server
            Socket socket = new Socket(serverAddr, SERVERPORT);

            try {
                sendMessage(buttonPushed);
                if (buttonPushed == "Disconnect") {
                    disconnectSession(socket);
                    return;
                }
                //send the message to the server

                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

                sendMessage(buttonPushed);

                //receive the message which the server sends back
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                //in this while the client listens for the messages sent by the server
                while (mRun) {
                    Log.e("TCP Client", "C: Reading from server...");

                    serverMessage = in.readLine();

                    Log.e("TCP Client", "C: Server successfully read!");
                    new TCPClient(mMessageListener);

                    if (serverMessage != null && mMessageListener != null) {
                        //call the method messageReceived from MyActivity class
                        mMessageListener.messageReceived(serverMessage);
                        i++;
                        Log.d("Incoming counter", String.valueOf(i));
                    } else {
                        serverMessage = null;
                    }

                    sendMessage(buttonPushed);
                    if (buttonPushed == "Disconnect") {
                        disconnectSession(socket);
                        break;
                    }
                }

                Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");




            } catch (Exception e) {
                Log.e("TCP", "S: Error", e);
            } finally {
                if (buttonPushed == "Disconnect") {
                    disconnectSession(socket);
                    return;
                }
            }

        } catch (Exception e) {
            Log.e("TCP", "C: Error", e);
        }

    }

    public void disconnectSession(Socket socket) throws IOException {
        stopClient();

        //the socket must be closed. It is not possible to reconnect to this socket
        // after it is closed, which means a new socket instance has to be created.
//        socket.close();
        serverMessage = null;
    }

    //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
    //class at on asynckTask doInBackground
    public interface OnMessageReceived {
        public void messageReceived(String message);
    }
}

提前致谢!

0 个答案:

没有答案