能够通过套接字发送图像,但不能发送文本文件 [英] Able to send images over socket but not text files

查看:88
本文介绍了能够通过套接字发送图像,但不能发送文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户端可以正常将图像发送到服务器,但是当涉及到文本文件时,它们会空着。任何想法我做错了什么?我真的很感激帮助,因为我一直试图让这项工作工作很多天。谢谢。

My client can send Images normally to server, but when it comes to text files they arrive empty. Any ideas what am I doing wrong? I'd really appreciate help, because I have been trying to make this work for many days now. Thanks.

这是服务器代码:

class TheServer {

    public void setUp() throws IOException { // this method is called from Main class.
        ServerSocket serverSocket = new ServerSocket(1991);
        System.out.println("Server setup and listening...");
        Socket connection = serverSocket.accept();
        System.out.println("Client connect");
        System.out.println("Socket is closed = " + serverSocket.isClosed());



        BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String str = rd.readLine();
        System.out.println("Recieved: " + str);
        rd.close();



        InputStream is = connection.getInputStream();

        int bufferSize = connection.getReceiveBufferSize();

        FileOutputStream fos = new FileOutputStream("C:/" + str);
        BufferedOutputStream bos = new BufferedOutputStream(fos);


        byte[] bytes = new byte[bufferSize];

        int count;

        while ((count = is.read(bytes)) > 0) {
            bos.write(bytes, 0, count);
        }

        bos.flush();
        bos.close();
        is.close();
        connection.close();
        serverSocket.close();


    }
}

这里是客户端代码:

public class TheClient {

    public void send(File file) throws UnknownHostException, IOException { // this method is called from Main class.
        Socket socket = null;
        String host = "127.0.0.1";

        socket = new Socket(host, 1991);

        // Get the size of the file
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            System.out.println("File is too large.");
        }

        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        wr.write(file.getName());
        wr.newLine();
        wr.flush();

        byte[] bytes = new byte[(int) length];
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

        int count;

        while ((count = bis.read(bytes)) > 0) {
            out.write(bytes, 0, count);
        }


        out.flush();
        out.close();
        fis.close();
        bis.close();
        socket.close();
    }
}


推荐答案


  1. 在读取所有数据之前,您在服务器端过早关闭 BufferedReader 。这基本上会关闭连接。

  2. 你不应该使用 Reader Writer 对于像二进制图像数据这样的非字符流。并且你不应该将 BufferedReader 与任何其他流包装器混合用于相同的流,因为它可以读取与填充缓冲区一样多的数据。

  1. You are prematurely closing BufferedReader on server side before reading all the data. This essentially closes the connection.
  2. You should not use Reader or Writer for non-character streams like binary image data. And you should not mix BufferedReader with any other stream wrapper for the same stream since it may read as many data as it fills in buffer.

这篇关于能够通过套接字发送图像,但不能发送文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆