Java中TCP套接字上的文件损坏

时间:2014-12-10 08:01:24

标签: java sockets tcp

我正在通过多个TCP套接字(多个随机端口)发送一些文件,但每个发送的文件最终都会被破坏。 我们首先测试了这个只有一个插槽的独立程序,它工作正常。但是现在在我们的最终项目中实现了所有文件都被破坏了。 我无法打开任何图像,mp3文件......

这是文件发件人:

public class FileSenderThread implements Runnable {

    // Make a serverSocket
    protected ServerSocket serverSocket = null;
    protected Socket socket = null;
    protected String fileName = null;
    protected Long fileSize = null;
    protected String pathName = null;
    protected int port = 8500;

    public FileSenderThread(String name, Long size, int portNumber, String path) throws IOException {
        // get socket connection
        System.out.println("Init file transfer sender");
        this.port = portNumber;
        serverSocket = new ServerSocket(port);
        this.fileName = name;
        this.fileSize = size;
        this.pathName = path;
    }

    public void run() {
        try {
            socket = serverSocket.accept();
            System.out.println("Accepted connection : " + socket);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {

            System.out.println("Sending " + fileName);

            // find the file & the size
            File transferFile = new File(pathName + fileName);
            System.out.println("size:" + fileSize);
            DataOutputStream outsize = new DataOutputStream(
                    socket.getOutputStream());
            outsize.writeLong(transferFile.length());

            // make bytearray from file
            byte[] bytearray = new byte[(int) transferFile.length()];

            FileInputStream fin = new FileInputStream(transferFile);
            BufferedInputStream bin = new BufferedInputStream(fin);
            bin.read(bytearray, 0, bytearray.length);

            System.out.println(bytearray.length);


            // open OutputStream and write bytearray
            OutputStream os = socket.getOutputStream();
            os.write(bytearray, 0, bytearray.length);
            os.flush();
            fin.close();
            bin.close();
            socket.close();
            System.out.println(fileName + " transfer complete");
        } catch (IOException ex) {
            System.out.println(ex);
        }

    }

}

这里是文件接收器:

public class FileReceiverThread implements Runnable {

    // Make a socket
    protected Socket socket = null;
    protected String fileName = null;
    protected Long fileSize = null;
    protected int port = 8500;

    public FileReceiverThread(String clientIp, String name, Long size, int randomPort)
            throws IOException {
        // get socket connection
        this.port = randomPort;
        System.out.println(clientIp);
        Socket socket = new Socket(clientIp, port);
        System.out.println("Connected to: " + socket);
        this.socket = socket;
        this.fileName = name;
        this.fileSize = size;
    }

    public void run() {
        int bytesRead;
        int currentTot = 0;
        int size = fileSize.intValue();

        // Create a buffer to hold temporary data
        byte[] byteArray = new byte[size];

        // Collect data
        InputStream iStream;
        try {
            iStream = socket.getInputStream();
            // Open a new file to write to
            FileOutputStream foStream = new FileOutputStream("files/replicated/" + fileName);
            // Write data to file via byteArray
            BufferedOutputStream boStream = new BufferedOutputStream(foStream);
            // Open inputStream to read from server
            bytesRead = iStream.read(byteArray, 0, byteArray.length);
            currentTot = bytesRead;

            do {
                // Read from inputStream into byteArray (buffer), start offset
                // in byteArray, maximum bytes to read from server in total
                // bytesRead is the number of bytes read in one loop from server
                bytesRead = iStream.read(byteArray, currentTot,
                        (byteArray.length - currentTot));

                // return value from bytesRead
                if (bytesRead >= 0) {
                    currentTot += bytesRead;
                }
                // if return value > 0, the file has been downloaded
            } while (bytesRead > -1);

            // Finish and write to file
            boStream.write(byteArray, 0, currentTot);
            System.out.println(fileName + " saved");
            // Close and empty outputBuffer
            boStream.flush();
            boStream.close();
            foStream.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

我们在主要课程中实际做的是:

  1. 在8500&之间找到一个randomport 65000
  2. 在发件人端打开一个新套接字&接收方在同一端口上
  3. 发送文件 - 接收文件
  4. 关闭文件
  5. 关闭套接字

0 个答案:

没有答案