[分布式java]基于JavaAPI实现消息方式的系统间通信:UDP/IP+NIO
来源:互联网 发布:ios禁止软件联网 编辑:程序博客网 时间:2024/04/30 07:39
基于JavaAPI实现消息方式的系统通信:UDP/IP+NIO,与UDP/IP+BIO类似,只不过NIO是非阻塞。使用上与TCP/IP+NIO类似,也是用到了Selector。
* DatagramChannel负责监听和读写,ByteBuffer用于数据流传输
代码实现:
public class UdpIpNioJavaMethod { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { new UdpIpNioJavaMethod().send(); } }).start(); new Thread(new Runnable() { @Override public void run() { new UdpIpNioJavaMethod().receive(); } }).start(); } public void send(){ DatagramChannel sendChannel=null; Selector selector=null; try { sendChannel=DatagramChannel.open(); sendChannel.configureBlocking(false);//设置非阻塞 SocketAddress target=new InetSocketAddress("127.0.0.1",1234); sendChannel.connect(target); selector=Selector.open(); sendChannel.register(selector, SelectionKey.OP_WRITE); while(true){ if(selector.select()==0){ continue; } Iterator<SelectionKey> keyIter=selector.selectedKeys().iterator(); while(keyIter.hasNext()){ SelectionKey key=keyIter.next(); keyIter.remove(); if(key.isWritable()){ ByteBuffer buffer=ByteBuffer.wrap(new String("Hello,I am Server!").getBytes()); DatagramChannel channel= (DatagramChannel) key.channel(); long successedNum=channel.write(buffer);//向信道中写数据 System.out.println("endoff send msg to client,send successed char :"+successedNum); } } return; } } catch (IOException e) { e.printStackTrace(); }finally { try { sendChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } public void receive(){ DatagramChannel receiveChannel=null; Selector selector=null; try { receiveChannel=DatagramChannel.open(); receiveChannel.configureBlocking(false);//设置非阻塞 DatagramSocket socket=receiveChannel.socket(); socket.bind(new InetSocketAddress(1234)); selector=Selector.open(); receiveChannel.register(selector, SelectionKey.OP_READ); while(true){ if(selector.select()==0){ continue; } Iterator<SelectionKey> keyIter=selector.selectedKeys().iterator(); while(keyIter.hasNext()){ SelectionKey key=keyIter.next(); keyIter.remove(); if(key.isReadable()){ DatagramChannel channel= (DatagramChannel) key.channel(); ByteBuffer buffer=ByteBuffer.allocate(2000); channel.receive(buffer);//注意UDP/IP+NIO这个是receive String msg=new String(buffer.array()); System.out.println("Server receive msg:"+msg); } } return; } } catch (IOException e) { e.printStackTrace(); }finally { try { receiveChannel.close(); } catch (IOException e) { e.printStackTrace(); } } }}
1、发送端DatagramChannel应该注册:sendChannel.register(selector,SelectionKey.OP_WRITE);
2、接收端DatagramChannel应该注册:receiveChannel.register(selector,SelectionKey.OP_READ);
3、DatagramChannel使用receive方法,若使用了read方法会报错: java.nio.channels.NotYetConnectedException
0 0
- [分布式java]基于JavaAPI实现消息方式的系统间通信:UDP/IP+NIO
- [分布式java]基于JavaAPI实现消息方式的系统间通信:TCP/IP+NIO
- [分布式java]基于JavaAPI实现消息方式的系统间通信:UDP/IP+BIO
- [分布式java]基于JavaAPI实现消息方式的系统间通信:TCP/IP+BIO
- java分布式开发TCP/IP NIO无阻塞 Socket((基于消息方式实现系统间的通信) )(转)
- 分布式Java应用之基于消息方式实现系统间的通信(2)
- Java——基于java自身包实现消息系统间的通信(TCP/IP+NIO)
- 基于java自身技术实现消息方式的系统间通信
- 基于消息实现系统间的通信(BIO,NIO,AIO)学习。
- 基于消息实现系统间的通信(BIO,NIO,AIO)
- 基于消息实现系统间的通信(BIO,NIO,AIO)
- Java——基于java自身包实现消息系统间的通信(TCP/IP+BIO)
- Java TCP/IP Socket,基于NIO的TCP通信
- 基于java的nio消息实现方式优缺点分析及示例代码说明
- java分布式,基于开源框架实现消息方式
- 基于WebService实现远程调用方式的系统间通信
- 基于WebService实现远程调用方式的系统间通信
- 分布式Java应用---实现系统间的通信
- 学习Qt5(1)
- Win32 to .NET API Map
- Gym 100338H High Speed Trains(高精度)
- 【Lua】访问C#中的属性和方法-特殊情况-带有out和ref关键字
- 寄存器概念整理
- [分布式java]基于JavaAPI实现消息方式的系统间通信:UDP/IP+NIO
- java快捷键和注释规范
- 黑马程序员——多线程——多线程概述,实现,控制与安全问题的解决
- list<T>类
- Leetcode #226 Invert Binary Tree
- javascript中的函数Function
- 华为OJ题2---字符串最后一个单词的长度
- mac 设置 HOMEBREW_GITHUB_API_TOKEN的正确方法
- 开源框架-ormlite-android数据库的使用