【Java-udp】NIO非阻塞UDP通信

news/2024/5/18 14:41:16 标签: java, udp, nio

原文地址:NIO非阻塞UDP通信

java">import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;

import org.junit.Test;

public class TestNonBlockingNIO2UDP {
	
	@Test
	public void send() throws IOException{
		DatagramChannel dc = DatagramChannel.open();
		
		dc.configureBlocking(false);
		
		ByteBuffer buf = ByteBuffer.allocate(1024);
		
		Scanner scan = new Scanner(System.in);
		
		while(scan.hasNext()){
			String str = scan.next();
			buf.put((new Date().toString() + ":\n" + str).getBytes());
			buf.flip();
			dc.send(buf, new InetSocketAddress("127.0.0.1", 9898));
			buf.clear();
		}
		
		dc.close();
	}
	
	@Test
	public void receive() throws IOException{
		DatagramChannel dc = DatagramChannel.open();
		
		dc.configureBlocking(false);
		
		dc.bind(new InetSocketAddress(9898));
		
		Selector selector = Selector.open();
		
		dc.register(selector, SelectionKey.OP_READ);
		
		while(selector.select() > 0){
			Iterator<SelectionKey> it = selector.selectedKeys().iterator();
			
			while(it.hasNext()){
				SelectionKey sk = it.next();
				
				if(sk.isReadable()){
					ByteBuffer buf = ByteBuffer.allocate(1024);
					
					dc.receive(buf);
					buf.flip();
					System.out.println(new String(buf.array(), 0, buf.limit()));
					buf.clear();
				}
			}
			
			it.remove();
		}
	}

}


http://www.niftyadmin.cn/n/5331858.html

相关文章

探索设计模式的魅力:抽象工厂模式的艺术

抽象工厂模式&#xff08;Abstract Factory Pattern&#xff09;是一种创建型设计模式&#xff0c;用于在不指定具体类的情况下创建一系列相关或相互依赖的对象。它提供了一个接口&#xff0c;用于创建一系列“家族”或相关依赖对象&#xff0c;而无需指定它们的具体类。 主要参…

使用虚拟机安装AIX7.2【re】

背景 同事的项目需要在aix上安装和配置db2&#xff0c;然而客户还没有将PowerPC端末开通使用&#xff0c;所以项目的设计阶段打算在虚拟机上进行检证&#xff0c;让我帮忙准备环境。 这是什么 AIX 7.2 是 IBM 开发的一款高级交互式执行系统&#xff08;Unix系统&#xff09;&a…

【JMeter】JMeter连OceanBase数据库

1、下载OB&#xff08;OceanBase简称&#xff0c;下同&#xff09;&#xff0c;下载地址&#xff1a;https://www.oceanbase.com/softwarecenter-enterprise 2、将下载下来的jar包放到jmeter安装目录的 lib 目录下&#xff0c;或者打开JMeter客户端&#xff0c;在测试计划中引入…

上海亚商投顾:沪指探底回升 大金融板块午后走强

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。 一.市场情绪 指昨日探底回升&#xff0c;深成指、创业板指午后跌超1%&#xff0c;尾盘集体拉升翻红&#xff0c;北证50指数涨…

Flutter 综述

Flutter 综述 1 介绍1.1 概述1.2 重要节点1.3 移动开发中三种跨平台框架技术对比1.4 flutter 技术栈1.5 IDE1.6 Dart 语言1.7 应用1.8 框架 2 Flutter的主要组成部分3 资料书籍 《Flutter实战第二版》Dart 语言官网Flutter中文开发者社区flutter 官网 4 搭建Flutter开发环境参考…

Matlab中常见的数据平滑方式

文章目录 1. 移动平均&#xff1a;2. Loess平滑&#xff1a; 在 MATLAB 中&#xff0c;您可以使用不同的平滑技术对向量数据进行平滑处理。以下是其中两种常用的平滑方法&#xff1a;移动平均和Loess平滑。 1. 移动平均&#xff1a; % 示例数据 x 1:100; y randn(1, 100); …

Jenkins的环境部署,(打包、发布、部署、自动化测试)

一、Tomcat环境安装 1.安装JDK&#xff08;Java环境&#xff09; JDK下载地址&#xff1a;Java Downloads | Oracle 安装好后在系统环境变量里配置环境变量&#xff1a; ①添加JAVA_HOME 变量名&#xff1a;JAVA_HOME变量值&#xff1a;C:\Program Files\Java\jdk1.8.0_18…

How to manually install PCL on ubuntu 18.04

1. Firstly, need to check which version pcl you need to install on your ubuntu system. PCL download link: https://github.com/PointCloudLibrary/pcl/releases?page2https://github.com/PointCloudLibrary/pcl/releases?page2 Find your version: pcl-1.9.1 Then c…