为什么将图像文件从python客户端传输到java服务器后,我无法传输字符串?

时间:2019-04-04 17:52:36

标签: java python sockets

我的目标是在将图像文件从python客户端传输到java服务器之后传输字符串数据。

我只能在java服务器和python客户端之间成功地传输字符串数据,但是当我在字符串数据传输代码之间添加图像文件传输代码时,我的代码被卡住或获取了空值。

简而言之,在将图像文件从python客户端传输到java服务器之后,我无法从java服务器向python客户端以及从python客户端向java服务器发送字符串数据。

我的Java eclipse版本是2018-12(4.10.0),而python版本是python 3。

1.JAVA服务器代码

package socket_test;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class socket_test {
    public static void main(String[] args) {

        ServerSocket server = null;

        try {
            server = new ServerSocket(8000);
            Socket client = server.accept();

            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            client.getInputStream(), "UTF-8"
                    )
            );
            PrintWriter pw = new PrintWriter(client.getOutputStream(), true);

            String message_1 = br.readLine();
            System.out.println(message_1 + "\n");

            pw.print("Server: Hello");
            pw.flush();

            String message_2 = br.readLine();
            System.out.println(message_2 + "\n");

            pw.print("Server: I am good");
            pw.flush();

            DataInputStream dis = new DataInputStream(client.getInputStream());
            FileOutputStream fos = new FileOutputStream("C:\\Users\\user\\Documents\\eclipse-workspace\\socket_test\\img\\1.jpg");

            byte[] buffer = new byte[4096];
            int len = 0;
            while ((len = dis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            // after this I can't transfer string data anymore.. when i use pw.print then code gets stuck and message_4 gets null.

            String message_4 = br.readLine();
            System.out.println(message_4 + "\n");
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            server.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

2.Python客户端代码

from socket import *
from select import select
import sys

HOST = '' # sorry it is my ip
PORT = 8000

client = socket(AF_INET, SOCK_STREAM)

image = '/home/pi/Downloads/1.jpg'

try:
    client.connect((HOST,PORT))

    client.sendall(bytes("Client: Hello", encoding = 'utf8'))
    client.sendall(bytes("\n", encoding = 'utf8'))

    reply = client.recv(1024)
    reply = reply.decode()
    print(reply, "\n")

    client.sendall(bytes("Client: How are you?", encoding = 'utf8'))
    client.sendall(bytes("\n", encoding = 'utf8'))

    reply = client.recv(1024)
    reply = reply.decode()
    print(reply, "\n")

    img = open(image, 'rb')
    converted_img = img.read()
    client.sendall(converted_img)
    img.close()

    client.sendall(bytes("Client: See you again", encoding = 'utf8'))
    client.sendall(bytes("\n", encoding = 'utf8'))

    client.close()
except Exception as e:
    print(e)

0 个答案:

没有答案