十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Java的NIO包中,有一个专门用于发送UDP数据包的类:DatagramChannel,UDP是一种无连接的网络协议,
一般用于发送一些准确度要求不太高的数据等。
完整的服务端程序如下:
public class StatisticsServer { //每次发送接收的数据包大小 private final int MAX_BUFF_SIZE = 1024 * 10; //服务端监听端口,客户端也通过该端口发送数据 private int port; private DatagramChannel channel; private Selector selector; private ScheduledExecutorService es = Executors.newScheduledThreadPool(1); public void init() throws IOException { //创建通道和选择器 selector = Selector.open(); channel = DatagramChannel.open(); //设置为非阻塞模式 channel.configureBlocking(false); channel.socket().bind(new InetSocketAddress(port)); //将通道注册至selector,监听只读消息(此时服务端只能读数据,无法写数据) channel.register(selector, SelectionKey.OP_READ); //使用线程的方式,保证服务端持续等待接收客户端数据 es.scheduleWithFixedDelay(new Runnable() { @Override public void run() { try { while(selector.select() > 0) { Iteratoriterator = selector.selectedKeys().iterator(); while(iterator.hasNext()) { SelectionKey key = iterator.next(); try { iterator.remove(); if(key.isReadable()) { //接收数据 doReceive(key); } } catch (Exception e) { logger.error("SelectionKey receive exception", e); try { if (key != null) { key.cancel(); key.channel().close(); } } catch (ClosedChannelException cex) { logger.error("Close channel exception", cex); } } } } } catch (IOException e) { logger.error("selector.select exception", e); } } }, 0L, 2L, TimeUnit.MINUTES); } //处理接收到的数据 private void doReceive(SelectionKey key) throws IOException { String content = ""; DatagramChannel sc = (DatagramChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(MAX_BUFF_SIZE); buffer.clear(); sc.receive(buffer); buffer.flip(); while(buffer.hasRemaining()) { byte[] buf = new byte[buffer.limit()]; buffer.get(buf); content += new String(buf); } buffer.clear(); logger.debug("receive content="+content); if(StringUtils.isNotBlank(content)) { doSave(content); } } }