L
L
leidek2014-03-18 18:36:11
Java
leidek, 2014-03-18 18:36:11

How to do a LAN port scan in JAVA?

Is it possible to scan the port of the local network (router) in any way (TCP SYN is probably the simplest) using native JAVA tools?
The problem is the need for a network connection of several programs, without knowing in advance the IP of the computers. The algorithm is something like this - I send a broadcast message (on the default UDP port, for example, 1234). The rest listen on this port, and, upon receiving a message, connect to the received IP. The problem is that the port may be closed or unavailable.
How can I check, without knowing other IPs, that the port is open? Let's take the address of the router as known. I’m probably heresy, because while I’m floating in the subject, or point-blank I don’t see simpler ways.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2014-03-18
@OLS

1. Open any predefined UDP port for listening. (If the problem is that you are afraid that the port will be already occupied by some other service, then start a pool of ports 5-10 in different parts of the range).
2. Send a broadcast UDP packet to this port(s) with a specific signature. If such a signature is received - start the exchange. If some other service has occupied this port, then it will simply ignore the signature unknown to it.
TCP broadcast requests are not possible - only UDP.
If the task is to establish a TCP connection, then before broadcasting, open any free TCP port for listening, and in the UDP packet after the signature, indicate which TCP port number is open.
If you are afraid that the TCP port on this machine is already occupied by some other listening service, then the listen procedure will simply return an error to you and you will choose the next free one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question