首页 > 学院 > 开发设计 > 正文

通过零拷贝实现有效数据传输

2019-11-17 04:17:42
字体:
来源:转载
供稿:网友

源:http://www.ibm.com/developerworks/cn/java/j-zerocopy/index.html

主要看代码就可以了.

view plaincopy to clipboardPRint?
public void testSendfile() throws IOException {  
        String host = "localhost";  
        int port = 9026;  
        SocketAddress sad = new InetSocketAddress(host, port);  
        SocketChannel sc = SocketChannel.open();  
        sc.connect(sad);  
        sc.configureBlocking(true);  
 
        String fname = "sendfile/NetworkInterfaces.c";  
        long fsize = 183678375L, sendzise = 4094;  
          
        // FileProposerExample.stuffFile(fname, fsize);  
        FileChannel fc = new FileInputStream(fname).getChannel();  
            long start = System.currentTimeMillis();  
        long nsent = 0, curnset = 0;  
        curnset =  fc.transferTo(0, fsize, sc);  
        System.out.println("total bytes transferred--"+curnset+" and time taken in MS--"+(System.currentTimeMillis() - start));  
        //fc.close();  
      } 
public void testSendfile() throws IOException {
     String host = "localhost";
     int port = 9026;
     SocketAddress sad = new InetSocketAddress(host, port);
     SocketChannel sc = SocketChannel.open();
     sc.connect(sad);
     sc.configureBlocking(true);

     String fname = "sendfile/NetworkInterfaces.c";
     long fsize = 183678375L, sendzise = 4094;
    
     // FileProposerExample.stuffFile(fname, fsize);
     FileChannel fc = new FileInputStream(fname).getChannel();
            long start = System.currentTimeMillis();
     long nsent = 0, curnset = 0;
     curnset =  fc.transferTo(0, fsize, sc);
     System.out.println("total bytes transferred--"+curnset+" and time taken in MS--"+(System.currentTimeMillis() - start));
     //fc.close();
   }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表