• 关于org.tio.core.udp的UDP交互使用示例


    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import java.util.Arrays;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.tio.core.Node;
    import org.tio.core.udp.UdpPacket;
    import org.tio.core.udp.UdpServer;
    import org.tio.core.udp.UdpServerConf;
    import org.tio.core.udp.intf.UdpHandler;
    
    import com.yanmade.configuration.Configuration;
    
    
    public class NewMachineUdpHandler {
    
    	private static Logger logger = LoggerFactory.getLogger(NewMachineUdpHandler.class);
    	
    	private static final String FORMAT = "$%s;%s\r\n";//$%s;%s\\r\\n	\r\n
    	
    	private static int LOCALPORT = 8090;
    	private int newMachinePort = 9090;
    	private static String newMachineHost = "172.16.55.100";
    	private static final int TIMEOUT = 5000;
    	
    	private Node remoteNode = null;
    	
    	private UdpServer server = null;
    	
    	private int frameSequenceNum = 0;
    	
    	public static void main(String[] args) {
    		newMachineHost = "192.168.230.205";
    		
    		newMachineHost = "127.0.0.1";
    		
    		
    		Configuration.loadConfiguration();
    		NewMachineUdpHandler h = new NewMachineUdpHandler();
    		h.startTestCommand();
    	}
    	
    	
    	public NewMachineUdpHandler(int lPort,String rIp,int rPort) {
    		LOCALPORT = lPort;
    		newMachinePort = rPort;
    		newMachineHost = rIp;
    	}
    	
    	public NewMachineUdpHandler() {
    		newMachinePort = Configuration.getConfigurationBean().newMachinePort;
    		newMachineHost = Configuration.getConfigurationBean().newMachineHost;
    		remoteNode = new Node(newMachineHost , newMachinePort);
    		UdpServerConf udpServerConf = new UdpServerConf(LOCALPORT, new MyUdpHandler(), TIMEOUT);
    		try {
    			logger.info("LocalIp:{},LOCALPORT:{},newMachineHost:{},newMachinePort:{},",InetAddress.getLocalHost().getHostAddress(),LOCALPORT,newMachineHost,newMachinePort);
    			server = new UdpServer(udpServerConf);
    			server.start();
    			logger.info("Success to init NewMachineUdpHandler.");
    		} catch (SocketException e) {
    			logger.error("Fail to init NewMachineUdpHandler.", e);
    		} catch (UnknownHostException e) {
    			logger.error("InetAddress.getLocalHost().getHostAddress().", e);
    			e.printStackTrace();
    		}
    	}
    	
    	public boolean startTestCommand() {
    		reset();
    		sendData(String.format(FORMAT, frameSequenceNum++%255 , "start" ));
    		for (int i = 0; i < 3; i++) {
    			try {
    				Thread.sleep(500);
    			} catch (InterruptedException e) {
    				logger.error("等待接受测试仪器的启动测试回送信息时被意外打断。errMsg:{}",e);
    				Thread.currentThread().interrupt();
    			}
    			if(!isReceiveStart) {
    				sendData(String.format(FORMAT, frameSequenceNum%255 , "start" ));
    			}else {
    				break;
    			}
    		}
    		return isReceiveStart;
    	}
    
    	public void aboortTestCommand() {
    		sendData(String.format(FORMAT, frameSequenceNum%255 , "abort" ));
    		for (int i = 0; i < 3; i++) {
    			try {
    				Thread.sleep(500);
    			} catch (InterruptedException e) {
    				logger.error("等待接受测试仪器的终止测试回送信息时被意外打断。。errMsg:{}",e);
    				Thread.currentThread().interrupt();
    			}
    			if(!isReceiveAbort) {
    				sendData(String.format(FORMAT, frameSequenceNum%255 , "abort" ));
    			}
    		}
    	}
    	
    	private void sendData(String str) {
    		logger.info("sendData:{},newMachineHost:{},newMachinePort:{}",str,remoteNode.getIp(),remoteNode.getPort());
    		server.send(str, remoteNode);
    	}
    	
    	public void stop() {
    		server.stop();
    	}
    
    	private boolean isReceiveStart = false;
    	private boolean isReceiveAbort = false;
    	private boolean isEnd = false;
    	private boolean isDready = false;
    	private boolean isDabnormal = false;
    	
    	public boolean isReceiveStart() {
    		return isReceiveStart;
    	}
    
    	public boolean isReceiveAbort() {
    		return isReceiveAbort;
    	}
    
    	public boolean isEnd() {
    		return isEnd;
    	}
    
    	public boolean isDready() {
    		return isDready;
    	}
    
    	public boolean isDabnormal() {
    		return isDabnormal;
    	}
    	
    	public boolean isTestFinished(){
    		return isEnd && (isDready || isDabnormal);
    	}
    
    	public void reset() {
    		isReceiveStart = false;
    		isReceiveAbort = false;
    		isEnd = false;
    		isDready = false;
    		isDabnormal = false;
    	}
    
    	class MyUdpHandler implements UdpHandler{
    
    		@Override
    		public void handler(UdpPacket udpPacket, DatagramSocket datagramSocket) {
    
    			logger.info("the original data:{},{},{}",Arrays.toString(udpPacket.getData()),udpPacket.getRemote(),udpPacket.getTime());
    			String msg = new String(udpPacket.getData());
    			logger.info("received data:{}",msg);
    			
    			if (msg.contains("start")) {
    				isReceiveStart = true;
    				return;
    			} else if (msg.contains("abort")) {
    				isReceiveAbort = true;
    				return;
    			}
    			
    			if(msg.contains("end")) {
    				isEnd = true;
    			}else if(msg.contains("dready")) {
    				isDready = true;
    			}else if(msg.contains("dabnormal")) {
    				isDabnormal = true;
    			}
    			
    			if(isEnd || isDready || isDabnormal) {
    				
    				try {
    					DatagramPacket p = new DatagramPacket(udpPacket.getData(), udpPacket.getData().length,
    //							InetAddress.getByName(udpPacket.getRemote().getIp()), udpPacket.getRemote().getPort()
    							InetAddress.getByName(newMachineHost), newMachinePort
    							);
    					datagramSocket.send(p);
    					logger.info("回送数据:{}",msg);
    				} catch (IOException  e) {
    					logger.error("发送数据:{},失败,error:{}。",msg,e);
    				}
    			}
    			
    		}
    		
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
  • 相关阅读:
    select、poll、epoll三种IO多路复用的区别
    自学之路Flutter使用Provider进行状态管理
    SSM(Spring SpringMVC MyBatis)配置文件信息,完成学生管理页面(前后端全部代码)
    认识Ruby
    回调函数的简单使用0717
    PDF文件解析
    java计算机毕业设计消防应急管理系统源代码+数据库+系统+lw文档
    【面试题】最新2023前端面试题
    golang点击下载文件与二维码下载文件
    lightdb21.3-新增优化器提示no_merge
  • 原文地址:https://blog.csdn.net/qq_43528792/article/details/134034620