7

I am currently working on a little chat utility in C#.
The problem is I can't 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 scan (6910 here).

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

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

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
Érik Desjardins
  • 993
  • 1
  • 12
  • 25

4 Answers4

10

Rather than using port scanning, I suggest you to implement a simple discovery mechanism based on multicast/broadcast communication.

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

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
1

You should consider multicast, but rather than rolling your own, rely on an existing standard with library support, like mDNS: http://en.wikipedia.org/wiki/Multicast_DNS

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

Clay Fowler
  • 2,059
  • 13
  • 15
1

Scanning ports is a poor choice, you will most likely trigger firewalls on machines in the network to display your machine as an attacker. Any Intrusion detection systems on the networks could potentially be triggered as well. It's a lot of overhead for what you need.

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

Another option would be to have a centralized server, either on a web server (php script, asp.net page, etc) or a web service (REST) which the chat client would connect to on start up, POSTing it's listening IP/Port, and then in turn would receive a list of all recently announced IP/Ports of the other clients on the network. You'd probably want some keep alive here, IE: the client would POST to the page every 5 minutes, if an IP does not POST for 10 minutes, it would be removed from the list.

To get the public IP of the machine, you could check out this page: http://www.whatismyip.com/faq/automation.asp

You'd just need to send a web request to it to retrieve the IP. If you want to get the non 0.0.0.0/127.0.0.1 IP of the local interface, you can check out these posts:

Get local IP address

How do I get the Local Network IP address of a computer programmatically? (C#)

Community
  • 1
  • 1
Developer
  • 2,021
  • 12
  • 11
0

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

To find out which other machines on the network have that port open, you'd have to iterate through the a range of IPs, attempting to connect on that port. There is no central repository to query on this.

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

Nik
  • 2,718
  • 23
  • 34