Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对等方重置连接:套接字写入错误(android/server)_Java_Android_Sockets_Networking_File Sharing - Fatal编程技术网

Java 对等方重置连接:套接字写入错误(android/server)

Java 对等方重置连接:套接字写入错误(android/server),java,android,sockets,networking,file-sharing,Java,Android,Sockets,Networking,File Sharing,我的java项目有一个问题(它会将文件从服务器发送到客户端…),它总是在几秒钟后告诉我 “由对等方重置连接:套接字写入错误”如果有人有answear,请告诉我的代码有什么问题…..我最近遇到了这个问题:试试这个 package main; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java

我的java项目有一个问题(它会将文件从服务器发送到客户端…),它总是在几秒钟后告诉我
“由对等方重置连接:套接字写入错误”如果有人有answear,请告诉我的代码有什么问题…..

我最近遇到了这个问题:试试这个

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;
}

}
同时检查您是否在同一WLAN连接上(设备和服务器本身)


我希望这能帮你做点什么

所以我试过了,但没用<代码>尝试{this.socket.setSendBufferSize(8192*8192);this.socket.setReceiveBufferSize(8192*8192);this.socket.setKeepAlive(false);}catch(SocketException e){//TODO自动生成的catch块e.printStackTrace();}它只是保持循环unitl(服务器)8200…但是客户端只收到了几个数据包…但是如果我放慢速度(使用线程它会有一点帮助,但是上传或下载任何较大的文件需要很长时间…)这两者都不能修复OP.okey中的错误,那么什么能修复这个问题呢?修复程序不起作用,仍然有相同的错误。
setBufferSize(1024*1024);
setKeepAlive(false);