简体   繁体   English

Java如何在不干扰的情况下侦听端口上的流量

[英]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? 在Java中,如何侦听端口上是否有任何流量而不会产生干扰?

I have a java application that communicates via UDP with some hardware. 我有一个Java应用程序,可以通过UDP与某些硬件进行通信。 However, there exists a legacy product that can remotely communicate with the hardware using port forwarding on 23982. 但是,存在可以使用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. 这两个系统无法同时与硬件通信,因此我需要侦听端口23982。如果端口上有任何流量(即,旧版软件正在主动连接到硬件),则需要给旧版系统优先。

Hence I need to listen but not interfere with the traffic being forwarded from port 23982 因此,我需要侦听但不干扰从端口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;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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