当前位置:首页 > 编程开发

使用ServerSocketChannel实现的file

webgou17年前 (2009-10-10)编程开发162

http://blog.csdn.net/java2000_net/archive/2008/06/10/2529015.aspx
http://www.java2000.net/viewthread.jsp?tid=6079

http://topic.csdn.net/u/20080610/02/6e33be0a-152f-481a-a10e-f9c11c8fd9ad.html?seed=577363641
   1. package test.io; 
   2.  
   3. import java.nio.channels.*; 
   4. import java.nio.charset.*; 
   5. import java.net.*; 
   6. import java.io.*; 
   7. import java.util.*; 
   8. import java.nio.*; 
   9.  
  10. public class FileServer { 
  11.   private int port = 8050; 
  12.  
  13.   private ServerSocketChannel serverSocketChannel; 
  14.  
  15.   private Charset charset = Charset.forName("GBK"); 
  16.  
  17.   private Selector selector = null; 
  18.  
  19.   public FileServer() throws IOException { 
  20.     selector = Selector.open(); 
  21.     serverSocketChannel = ServerSocketChannel.open(); 
  22.     serverSocketChannel.socket().setReuseAddress(true); 
  23.     serverSocketChannel.socket().bind(new InetSocketAddress(port)); 
  24.     System.out.println("服务器启动"); 
  25.   } 
  26.  
  27.   /* 编码过程 */ 
  28.   public ByteBuffer encode(String str) { 
  29.     return charset.encode(str); 
  30.   } 
  31.  
  32.   /* 解码过程 */ 
  33.   public String decode(ByteBuffer bb) { 
  34.     return charset.decode(bb).toString(); 
  35.   } 
  36.  
  37.   /* 服务器服务方法 */ 
  38.   public void service() throws IOException { 
  39.     serverSocketChannel.configureBlocking(false); 
  40.     serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); 
  41.     /** 外循环,已经发生了SelectionKey数目 */ 
  42.     while (selector.select() > 0) { 
  43.       /* 得到已经被捕获了的SelectionKey的集合 */ 
  44.       Iterator iterator = selector.selectedKeys().iterator(); 
  45.       while (iterator.hasNext()) { 
  46.         SelectionKey key = null; 
  47.         try { 
  48.           key = (SelectionKey) iterator.next(); 
  49.           iterator.remove(); 
  50.           if (key.isAcceptable()) { 
  51.             ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); 
  52.             SocketChannel sc = ssc.accept(); 
  53.             System.out.println("客户端机子的地址是 " + sc.socket().getLocalAddress() + "  客户端机机子的端口号是 " + sc.socket().getLocalPort()); 
  54.             sc.configureBlocking(false); 
  55.             ByteBuffer buffer = ByteBuffer.allocate(1024); 
  56.             sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, buffer); 
  57.           } 
  58.           if (key.isReadable()) { 
  59.             reveice(key); 
  60.           } 
  61.           if (key.isWritable()) { 
  62.             send(key); 
  63.           } 
  64.         } catch (IOException e) { 
  65.           e.printStackTrace(); 
  66.           try { 
  67.             if (key != null) { 
  68.               key.cancel(); 
  69.               key.channel().close(); 
  70.             } 
  71.           } catch (ClosedChannelException cex) { 
  72.             e.printStackTrace(); 
  73.           } 
  74.         } 
  75.       } 
  76.       /* 内循环完 */ 
  77.     } 
  78.     /* 外循环完 */ 
  79.   } 
  80.  
  81.   /* service方法完 */ 
  82.   public void reveice(SelectionKey key) throws IOException { 
  83.     if (key == null) 
  84.       return; 
  85.     ByteBuffer buff = (ByteBuffer) key.attachment(); 
  86.     SocketChannel sc = (SocketChannel) key.channel(); 
  87.     buff.limit(buff.capacity()); 
  88.     buff.position(0); 
  89.     sc.read(buff); 
  90.     buff.flip(); 
  91.     String reviceData = decode(buff); 
  92.   } 
  93.  
  94.   /* 发送文件 */ 
  95.   public void send(SelectionKey key) { 
  96.     if (key == null) 
  97.       return; 
  98.     ByteBuffer buff = (ByteBuffer) key.attachment(); 
  99.     SocketChannel sc = (SocketChannel) key.channel(); 
 100.     String data = decode(buff); 
 101.     if (data.indexOf("get") == -1) 
 102.       return; 
 103.     String subStr = data.substring(data.indexOf(" "), data.length()); 
 104.     System.out.println("截取之后的字符串是 " + subStr); 
 105.     FileInputStream fileInput = null; 
 106.     try { 
 107.       fileInput = new FileInputStream(subStr); 
 108.       FileChannel fileChannel = fileInput.getChannel(); 
 109.       fileChannel.transferTo(0, fileChannel.size(), sc); 
 110.     } catch (IOException e) { 
 111.       e.printStackTrace(); 
 112.     } finally { 
 113.       try { 
 114.         fileInput.close(); 
 115.       } catch (IOException ex) { 
 116.         ex.printStackTrace(); 
 117.       } 
 118.     } 
 119.   } 
 120.  
 121.   public static void main(String[] args) throws IOException { 
 122.     new FileServer().service(); 
 123.   } 
 124. }  

扫描二维码推送至手机访问。

版权声明:本文由知了博客发布,如需转载请注明出处。

本文链接:https://www.webgou.info/?id=38

标签: javasocket
分享给朋友:

“使用ServerSocketChannel实现的file” 的相关文章

实用Android开发工具和资源精选

\ 本文介绍了20个关于Android应用程序开发的实用工具资源,供大家分享。     在google、开源平台,和来自移动电话制造商HTC,Samsung和Sony Ericsson的支持下,Android平台在市场占有率上相比去年取得的886%增长!如果我只看增长率…

3D动画基础

顶点动画...…

Unity反编译备忘录

1. disunity+disunityGUI编译后可执行文件地址:https://github.com/ata4/disunity/releasesdisunity在github源码地址:https://github.com/ata4/disunity 2.DevXUnity-Unpac…

DirectShow编程(2)- 开始DirectShow旅程

2. 开始DirectShow旅程    这个章节的内容主要是编写DirectShow应用所需的一些基本概念,可以把它当作一个高级介绍,理解这些内容只需具备一般的编程和有关多媒体的知识。2.1. 设置DirectShow开发的编译环境   …

JVM terminated. Exit code=13 问提解决

在ubuntu 10.4 下载eclipse出现: JVM terminated. Exit code=13 原因是:eclipse 32安装在ubuntu 64 位上,应该安装eclipse 64程序…

stdcall cdecl fastcall thiscall naked call

在C语言中,假设我们有这样的一个函数:int function(int a,int b)调用时只要用result = function(1,2)这样的方式就可以使用这个函数。但是,当高级语言被编译成计算机可…

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。