简体   繁体   中英

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. 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. 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.

Here is my android client onClick function:

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:

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.

This is because readUTF (and therefore writeUTF ) may not do what you think it does; from DataInput documentation , referenced by the DataInputStream :

First, two bytes are read and used to construct an unsigned 16-bit integer in exactly the manner of the readUnsignedShort method . This integer value is called the UTF length and specifies the number of additional bytes to be read.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM