代码如下
//数据接收
@Transformer(inputChannel = "udpChannel", outputChannel = "udpFilter")
public String transformer(@Payload byte[] payload, @Headers Map<String, Object> headers) {
String message = new String(payload);
if (dtuInfoDtoList==null){
dtuInfoDtoList=new ArrayList<>();
}
dtuInfoDtoList.add(headers);
InetSocketAddress inetSocketAddress = (InetSocketAddress) headers.get("ip_packetAddress");
String id = headers.get("id").toString();
// 获取来源IP,可以进行IP过滤
String ip = headers.get("ip_address").toString();
// 获取来源Port
String port = headers.get("ip_port").toString();
logger.info(ip);
logger.info(inetSocketAddress.getHostName());
logger.info(String.valueOf(inetSocketAddress.getAddress()));
logger.info(String.valueOf(inetSocketAddress.getPort()));
udpSimpleService.sendMessage("we got that",inetSocketAddress);
// 转换为大写
// message = message.toUpperCase();
// 向客户端响应,还不知道怎么写
return message;
}
//数据发送
public void sendMessage(String message,InetSocketAddress inetSocketAddress ) {
logger.info("发送UDP: {}", message);
//发送端口
byte[] udpMessage = message.getBytes();
DatagramPacket datagramPacket = null;
try (DatagramSocket datagramSocket = new DatagramSocket()) {
datagramPacket = new DatagramPacket(udpMessage, udpMessage.length, inetSocketAddress);
datagramSocket.send(datagramPacket);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
logger.info("发送成功");
}
阿里云服务端打印的UDP客户端的端口和发送端口不一致,有了解的大佬请指教