Java NIO——6 基于非阻塞编程UDP NIO的例子

来源:互联网 发布:淘宝店铺密码忘了 编辑:程序博客网 时间:2024/06/05 08:39

转自 http://blog.csdn.net/chenxuegui1234/article/details/17981203

好吧,承接上篇文章,下面给出一个udp不可靠无连接的例子,他的次传送都是一个udp报文,不向上面文章中tcp是基于流的


代码:


Server:

[java] view plain copy
 print?
  1. /** 
  2.  * 服务器端 
  3.  *  
  4.  * @author Joeson 
  5.  *  
  6.  */  
  7. public class UDPServer  
  8. {  
  9.     DatagramChannel channel;  
  10.   
  11.     Selector selector;  
  12.   
  13.     public void work()  
  14.     {  
  15.         try  
  16.         {  
  17.             // 打开一个UDP Channel  
  18.             channel = DatagramChannel.open();  
  19.   
  20.             // 设定为非阻塞通道  
  21.             channel.configureBlocking(false);  
  22.             // 绑定端口  
  23.             channel.socket().bind(new InetSocketAddress(8080));  
  24.   
  25.             // 打开一个选择器  
  26.             selector = Selector.open();  
  27.             channel.register(selector, SelectionKey.OP_READ);  
  28.         } catch (Exception e)  
  29.         {  
  30.             e.printStackTrace();  
  31.         }  
  32.   
  33.         ByteBuffer byteBuffer = ByteBuffer.allocate(65536);  
  34.         while (true)  
  35.         {  
  36.             try  
  37.             {  
  38.                 // 进行选择  
  39.                 int n = selector.select();  
  40.                 if (n > 0)  
  41.                 {  
  42.                     // 获取以选择的键的集合  
  43.                     Iterator iterator = selector.selectedKeys().iterator();  
  44.   
  45.                     while (iterator.hasNext())  
  46.                     {  
  47.                         SelectionKey key = (SelectionKey) iterator.next();  
  48.   
  49.                         // 必须手动删除  
  50.                         iterator.remove();  
  51.   
  52.                         if (key.isReadable())  
  53.                         {  
  54.                             DatagramChannel datagramChannel = (DatagramChannel) key  
  55.                                     .channel();  
  56.   
  57.                             byteBuffer.clear();  
  58.                             // 读取  
  59.                             InetSocketAddress address = (InetSocketAddress) datagramChannel  
  60.                                     .receive(byteBuffer);  
  61.   
  62.                             System.out.println(new String(byteBuffer.array()));  
  63.   
  64.                             // 删除缓冲区中的数据  
  65.                             byteBuffer.clear();  
  66.   
  67.                             String message = "data come from server";  
  68.   
  69.                             byteBuffer.put(message.getBytes());  
  70.   
  71.                             byteBuffer.flip();  
  72.   
  73.                             // 发送数据  
  74.                             datagramChannel.send(byteBuffer, address);  
  75.                         }  
  76.                     }  
  77.                 }  
  78.             } catch (Exception e)  
  79.             {  
  80.                 e.printStackTrace();  
  81.             }  
  82.         }  
  83.   
  84.     }  
  85.   
  86.     public static void main(String[] args)  
  87.     {  
  88.         new UDPServer().work();  
  89.   
  90.     }  
  91.   
  92. }  


客户端:

[java] view plain copy
 print?
  1. /** 
  2.  * 客户端 
  3.  *  
  4.  * @author Joeson 
  5.  */  
  6. public class UDPClient  
  7. {  
  8.     DatagramChannel channel;  
  9.     Selector selector;  
  10.   
  11.     public void work()  
  12.     {  
  13.   
  14.         try  
  15.         {  
  16.             // 开启一个通道  
  17.             channel = DatagramChannel.open();  
  18.   
  19.             channel.configureBlocking(false);  
  20.   
  21.             SocketAddress sa = new InetSocketAddress("localhost"8080);  
  22.   
  23.             channel.connect(sa);  
  24.         } catch (Exception e)  
  25.         {  
  26.             e.printStackTrace();  
  27.         }  
  28.   
  29.         try  
  30.         {  
  31.             selector = Selector.open();  
  32.             channel.register(selector, SelectionKey.OP_READ);  
  33.             channel.write(Charset.defaultCharset().encode("data come from client"));  
  34.         } catch (Exception e)  
  35.         {  
  36.             e.printStackTrace();  
  37.         }  
  38.   
  39.         ByteBuffer byteBuffer = ByteBuffer.allocate(100);  
  40.         while (true)  
  41.         {  
  42.             try  
  43.             {  
  44.                 int n = selector.select();  
  45.                 if (n > 0)  
  46.                 {  
  47.   
  48.                     Iterator iterator = selector.selectedKeys().iterator();  
  49.   
  50.                     while (iterator.hasNext())  
  51.                     {  
  52.                         SelectionKey key = (SelectionKey) iterator.next();  
  53.                         iterator.remove();  
  54.                         if (key.isReadable())  
  55.                         {  
  56.                             channel = (DatagramChannel) key.channel();  
  57.                             channel.read(byteBuffer);  
  58.   
  59.                             System.out.println(new String(byteBuffer.array()));  
  60.                               
  61.                               
  62.                             byteBuffer.clear();  
  63.                               
  64.                             channel.write(Charset.defaultCharset().encode(  
  65.                                     "data come from client"));  
  66.                         }  
  67.                     }  
  68.                 }  
  69.             } catch (Exception e)  
  70.             {  
  71.                 e.printStackTrace();  
  72.             }  
  73.         }  
  74.   
  75.     }  
  76.   
  77.     public static void main(String[] args)  
  78.     {  
  79.         new UDPClient().work();  
  80.     }  
  81. }  

0 0
原创粉丝点击