簡體   English   中英

無法使用套接字將字節數據從Android App發送到Python Server

[英]Cannot send byte Data from Android App to Python Server using sockets

我已經盡一切努力使它起作用。 基本上,我有一個Android應用程序,可以從本地網絡連接上的基於Python的服務器接收數據。 我可以收到數據沒問題。 但是,當我嘗試將數據發送回時,應用程序崩潰了,Python服務器接收到空白數據。 我嘗試了幾種不同的方法,但沒有奏效。 這是我編寫的用於接收消息的Python方法:

   def checkReply(self):
    reply = "no reply yet"
    self.conn.settimeout(1)
    try:
        test =  self.conn.recv(1024)
    except:
        self.conn.timeout;
        print("I failed to hear this") #Debug to help see if I have heard an incomming message
    try:
        data = test.decode()

        reply = data
    except:
        print("I failed to decode this") #Debug to help see if I could not decode an incomming message
    print(reply)
    self.conn.settimeout(0)

我的Android應用程序上的客戶端如下所示:

public class Client extends AsyncTask<Void, Void, Void> {

String dstAddress;
int dstPort;
String response = "No data has been sent yet";
TextView textResponse;
Socket socket = null;


Client(String addr, int port, TextView textResponse) {
    dstAddress = addr;
    dstPort = port;
    this.textResponse = textResponse;
}

@Override
protected Void doInBackground(Void... arg0) {

    Socket socket = null;

    try {
        socket = new Socket(dstAddress, dstPort);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                1024);
        byte[] buffer = new byte[1024];

        int bytesRead;
        InputStream inputStream = socket.getInputStream();

     /*
      * notice: inputStream.read() will block if no data return
      */
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, bytesRead);
            response = byteArrayOutputStream.toString("Ascii");
        }

        byteArrayOutputStream.flush();


    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        response = "UnknownHostException: " + e.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        response = "IOException: " + e.toString();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return null;
}

@Override
protected void onPostExecute(Void result) {
    textResponse.setText(response);
    super.onPostExecute(result);
}

protected String getSite(){
    return response;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void returnMsg(){

        try (DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream())) {
            byte[] buf = "hello".getBytes("UTF-8");
            outToClient.writeBytes("Test");
            outToClient.flush();

        } catch (IOException e) {}




}

}

我必須在物理設備上進行測試,所以我沒有日志來跟蹤錯誤消息。 任何幫助將不勝感激。 謝謝

我設法解決了這個問題。 我需要先刷新緩沖區,然后再嘗試通過套接字發送回任何數據

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM