天天看點

Java NIO——6 基于非阻塞程式設計UDP NIO的例子

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

好吧,承接上篇文章,下面給出一個udp不可靠無連接配接的例子,他的次傳送都是一個udp封包,不向上面文章中tcp是基于流的

代碼:

Server:

[java]  view plain  copy  print ?

  1. public class UDPServer  
  2. {  
  3.     DatagramChannel channel;  
  4.     Selector selector;  
  5.     public void work()  
  6.     {  
  7.         try  
  8.         {  
  9.             // 打開一個UDP Channel  
  10.             channel = DatagramChannel.open();  
  11.             // 設定為非阻塞通道  
  12.             channel.configureBlocking(false);  
  13.             // 綁定端口  
  14.             channel.socket().bind(new InetSocketAddress(8080));  
  15.             // 打開一個選擇器  
  16.             selector = Selector.open();  
  17.             channel.register(selector, SelectionKey.OP_READ);  
  18.         } catch (Exception e)  
  19.         {  
  20.             e.printStackTrace();  
  21.         }  
  22.         ByteBuffer byteBuffer = ByteBuffer.allocate(65536);  
  23.         while (true)  
  24.         {  
  25.             try  
  26.             {  
  27.                 // 進行選擇  
  28.                 int n = selector.select();  
  29.                 if (n > 0)  
  30.                 {  
  31.                     // 擷取以選擇的鍵的集合  
  32.                     Iterator iterator = selector.selectedKeys().iterator();  
  33.                     while (iterator.hasNext())  
  34.                     {  
  35.                         SelectionKey key = (SelectionKey) iterator.next();  
  36.                         // 必須手動删除  
  37.                         iterator.remove();  
  38.                         if (key.isReadable())  
  39.                         {  
  40.                             DatagramChannel datagramChannel = (DatagramChannel) key  
  41.                                     .channel();  
  42.                             byteBuffer.clear();  
  43.                             // 讀取  
  44.                             InetSocketAddress address = (InetSocketAddress) datagramChannel  
  45.                                     .receive(byteBuffer);  
  46.                             System.out.println(new String(byteBuffer.array()));  
  47.                             // 删除緩沖區中的資料  
  48.                             byteBuffer.clear();  
  49.                             String message = "data come from server";  
  50.                             byteBuffer.put(message.getBytes());  
  51.                             byteBuffer.flip();  
  52.                             // 發送資料  
  53.                             datagramChannel.send(byteBuffer, address);  
  54.                         }  
  55.                     }  
  56.                 }  
  57.             } catch (Exception e)  
  58.             {  
  59.                 e.printStackTrace();  
  60.             }  
  61.         }  
  62.     }  
  63.     public static void main(String[] args)  
  64.     {  
  65.         new UDPServer().work();  
  66.     }  
  67. }  

用戶端:

[java]  view plain  copy  print ?

  1. public class UDPClient  
  2. {  
  3.     DatagramChannel channel;  
  4.     Selector selector;  
  5.     public void work()  
  6.     {  
  7.         try  
  8.         {  
  9.             // 開啟一個通道  
  10.             channel = DatagramChannel.open();  
  11.             channel.configureBlocking(false);  
  12.             SocketAddress sa = new InetSocketAddress("localhost", 8080);  
  13.             channel.connect(sa);  
  14.         } catch (Exception e)  
  15.         {  
  16.             e.printStackTrace();  
  17.         }  
  18.         try  
  19.         {  
  20.             selector = Selector.open();  
  21.             channel.register(selector, SelectionKey.OP_READ);  
  22.             channel.write(Charset.defaultCharset().encode("data come from client"));  
  23.         } catch (Exception e)  
  24.         {  
  25.             e.printStackTrace();  
  26.         }  
  27.         ByteBuffer byteBuffer = ByteBuffer.allocate(100);  
  28.         while (true)  
  29.         {  
  30.             try  
  31.             {  
  32.                 int n = selector.select();  
  33.                 if (n > 0)  
  34.                 {  
  35.                     Iterator iterator = selector.selectedKeys().iterator();  
  36.                     while (iterator.hasNext())  
  37.                     {  
  38.                         SelectionKey key = (SelectionKey) iterator.next();  
  39.                         iterator.remove();  
  40.                         if (key.isReadable())  
  41.                         {  
  42.                             channel = (DatagramChannel) key.channel();  
  43.                             channel.read(byteBuffer);  
  44.                             System.out.println(new String(byteBuffer.array()));  
  45.                             byteBuffer.clear();  
  46.                             channel.write(Charset.defaultCharset().encode(  
  47.                                     "data come from client"));  
  48.                         }  
  49.                     }  
  50.                 }  
  51.             } catch (Exception e)  
  52.             {  
  53.                 e.printStackTrace();  
  54.             }  
  55.         }  
  56.     }  
  57.     public static void main(String[] args)  
  58.     {  
  59.         new UDPClient().work();  
  60.     }  
  61. }