簡體   English   中英

android客戶端無法收到python服務器的響應

[英]The android client can't receive a response from the python server

python服務器從android接收圖像文件,並作為響應發送字符串“ OK”。 python服務器的源代碼如下:

serverSocket = socket(AF_INET, SOCK_STREAM)

serverSocket.bind(ADDR)
print('bind')

serverSocket.listen(CLIENT_NUM)
print('listen')

while True:
print('waiting...')
try:
    connectionSocket, addr_info = serverSocket.accept()
    print('accept')
    print('--client information--')
    print(connectionSocket)

    img = open("./img.jpg", 'wb')
    while True:
        img_data = connectionSocket.recv(BUFSIZE)
        data = img_data
        if img_data:
            while img_data:
                print("receiving Img...")
                img_data = connectionSocket.recv(BUFSIZE)
                data += img_data
            else:
                break
    img_file = open("img.jpg", "wb")
    print("finish img recv")
    img_file.write(data)
    img_file.close()

    connectionSocket.send("OK".encode())
    connectionSocket.close()
    print('connection closed')

except KeyboardInterrupt:
    sys.exit(0)

android客戶端將圖像文件發送到python服務器,並從python服務器接收字符串“ OK”。 python服務器的源代碼如下:

 public void run() {
    try {
        InetAddress serverAddr = InetAddress.getByName(serverIp);
        socket = new Socket(serverAddr, serverPort);
        try {
            dataOutput = new DataOutputStream(socket.getOutputStream());
            dataInput = new DataInputStream(new FileInputStream(img));

            byte[] buf = new byte[BUF_SIZE];
            int dataLen;
            while ((dataLen = dataInput.read(buf)) != -1) {
                dataOutput.write(buf, 0, dataLen);
                dataOutput.flush();
            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            Log.d("Socket", reader.readLine());
        } catch (Exception e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            String exceptionAsString = sw.toString();
            Log.e("StackTrace", exceptionAsString);
        } finally {
            try {
                if (dataInput != null)
                    dataInput.close();
                if (dataOutput != null)
                    dataOutput.close();
                if (socket != null)
                    socket.close();
            } catch (IOException e) {
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                String exceptionAsString = sw.toString();
                Log.e("StackTrace", exceptionAsString);
            }
        }
    } catch (IOException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.e("StackTrace", exceptionAsString);
    }

}

服務器無法

如果刪除下面兩行,服務器將正常接收文件。 但是,如果我在下面插入兩行,則服務器不會收到該文件。

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("Socket", reader.readLine());

Android客戶端如何將圖像文件發送到python服務器並獲得響應?

我解決了這個問題。 我認為導致此問題的原因一定是在服務器端,所以我修改了服務器的代碼,它起作用了!

img = open("./img.jpg", 'wb')
img_data = connectionSocket.recv(BUFSIZE)
data = img_data
firstPacketLen = len(img_data)
print("receiving Img...")
while len(img_data) > 0:
    img_data = connectionSocket.recv(BUFSIZE)
    data += img_data
    if len(img_data) < firstPacketLen:
        break
print("finish img recv")
img.write(data)
img.close()

connectionSocket.send("OK\r\n".encode())
connectionSocket.shutdown(SHUT_RDWR)
connectionSocket.close()
print('connection closed')

暫無
暫無

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

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