0

我这里有客户端和服务器,客户端应该先发送文件,当服务器收到它时,它必须发送一个对象,发送和接收文件工作正常,但客户端没有收到结果对象

public class Server_file {

    public static void main(String[] args) throws IOException {
        ServerSocket server_socket = new ServerSocket(2233);
        Socket socket = server_socket.accept();
        FileOutputStream fos = new FileOutputStream(
                "C:/Users/Asmaa/Desktop/test51.txt");
        BufferedOutputStream out = new BufferedOutputStream(fos);
        byte[] buffer = new byte[1024];
        int count;
        OutputStream os = socket.getOutputStream();
        InputStream in = socket.getInputStream();
         while((count=in.read(buffer)) >=0 ){
         fos.write(buffer, 0, count);
         }
         fos.close();
         System.out.println("test");
        Resultat res = new Resultat(14);
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(res);
        oos.close();
        socket.close();

    }
}

这里是客户:

public class Client_file {
    public static void main(String[] argv) throws Exception {
        Socket socket = new Socket("127.0.0.1", 2233);
        int count;
        byte[] buffer = new byte[1024];
        File myFile = new File("C:/Users/Asmaa/Desktop/s.txt");

        OutputStream out = socket.getOutputStream();
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(
                myFile));
        while ((count = in.read(buffer)) >= 0) {
            out.write(buffer, 0, count);
            out.flush();
        }
        System.out.println("hi");
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        Resultat to = (Resultat) ois.readObject();
        System.out.println("test");
        System.out.println(to.getA());
        out.close();
        socket.close();
    }

}
4

0 回答 0