Java套接字,接收空文件

时间:2016-01-21 00:06:01

标签: java file sockets transfer

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

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

  1. 第一次尝试:没有任何事情发生,没有错误,没有发送文件。
  2. 第二次尝试:Java抛出了已经使用的Ip错误,但是我在服务器PC上收到了该文件但没有数据。
  3. 以下是客户的代码:

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

2 个答案:

答案 0 :(得分:0)

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

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

否则您可能会丢失数据。

NB

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

答案 1 :(得分:-1)

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

然而,EJP我会建议你在频道上写字时进行更好的字节检查。