我制作了两个节目。一个是将UDP数据包发送到UDP服务器的客户端程序,另一个是从UDP客户端接收UDP数据包的服务器程序。
UDP服务器是由Spring启动的。 问题是在STS(Spring Tools Suite)中运行和运行jar(maven安装)之间存在性能差异。 当我在STS(Run as-> Spring启动应用程序)中运行此程序时,它从客户端获取所有数据包。如果客户端发送了3,000个数据包,则会获得3,000个数据但是当我以jar(maven install)运行时,它会获得大约200-300的一些数据包。即使它是完全相同的代码。
我不知道为什么会这样。有没有办法构建spring boot app或需要一些选项?
整个代码太大了。所以我上传了连接,发送,绑定代码。
这是客户端程序
public UdpClient(String[] args) {
connector = new NioDatagramConnector();
connector.setHandler(this);
ConnectFuture connFuture = connector.connect(new InetSocketAddress(
args[0], 6001));
connFuture.awaitUninterruptibly();
if(args[1] != null) {
totalCount = Integer.parseInt(args[1]);
}
if(args[2] != null) {
count = Integer.parseInt(args[2]);
}
connFuture.addListener(new IoFutureListener<ConnectFuture>() {
public void operationComplete(ConnectFuture future) {
if (future.isConnected()) {
session = future.getSession();
try {
sendData(totalCount, count);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
LOGGER.error("Not connected...exiting");
}
}
});
}
private void sendData(int totalCount, int count) throws InterruptedException {
int tempCount = 0;
for (int i = 0; i < totalCount; i++) {
try{
File udpData = new File("udp_data.txt");
BufferedReader br = new BufferedReader(new FileReader(udpData));
String line;
int k = 0;
ArrayList<IoBuffer> bufList = new ArrayList<IoBuffer>();
while((line = br.readLine()) != null) {
String[] str = line.split(" ");
IoBuffer buf = IoBuffer.allocate(str.length);
for (int j = 0; j < str.length; j++) {
int hexData = Integer.parseInt(str[j],16);
byte hexByte = (byte)hexData;
buf.put(hexByte);
}
bufList.add(buf);
buf.flip();
}
long startTime = System.currentTimeMillis();
for (int j = 0; j < count; j++) {
session.write(bufList.get(j));
tempCount++;
if(j % 100 == 0) {
Thread.sleep(1);
}
}
long endTime = System.currentTimeMillis();
System.out.println("oper Time : " + (endTime - startTime));
} catch(Exception e) {
e.printStackTrace();
}
}
System.out.println("Total Send Count : " + tempCount);
}
这是服务器程序。
@Service
public class ModBusUdpMina {
private static Logger logger = LoggerFactory.getLogger(ModBusUdpMina.class);
@Autowired
private IoHandlerAdapter ioUdpHandlerAdapter;
@Autowired
private ProtocolCodecFactory ambProtocolCodecFactory;
@PostConstruct
public void bind() {
NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
acceptor.getFilterChain().addLast(\"codec\", new ProtocolCodecFilter(ambProtocolCodecFactory));
acceptor.setHandler(ioUdpHandlerAdapter);
acceptor.getSessionConfig().setReuseAddress(true);
acceptor.getSessionConfig().setReceiveBufferSize(1024*512);
acceptor.getSessionConfig().setReadBufferSize(2048);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
try {
acceptor.bind(new InetSocketAddress(6001));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}