tarka
tarka

Reputation: 5587

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 (i.e. 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

Upvotes: 1

Views: 952

Answers (3)

Pavel Niedoba
Pavel Niedoba

Reputation: 1557

Just by opening port you are interfering with the traffic, the behavior would change from "unable to connect" which comes immediately to "server not responding" after some timeout.

Upvotes: 0

rmbl
rmbl

Reputation: 50

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;

Upvotes: 1

Alexander
Alexander

Reputation: 490

I think you want to use ServerSocket.

Upvotes: 1

Related Questions