如何使用套接字通道发送文件名和文件内容

How to send file name and file content using socket channel

我想使用 socketchannel 一起发送文件名和文件内容。 我试图将文件名转换为字节,将这些字节包装在字节缓冲区中,然后将这些缓冲区内容发送到客户端(这是在服务器端)。

在客户端,我尝试遍历缓冲区中的内容,将字节转换为字符并检查是否存在特殊字符以记录文件名的结尾。识别出该字符后,我调用缓冲区方法 compact() 以便我现在可以开始读取内容。但这不起作用!我的客户端首先 while 循环 while(bb.hasRemaining()) 没有中断,并且此循环没有打印任何字符!

服务器端

FileChannel sbc;
    ServerSocketChannel ssc=null;
    SocketChannel clientchannel=null;

try { 

 ssc=ServerSocketChannel.open();
 ssc.bind(new InetSocketAddress(5002));

 clientchannel=ssc.accept();

String filename=f.getName()+"?";
  byte[] nameinbytes=filename.getBytes("UTF-8");
System.out.println("name of file to send: "+filename);
ByteBuffer namebuffer=ByteBuffer.wrap(nameinbytes);
clientchannel.write(namebuffer);

                sbc=FileChannel.open(f.toPath());
                 ByteBuffer buff=ByteBuffer.allocate(10000000);

                 int bytesread=sbc.read(buff);
                 double read=(double)bytesread;
                 while(bytesread != -1){
                read+=(double) bytesread;
                buff.flip();
               clientchannel.write(buff);
                buff.clear();
                System.out.println("current position: "+sbc.position());
                bytesread=sbc.read(buff);
                 }
  System.out.println("file data written");

客户端

SocketAddress address=new InetSocketAddress(InetAddress.getLocalHost(),5002);
     clientChannel=SocketChannel.open(address);
     ByteBuffer bb=ByteBuffer.allocate(10000000);
     int bytesRead=clientChannel.read(bb);
     String filename="";
     while(bb.hasRemaining()){
         byte bm=bb.get();
         char c=(char)(bm & 0xFF);
         System.out.println(c);
         if(c != '?'){
             filename+=Character.toString(c);
         }else{
             bb.compact();
             break;
         }
     }

      File file=new File("C:\Users\C-I-C\Desktop\fromclient\"+filename);

     bout =new FileOutputStream(file);
      sbc=bout.getChannel();

     while(bytesRead != -1){
       bb.flip();
       sbc.write(bb);
       bb.clear();
      bytesRead=clientChannel.read(bb);
     }
     System.out.println("received: "+filename);

Q

如何使用同一个通道同时发送文件名和文件内容?

服务器端

  1. 将文件名转换为字节数组。
  2. 将字节数组包装在 ByteBuffer 对象中。
  3. 将ByteBuffer对象内容发送到socket通道

发送包含文件名(当然以字节为单位)的缓冲区后:

  1. 现在创建另一个 ByteBuffer 对象并给它一个大小
  2. 开始将文件内容从 FileChannel 对象读取到缓冲区。
  3. 在 while 循环中,开始将内容发送到套接字通道,直到到达文件通道的文件末尾。

    ServerSocket server=ServerSocket.open();
    server.bind(new InetSocketAddress(1000));
    SocketChannel clientChannel= server.accept();
    File fileToSend=new File("stalkunderflow.txt").
    String filename=fileToSend.getName();
    byte[] nameBytes=filename.getBytes("UTF-8");
    ByteBuffer nameBuffer=ByteBuffer.wrap(nameBytes);
    clientChannel.write(nameBuffer);
    
    //now  prepare and send file contents
    
    FileChannel sbc=FileChannel.open(fileToSend.toPath());
                 ByteBuffer buff=ByteBuffer.allocate(10000000);
    
                 int bytesread=sbc.read(buff);
    
                 while(bytesread != -1){
                buff.flip();
               clientChannel.write(buff);
                buff.compact();
                bytesread=sbc.read(buff);
                 }
    

客户端

  1. 创建一个 ByteBuffer 对象(可以叫它 nameBuffer)并给它一个大小(不要大)。

  2. 将套接字通道中的内容写入缓冲区

  3. 翻转缓冲区并开始将缓冲区内容写入字节数组。(在 while 循环中执行)

  4. 将字节数组转换为字符串,然后您就有了文件名。

  5. 之后,创建另一个 ByteBuffer 对象(称之为 nameBuffer)来存储文件内容,现在您将从该 nameBuffer 读取到一个文件通道,该文件通道使用您获得的名称写入文件来自 nameBuffer.

     //this is a test enviroment,therefore my server is running on the same machine as the client. 
    SocketAddress address=new InetSocketAddress(InetAddress.getLocalHost(),1000);
     SocketChannel clientChannel=SocketChannel.open(address);
    
     ByteBuffer namebuff=ByteBuffer.allocate(500);
     clientChannel.read(namebuff);
    
    byte[] namebyte=new byte[500];
    String filename="";
    
    int position=namebuff.position();
    
     while(namebuff.hasRemaining()){
        namebyte[position]=namebuff.get();
        position=namebuff.position();
     }
     filename=new String(namebyte,0,position);
    
     File file=new File(filename);
    
     ByteBuffer bb=ByteBuffer.allocate(10000000);
     int bytesRead=clientChannel.read(bb);
     FileOutputStream bout =new FileOutputStream(file);
      FileChannel sbc=bout.getChannel();
    
     while(bytesRead != -1){
       bb.flip();
       sbc.write(bb);
       bb.compact();
      bytesRead=clientChannel.read(bb);
     }