0
package main;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.Socket;

public class FileTransfer {

private Socket socket;
private static final int MAX_BUFFER = 8192;

public FileTransfer(Socket socket) {
    this.socket = socket;
}

public boolean sendFile(File file) {

    boolean errorOnSave = false;
    long length = file.length();

    if (file.exists()) {

        FileInputStream in = null;
        DataOutputStream out = null;

        try {
            in = new FileInputStream(file);
            out = new DataOutputStream(this.socket.getOutputStream());

            out.writeLong(length);
            out.flush();

            byte buffer[] = new byte[MAX_BUFFER];
            int read = 0;
            int i=0;
            while ((read = in.read(buffer)) != -1) {
                System.out.println(i);
                i++;
                out.write(buffer, 0, read);
                out.flush();
                buffer = new byte[MAX_BUFFER];
            }

        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            return false;
        } catch (IOException e) {
            System.out.println("An error has occurred when try send file " + file.getName() + " \nSocket: "
                    + socket.getInetAddress() + ":" + socket.getPort() + "\n\t" + e);
            errorOnSave = true;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    System.out.println("An error has occurred when closing the InputStream of the file "
                            + file.getName() + "\n\t" + e.getMessage());
                }
            }

        }
        return !errorOnSave;
    } else {
        return false;
    }
}

public boolean saveFile(File fileSave) {
    RandomAccessFile file = null;
    DataInputStream in = null;

    boolean errorOnSave = false;
    try {
        file = new RandomAccessFile(fileSave, "rw");

        file.getChannel().lock();

        in = new DataInputStream(this.socket.getInputStream());
        long fileSize = in.readLong();

        byte buffer[] = new byte[MAX_BUFFER];
        int read = 0;

        while ((fileSize > 0) && ((read = in.read(buffer, 0, (int) Math.min(buffer.length, fileSize))) != -1)) {
            file.write(buffer, 0, read);
            fileSize -= read;
            buffer = new byte[MAX_BUFFER];
        }

    } catch (FileNotFoundException e1) {
        System.out.println(e1.getMessage());
        return false;
    } catch (IOException e) {
        System.out.println("An error has occurred when saving the file\n\t" + e.getMessage());
        errorOnSave = true;
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (IOException e) {
                System.out.println(
                        "An error occurred when closing the file " + fileSave.getName() + "\n\t" + e.getMessage());
                errorOnSave = true;
            }
        }
        if (errorOnSave) {
            if (fileSave.exists()) {
                fileSave.delete();
            }
        }

    }
    return !errorOnSave;
}

}

I have a problem with my java project(its going to send files from server to client...) its always telling me after a few seconds "Connection reset by peer: socket write error" if someone has the answear please tell whats wrong with my code.....

1 Answers1

-1

I had this issue recently: Try this

setBufferSize(1024*1024);
setKeepAlive(false);

Also check if you are on the same WLAN connection (both your device and the server itself)

I hope this helps you get something going!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
  • so i have tried this but it didnt help..... `try { this.socket.setSendBufferSize(8192*8192); this.socket.setReceiveBufferSize(8192*8192); this.socket.setKeepAlive(false); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); }` it just kept looping unitl (server) 8200 ....but the client received only a few packets....but if I slow it down (with thread it will help a littlebit,but it takes soo long to upload or download any larger file....) – Daniel Maly Jul 10 '16 at 20:43
  • Neither of these will fix the error in the OP. – user207421 Jul 10 '16 at 22:11
  • okey, what will fix this problem ? – Daniel Maly Jul 11 '16 at 10:07