Search code examples
javaandroidpythonsocketstcp

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


The python server receives an image file from the android and sends the string "OK" in response. The source code of the python server is as follows:

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)

The android client sends an image file to the python server and it receive the string "OK" from python server. The source code of python server is as follows:

 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);
    }

}

The server cannot

If I delete the two lines below, the server receives the file normally. But if I insert the two lines below, the server doesn't receive the file.

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

How can the android client send an image file to the python server and get a response?


Solution

  • I solved this problem. I thought the cause of this problem must have been on the server side, so I modified the code of server and it worked!

    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')