NetworkComms.Net’s functionality is totally customisable for a general selection of network adaptors. Multiple adaptors can be included when listening for incoming connections by specifying IPAddress.Any for the desiredLocalEndPoint parameter, i.e.
//Will listen for new TCP connections on all available adaptors //using a randomly selected port. Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0)); //Will listen for new UDP connection on all available adaptors //using a randomly selected port. Connection.StartListening(ConnectionType.UDP, new IPEndPoint(IPAddress.Any, 0));
Here the port parameter provided is zero which implies a random port should be selected. If a non-zero port number is provided that port will be used across all adaptors. The IP addresses corresponding with these adaptors can be enumerated from HostInfo.IP.FilteredLocalAddresses().
To listen on a reduced number of adaptors there are several options. The first one is to specify allowed local IP ranges, e.g. if the IP address corresponding with an adaptor matches one of the provided CIDR ranges it will be used:
//Specify specific allowed local listen IP ranges HostInfo.IP.RestrictLocalAddressRanges = new IPRange[] { new IPRange("192.0.0.0/8"), new IPRange("127.0.0.0/8") }; //Listen only on allowed IP addresses Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0));
The second option is to restrict the allowed adaptor names, e.g. to only listen on a wireless adaptor with name “wlan0”:
//Specify specific allowed adaptor names HostInfo.RestrictLocalAdaptorNames = new string[] { "wlan0" }; //Listen only on allowed IP addresses Connection.StartListening(ConnectionType.TCP, new IPEndPoint(IPAddress.Any, 0));
You can enumerate all known adaptor names using HostInfo.AllLocalAdaptorNames();. The third option is to create a list of desired local endpoints, and then start listening separately for each one:
//Create a list of desired endPoints List<IPEndPoint> endPointsToUse = new List<IPEndPoint>() { new IPEndPoint(IPAddress.Parse("192.168.0.1"), 4567), new IPEndPoint(IPAddress.Parse("192.168.10.5"), 7654) }; //Start listening for each specific endPoint foreach (IPEndPoint endPoint in endPointsToUse) Connection.StartListening(ConnectionType.TCP, endPoint);