简体   繁体   中英

Java how to listen for traffic on port without interfering

In Java how can I listen to see if there is any traffic on a port without interfering?

I have a java application that communicates via UDP with some hardware. However, there exists a legacy product that can remotely communicate with the hardware using port forwarding on 23982.

The two systems cannot communicate with the hardware at the same time so I need to listen on port 23982. If there is any traffic on the port (ie the legacy software is actively connecting to the hardware) I need to give the legacy system priority.

Hence I need to listen but not interfere with the traffic being forwarded from port 23982

我认为您想使用ServerSocket

int port = 23982;
java.net.ServerSocket serverSocket = new java.net.ServerSocket(port);
java.net.Socket client = serverSocket.accept(); 
// blocks until there is a connection-request. So you can
// now handle your notification, because if the program reaches this part of code, 
// a client has connected, e.g.:

boolean connected = true;

只需打开您正在干扰流量的端口,行为就会从“无法连接”更改为“超时”,立即变为“服务器无响应”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM