Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scan for a port waiting for a connection on a network

Tags:

c#

networking

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?

like image 928
Érik Desjardins Avatar asked Mar 08 '12 03:03

Érik Desjardins


1 Answers

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.

like image 161
Hakan Serce Avatar answered Sep 19 '22 07:09

Hakan Serce