How to scan a port that is waiting on a network connection

I am currently working on a small chat utility in C #.
The problem is that I cannot figure out how to scan the network for a specific port on all machines. I am currently using this method

IPGlobalProperties network = IPGlobalProperties.GetIPGlobalProperties(); IEnumerable<IPEndPoint> connections = network.GetActiveTcpListeners() .Where(x => x.Port == ConstParams.iPort); 

where ConstParams.iPort is the port I want to check (6910 here).

The problem is that only the local ports and IP address "0.0.0.0" are returned values ​​...

How can I scan all open ports (6910) on the current network?

+7
source share
4 answers

Instead of using port scanning, I suggest you implement a simple multicast / broadcast based discovery mechanism.

During startup, the application should broadcast / multicast its IP port information. All running instances should reply to this message with their IP port information. This discovery mechanism is easy to implement, and it is faster at runtime and more dynamic than the port scanning approach.

+9
source

You should consider multicasting, but instead of skating yourself, rely on an existing standard with library support, for example mDNS: http://en.wikipedia.org/wiki/Multicast_DNS

Or, as you said C # using one of your own solutions: http://msdn.microsoft.com/en-us/library/system.net.peertopeer.aspx

+1
source

Scanning ports are a poor choice, most likely you will run firewalls on computers on the network to display your machine as an attacker. Any network intrusion detection system can also be launched. This is a lot of overhead for what you need.

I would recommend doing the broadcast using UDP or multicast to detect other clients http://www.codeproject.com/Articles/1705/IP-Multicasting-in-C

Another option would be to create a centralized server on a web server (php script, asp.net page, etc.) or a web service (REST) ​​with which the chat client will connect at startup, POSTing it listens for IP / Port, and then in turn will receive a list of all recently announced IP / ports of other clients on the network. You probably want someone to stay alive here, IE: the client will send POST to the page every 5 minutes, if the IP address is not POST for 10 minutes, it will be removed from the list.

To get the public IP address of the device, you can check this page: http://www.whatismyip.com/faq/automation.asp

You just need to send a web request to get the IP address. If you want to get the IP address not 0.0.0.0/127.0.0.1 of the local interface, you can check these messages:

Get local IP address

How to get the software IP address of a local area network of a computer? (FROM#)

+1
source

GetIPGlobalProperties returns only information about your local machine (see http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipglobalproperties.getipglobalproperties.aspx ).

To find out what other computers on the network open this port, you will need to go through a series of IP addresses, trying to connect to this port. There is no central repository for this.

This article describes the approach: http://www.dijksterhuis.org/building-a-simple-portscanner-in-c/

0
source

All Articles