Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for port in use when there are multiple interfaces

I know that For TCP, we can only have one application listening on a single port at one time. Now if you had 2 network cards, you could have one application listen on the first IP and the second one on the second IP using the same port number.

Now in this case If I have to check whether a particular port is in use, how do I do that?

My intention is a port should be used by only one application even though there are multiple interfaces. Is it Ok if I list all the interfaces and bind with the port number with all those interfaces in a loop, or is there any better way to do this checking.

like image 817
Vijay Avatar asked Dec 15 '14 07:12

Vijay


People also ask

Can a port have multiple interfaces?

You can add multiple Ethernet interfaces to the same port group. After you run a configuration command once in the port group view, the configuration takes effect on all the Ethernet interfaces in the port group, reducing the configuration workload.

Are ports per network interface?

Port numbers and interfaces exist on different layers, so there's no restriction on what ports can be used on a particular interface.

Which command is used to check whether the port is listening in nmap?

To scan for UDP connections, type: sudo nmap -sU scanme.nmap.org.


1 Answers

I know that For TCP, we can only have one application listening on a single port at one time.

Except as below.

Now if you had 2 network cards, you could have one application listen on the first IP and the second one on the second IP using the same port number.

Correct.

Now in this case If I have to check whether a particular port is in use, how do I do that?

See below.

My intention is a port should be used my only one application even though there are multiple interfaces. Is it Ok if I list all the interfaces and bind with the port number with all those interfaces in a loop, or is there any better way to do this checking.

Just bind it to 0.0.0.0:port. In Java that means a null InetAddress and a non-zero port number. If there are any applications listening at specific interfaces and the same port number, you will get a BindException. Conversely, if you are first, it will preclude any other applications from binding to that port in any way at all.

like image 139
user207421 Avatar answered Sep 30 '22 05:09

user207421