Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 尝试传输文件时发生套接字关闭异常_Java_Sockets_Networking_Tcp - Fatal编程技术网

Java 尝试传输文件时发生套接字关闭异常

Java 尝试传输文件时发生套接字关闭异常,java,sockets,networking,tcp,Java,Sockets,Networking,Tcp,我正在尝试使用Java和TCP将文件从服务器传输到客户端,但是在客户端我得到了一个套接字关闭异常,而服务器在尝试传输文件时没有错误。我对这个错误感到困惑,因为我在尝试读取之前没有关闭套接字。服务器接受连接并发送文件,但客户端收到该错误。有什么建议吗 错误是: java.net.SocketException:套接字已关闭 服务器线程的运行函数: public void run() { System.out.println("Service thread running for clien

我正在尝试使用Java和TCP将文件从服务器传输到客户端,但是在客户端我得到了一个套接字关闭异常,而服务器在尝试传输文件时没有错误。我对这个错误感到困惑,因为我在尝试读取之前没有关闭套接字。服务器接受连接并发送文件,但客户端收到该错误。有什么建议吗

错误是:

java.net.SocketException:套接字已关闭

服务器线程的运行函数:

public void run() {
    System.out.println("Service thread running for client at " + socket.getInetAddress() + " on port " + socket.getPort());
    try {
        File file = new File("hank.txt");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        OutputStream os = socket.getOutputStream();

        byte[] contents;
        long fileLength = file.length();
        long current = 0;

        long start = System.nanoTime();
        while(current!=fileLength) {
            int size = 1000;
            if(fileLength - current >= size) {
                current += size;
            }
            else {
                size = (int)(fileLength - current);
                current = fileLength;
            }
            contents = new byte[size];
            bis.read(contents,0,size);
            os.write(contents);
            System.out.println("sending file..." + (current*100)/fileLength+"% complete!");
        }
        os.flush();
        this.socket.close();
    }catch(Exception e) {
        e.printStackTrace();
    }
}
接收文件代码的客户端:

    System.out.println("Going to get the file...");
    socket = new Socket(response.getIP().substring(1), response.getPort());

    byte[] contents = new byte[10000];
    FileOutputStream fos = new FileOutputStream("hank.txt");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    InputStream in = socket.getInputStream();

    int bytesRead = 0;
    System.out.println("Starting to read file...");
    while((bytesRead = is.read(contents))!=-1) { //the error points to this lin
        bos.write(contents,0,bytesRead);
    }

    bos.flush();
    bos.close();
    in.close();

    //
    socket.close();

此套接字的输入流在

InputStream in = socket.getInputStream();
所以


更改为。read(contents)改为in。read(contents))非常感谢您修复了它。我有一个名为“is”的ObjectInputStream,所以它没有红色下划线。我从来没有发现过更好的代码。这很有效,非常感谢。我觉得自己像个白痴我盯着这个看了30分钟。
 Change is.read(contents)) to in.read(contents))