Java 套接字,正在接收空文件

Java Sockets, Receiving Empty File

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

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

  1. 第一次尝试:没有任何反应,没有错误,没有发送文件。
  2. 第二次尝试:Java 抛出 Ip already in use 的错误,但我在我的服务器 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();
    }


    }


}

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

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

否则您可能会丢失数据。

注意

  • Java 不会抛出错误 'IP already in use'。要准确。
  • 你不需要睡觉。不要将睡眠添加到网络代码中。它没有解决任何问题。

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

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