繁体   English   中英

服务器正在通过套接字发送数据,但是客户端没有接收到它(Java)

[英]The server is sending data through the socket, but the client is not receving it (Java)

我正在使用Java编写文件存储和传输系统。 这是客户端上接收文件的代码:

public static void receiveFile(Socket socket) throws IOException{   
    String fileLocation="/home/limafoxtrottango/Downloads/receivedFile";
    int bytesRead=0;
    int current = 0;
    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;

    try {
        // receive file
        byte [] byteArray  = new byte [60022386];
        System.out.println("Waiting to receive a file...");
        //reading file from socket
        InputStream inputStream = socket.getInputStream();
        fileOutputStream = new FileOutputStream(fileLocation);
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bytesRead = inputStream.read(byteArray,0,byteArray.length);                 //copying file from socket to byteArray
        current = bytesRead;
        do {
            bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
            if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);
        bufferedOutputStream.write(byteArray, 0 , current);                         //writing byteArray to file
        bufferedOutputStream.flush();                                               //flushing buffers

        System.out.println("File " + fileLocation  + " downloaded ( size: " + current + " bytes read)");
    } catch(SocketException e){
        System.out.println("Some error occured");
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        if (fileOutputStream != null) fileOutputStream.close();
        if (bufferedOutputStream != null) bufferedOutputStream.close();
        if (socket != null) socket.close();
    }
}

接收文件时,出现以下错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:128)
    at Test.receiveFile(Test.java:211)
    at Test.main(Test.java:70)

注意:错误在代码的以下行中:

bufferedOutputStream.write(byteArray, 0 , current);  

调试之后,我发现客户端的输入流中没有任何数据,因此read()方法始终返回-1(eof)。 但是服务器正在成功发送文件。

这是服务器的代码:

public static void sendFile(Socket socket, String fileLocation)
{
    FileInputStream fileInputStream = null;
    BufferedInputStream bufferedInputStream = null;
    OutputStream outputStream = null;
    File file = new File (fileLocation);
    byte [] byteArray  = new byte [(int)file.length()];
    try {
        socket=new Socket(socket.getInetAddress(),port_no);
        fileInputStream = new FileInputStream(file);
        bufferedInputStream = new BufferedInputStream(fileInputStream);
        bufferedInputStream.read(byteArray,0,byteArray.length); // copied file into byteArray

        //sending file through socket
        outputStream = socket.getOutputStream();
        System.out.println("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)");
        outputStream.write(byteArray,0,byteArray.length);           //copying byteArray to socket
        outputStream.flush();                                       //flushing socket
        System.out.println("Done sending!");    
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是我对上述方法的调用:

sendFile(clientSocket, "/home/limafoxtrottango/Downloads/serverDownloads/"+sender);

问题是服务器已成功将字节写入流中,但客户端的输入流中似乎没有任何数据。

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[],%20int,%20int)

inputStream.read(字节阵列,0,byteArray.length); 在某些情况下,可能会返回-1(如上述文档所述)。 请迎合这种情况。

另外,我建议对客户端和服务器使用此处提供的解决方案: 将InputStream写入Java中文件的有效方法(版本6)

客户代码:

final Path destination = Paths.get(fileLocation);
try (
    final InputStream in = socket.getInputStream();
) {
    Files.copy(in, destination);
}

服务器代码:

try (
    final InputStream in = new FileInputStream(fileLocation);
) {
    Files.copy(in, socket.getOutputStream());
}

亲切的问候,巴拉

与您的标题相反,服务器未发送任何内容。 它立即关闭了连接,因此bytesRead最初为-1且从未更改,并且您无法对此进行防御,因此得到ArrayIndexOutOfBoundsException

但是,在您发布的代码中,服务器正在发送内容,但从未关闭套接字,这是您需要修复的另一个错误。 它还忽略了FileInputStream.read()返回的计数,并假定它已填充缓冲区,这不是规范的一部分。

因此,这不是真正的服务器代码,或者您正在连接其他东西,或者服务器遇到了您未提及的IOException

奇怪的是,您使用两个不同的代码段进行复制。 用Java复制流的标准方法是:

char buffer = new char[8192]; // or whatever size you prefer > 0
int count;
while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

在两端使用它。 不需要英雄般大小的缓冲区,也不需要缓冲区文件的大小,也无需假设文件大小适合int

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM