简体   繁体   English

使用Android客户端和python服务器在DataOutputStream和DataInputStream之间滞后

[英]Lag between DataOutputStream and DataInputStream using an android client and python server

I'm having an issue with my android client where I click a button to send data to a python server through a socket, which processes it, then sends it back upper cased. 我的android客户端出现问题,我单击一个按钮通过套接字将数据发送到python服务器,然后将其处理,然后将其发送回大写。 My android client's datainputstream always seems to be "one step behind": the first time I click the button the python server clearly receives it(as shown by the print function) but the TextView in my android doesn't show anything. 我的android客户端的datainputstream似乎总是“落后一步”:第一次单击该按钮时,python服务器会清楚地接收到它(如打印功能所示),但是我的android中的TextView却什么也没显示。 When I click again with different data the python server receives the new data but my android's dis.readUTF() seems to read the old data. 当我再次单击不同的数据时,python服务器接收到新数据,但是我的android的dis.readUTF()似乎读取了旧数据。

Here is my android client onClick function: 这是我的android客户端onClick函数:

public void onClick(View arg0) {

    Thread t = new Thread(){

        @Override
        public void run() {
            try {
                Socket s = new Socket("192.168.4.1", 9999);
                //OutputStreamWriter osw= new OutputStreamWriter(s.getOutputStream(), "UTF-8");
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                //byte[] bufO=message.getText().toString().getBytes("UTF-8");

                dos.writeUTF(message.getText().toString());
                DataInputStream dis = new DataInputStream(s.getInputStream());

                serverResponse= dis.readUTF();
                s.close();

            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    if(message.getText().toString().equals("")){
        Toast.makeText(this, "Empty command", Toast.LENGTH_SHORT).show();
    }
    else{
        message_received.setText("");
        t.start();
        Toast.makeText(this, "Command sent", Toast.LENGTH_SHORT).show();
        message_received.append(serverResponse + "\n");
        Log.d(TAG, serverResponse);


    }
}

Here is my Python server: 这是我的Python服务器:

import socketserver
import socket

class MyHandler(socketserver.BaseRequestHandler):
        def handle(self):
                self.sentence= self.request.recv(1024).strip()
                print(self.sentence)
                self.request.sendall(self.sentence.upper())


def main():
        print ("Opening socket")
        host='192.168.4.1'
        port=9999
        server1=socketserver.TCPServer((host,port),MyHandler)
        print ("Running server")
        server1.serve_forever()
main()

I've tried different things including adding a thread.sleep between the writeutf() and readutf() but nothing seems to fix this. 我尝试了不同的方法,包括在writeutf()和readutf()之间添加thread.sleep,但似乎没有任何办法可以解决此问题。

This is because readUTF (and therefore writeUTF ) may not do what you think it does; 这是因为readUTF (以及writeUTF )可能无法完成您认为的工作; from DataInput documentation , referenced by the DataInputStream : DataInput文档 ,通过引用DataInputStream

First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the readUnsignedShort method . 首先,读取两个字节,并以与readUnsignedShort方法完全相同的方式来构造一个无符号的16位整数。 This integer value is called the UTF length and specifies the number of additional bytes to be read. 此整数值称为UTF长度,它指定要读取的其他字节数。

In general it is a good idea to include the length of the string into the string encoding. 通常,将字符串的长度包括在字符串编码中是一个好主意。 So maybe you should alter the python server code to receive and send the length encoding as well. 因此,也许您应该更改python服务器代码以接收和发送长度编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TCP客户端/服务器程序,DataInputStream / DataOutputStream问题 - TCP client/server program, DataInputStream / DataOutputStream issue Android:DataInputStream和DataOutputstream看不到我的目录 - Android: DataInputStream & DataOutputstream not seeing my directory 使用DataInputStream和DataOutputStream和套接字的管道断开 - Broken pipe using DataInputStream and DataOutputStream and sockets DataInputStream/DataOutputStream 类与 InputStream/OutputStream 类的区别 - Difference Between DataInputStream/DataOutputStream Class & InputStream/OutputStream Class Java DataOutputStream / DataInputStream OutOfMemoryError - Java DataOutputStream / DataInputStream OutOfMemoryError DataInputStream \\ DataOutputStream的性能 - Performance of DataInputStream\DataOutputStream 在C中等效于DataOutputStream和DataInputStream - Equivalent of DataOutputStream and DataInputStream in C 使用dataoutputstream将字节写入套接字时,如何读取datainputstream? - How to read a datainputstream when using a dataoutputstream to write bytes to socket? java网络中DataInputStream和DataOutputStream的使用 - Use of DataInputStream and DataOutputStream in java networking 如何使用DataOutputStream和DataInputStream进行序列化/反序列化? - How to serialize/deserialize with DataOutputStream and DataInputStream?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM