Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用套接字与esp32进行实时通信_Java_Android_Sockets_Network Programming_Esp32 - Fatal编程技术网

Java 使用套接字与esp32进行实时通信

Java 使用套接字与esp32进行实时通信,java,android,sockets,network-programming,esp32,Java,Android,Sockets,Network Programming,Esp32,我正在开发一款android应用程序,它可以连接esp-32以共享实时数据。我目前正在使用线程来打开套接字和维护连接。这是代码 public TcpClient(String IP, int Port) { this.IP = IP; this.Port = Port; this.mClientThread = initThread(); } public void run (){ mClientThread.start(); } /** * Method

我正在开发一款android应用程序,它可以连接esp-32以共享实时数据。我目前正在使用线程来打开套接字和维护连接。这是代码

public TcpClient(String IP, int Port) {
    this.IP = IP;
    this.Port = Port;
    this.mClientThread = initThread();
}


public void run (){
    mClientThread.start();
}

/**
 * Method to init new thread
 * @return
 */
private Thread initThread (){
    Thread  thread = new Thread() {
        @Override
        public void run() {

            while (flag){

                try {
                    // init the socket
                    mClientSocket = new Socket();
                    mClientSocket.setSoTimeout(mSocketTimeOut);
                    mClientSocket.connect(new InetSocketAddress(IP,Port), mSocketTimeOut);

                    // reader and writer streams
                    PrintWriter TX = new PrintWriter(mClientSocket.getOutputStream(), true);
                    BufferedReader RX = new BufferedReader(new InputStreamReader( mClientSocket.getInputStream()));

                    // send and receive data
                    ...
                    // closing input and output streams
                    TX.close();
                    RX.close();
                    mClientSocket.close();

                } catch (IOException e) {
                   ...
                }
            }
        }
    };
    return thread;
}
问题是,当服务器没有响应并且我想停止线程时,我必须等待设置的超时,以便它在下一个循环中中断循环

1-有什么建议我该如何处理

2-这是实现实时通信的好方法。我不能使用web套接字,因为esp正在使用普通套接字,更改它们将是一个大混乱

3-对于原始线程,我需要自己处理所有的活动更改,这需要大量代码。我调查了一下

--异步任务:我相信它使用单后台线程进行调度。因此,如果我需要两个通信通道,它将不再是实时的

--具有PeriodicWorkRequest的Workmanager:因为最小间隔是15分钟,所以我也不能使用它

如有任何建议,将不胜感激