【问题标题】:Java Sockets, Receiving Empty FileJava 套接字,接收空文件
【发布时间】:2016-01-21 00:06:01
【问题描述】:

我正在创建一个应用程序来从客户端发送文件并通过 Socket 使用服务器接收它。

当我在我的 PC(同一台 PC 上的客户端-服务器)中测试应用程序时,一切正常,但是当我在不同的 PC 上测试应用程序时,我遇到了以下错误。

  1. 第一次尝试:没有任何反应,没有错误,没有发送文件。
  2. 第二次尝试:Java 抛出 Ip 已在使用中的错误,但我在我的服务器 PC 上收到该文件,但其中没有数据。

这是客户端的代码:

    public class FileSender {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
FileSender nioClient = new FileSender();
SocketChannel socketChannel = nioClient.createChannel();
try {
    nioClient.sendFile(socketChannel);
} catch (FileNotFoundException | InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
//SocketChannel socketChannel = nioClient.createc
    }
public SocketChannel createChannel(){

    SocketChannel socketChannel = null;

    try {
        socketChannel = SocketChannel.open();
        SocketAddress socketAddress = new InetSocketAddress("xx.xxx.xxx.x", 10002);
        socketChannel.connect(socketAddress);
        System.out.println("Connected..Now Sending the File");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
return socketChannel;   
}

public void sendFile(SocketChannel socketChannel) throws FileNotFoundException, InterruptedException{

    RandomAccessFile afile = null;

    try {
        File file = new File("/home/dionisio/Imágenes/ImagenesOriginalesPrueba/flowers.jpg");
        afile = new RandomAccessFile(file, "r");
        FileChannel inChannel = afile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(8192);
        while (inChannel.read(buffer) != -1) {
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();     
        }
        socketChannel.close();
        afile.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

服务器代码

public class FileReceiver {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
FileReceiver nioServer = new FileReceiver();
SocketChannel socketChannel = nioServer.createServerSocketChannel();
nioServer.readFileFromSocket(socketChannel);
    }

    private SocketChannel createServerSocketChannel() {
        // TODO Auto-generated method stub
        ServerSocketChannel serverSocketChannel = null;
        SocketChannel socketChannel = null;

        try {
            serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.socket().bind(new InetSocketAddress(10002));
            socketChannel = serverSocketChannel.accept();
            System.out.println("Connection Stablished..."+socketChannel.getRemoteAddress());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return socketChannel;
    }


    private void readFileFromSocket(SocketChannel socketChannel) {
        // TODO Auto-generated method stub

    RandomAccessFile afile = null;

    try {
        afile = new RandomAccessFile("/home/dionisio/Imágenes/imagenesCopiaPrueba/flowersCopia.jpg","rw");
        ByteBuffer buffer = ByteBuffer.allocate(8192);
        FileChannel fileChannel = afile.getChannel();
        while (socketChannel.read(buffer)>0) {
            buffer.flip();
            fileChannel.write(buffer);
            buffer.clear();
        }
        Thread.sleep(1000);
        fileChannel.close();
        System.out.println("End of file reached...Closing Channel");
        socketChannel.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    }


}

【问题讨论】:

    标签: java file sockets transfer


    【解决方案1】:

    您的读取循环不正确。它们应该是以下形式:

    while (in.read(buffer) > 0 || buffer.position() > 0)
    {
        buffer.flip();
        out.write(buffer);
        buffer.compact();
    }
    

    否则您可能会丢失数据。

    注意

    • Java 不会抛出错误“IP 已在使用中”。准确无误。
    • 你不需要睡觉。不要在网络代码中添加睡眠。它不能解决任何问题。

    【讨论】:

    • 您好 EJP,感谢您的提示,我将尝试这样做,看看会发生什么。但是在测试你的代码之前,是否有理由让我的代码在我的本地主机上工作(显然还没有你的新代码)但不能在不同的电脑上工作?
    • 我已经尝试过你的代码,但我仍然遇到同样的问题 :( 啊,如果你看到我的代码,你可以看到我有一个 println,上面写着“连接已建立”,没有显示输出在不同的 PC 中但在 localhost 中。我不知道问题是否来自我在 Linux 中运行客户端和在 Windows 中运行服务器这一事实,这应该不是问题,因为 Java 是跨平台的。
    【解决方案2】:

    我的人感谢您的回复,现在我知道我的错误是什么,我在客户端应用程序上输入了错误的 IP 地址。

    不过,EJP 我会采纳您的建议,以便在 Channel 上写入时进行更好的字节检查。

    【讨论】:

    • 如果您的 IP 地址错误,您将无法收到空文件。
    猜你喜欢
    • 1970-01-01
    • 2014-06-20
    • 2016-12-08
    • 1970-01-01
    • 2020-12-21
    • 2021-12-16
    • 1970-01-01
    • 2016-10-06
    • 2011-07-28
    相关资源
    最近更新 更多