internal static void WorkingThread() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface NetworkInterface ni = nis[0]; ni.EnableAutomaticDns(); ni.EnableDhcp(); // check if we have an IP CheckIP(); if (_requiresDateTime) { IpAddressAvailable.WaitOne(); SetDateTime(); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
public static void SetupAndConnectNetwork() { wifi = WiFiAdapter.FindAllAdapters()[0]; WiFiConnectionResult wiFiConnectionResult = wifi.Connect(c_SSID, WiFiReconnectionKind.Automatic, c_AP_PASSWORD); NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { NetworkInterface ni = nis[0]; ni.EnableAutomaticDns(); ni.EnableDhcp(); CheckIP(); if (!NetworkHelpers.IpAddressAvailable.WaitOne(5000, false)) { throw new NotSupportedException("ERROR: IP address is not assigned to the network interface."); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
internal static void WorkingThread() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface NetworkInterface ni = nis[0]; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi Debug.WriteLine("Network connection is: Wi-Fi"); //Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; //// note on checking the 802.11 configuration //// on secure devices (like the TI CC3220SF) the password can't be read //// so we can't use the code block bellow to automatically set the profile //if ( (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) && // (wc.Ssid != "" && wc.Password != "")) //{ // // have to update Wi-Fi configuration // wc.Ssid = c_SSID; // wc.Password = c_AP_PASSWORD; // wc.SaveConfiguration(); //} //else //{ // // Wi-Fi configuration matches // // (or can't be validated) //} } else { // network interface is Ethernet Debug.WriteLine("Network connection is: Ethernet"); } ni.EnableAutomaticDns(); ni.EnableDhcp(); // check if we have an IP CheckIP(); if (_requiresDateTime) { IpAddressAvailable.WaitOne(); SetDateTime(); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
internal static void WorkingThread() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface NetworkInterface ni = nis[0]; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi Console.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) { // have to update Wi-Fi configuration wc.Ssid = c_SSID; wc.Password = c_AP_PASSWORD; wc.SaveConfiguration(); } else { // Wi-Fi configuration matches } } else { // network interface is Ethernet Console.WriteLine("Network connection is: Ethernet"); ni.EnableAutomaticDns(); ni.EnableDhcp(); } // check if we have an IP CheckIP(); if (_requiresDateTime) { IpAddressAvailable.WaitOne(); SetDateTime(); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
/// <summary> /// This thread monitors the Ethernet interface for client connection. /// If a client is connected, the thread waits for client data which is then /// passed back through the OnSerialDataReceived event. /// </summary> private void ConnectionListenerThread() { byte [] buff = new byte[1024]; int received; // // Grab a collection of available network interfaces. // NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); // // We need at least one interface to continue. // if (nis.Length > 0) { NetworkInterface ni = nis[0]; ni.EnableAutomaticDns(); ni.EnableDhcp(); // // Wait to be assigned an IP address. // while (ni.IPv4Address == null || ni.IPv4Address.Length == 0 || ni.IPv4Address.Equals("0.0.0.0")) { Thread.Sleep(100); } Debug.WriteLine($"DHCP has given us the address {ni.IPv4Address},{ni.IPv4SubnetMask},{ni.IPv4GatewayAddress}"); while (true) { _clientSocket = null; // // We now have an IP address allocated so we can await // incoming client connections. // IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, ListeningOnPort); Socket listener = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { listener.Bind(localEndPoint); listener.Listen(100); Debug.WriteLine($"Waiting for an incoming connection on port {ListeningOnPort}.."); _clientSocket = listener.Accept(); Debug.WriteLine("Connected to a client"); while (true) { received = _clientSocket.Receive(buff); if (received > 0) { OnSerialDataReceived?.Invoke(this, new BytesReceivedEventArgs(buff, received)); } } } catch (Exception ex) { Debug.WriteLine($"Got a network exception: {ex.Message}"); } } } }
public static void Main() { // change here the network interface index to be used during the test // usual values // 0: for STM32 boards (only one network adapter) // 2: for ESP32 const int networkInterfaceIndex = 0; // change here the network configurations to be used during the test const string staticIPv4Address = "192.168.1.222"; const string staticIPv4SubnetMask = "255.255.255.0"; const string staticIPv4DNSAdress = "192.168.1.2"; try { NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged; NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; DisplayNetworkInterfacesDetails(); // display wireless 802.11 network configurations Wireless80211Configuration[] configs = Wireless80211Configuration.GetAllWireless80211Configurations(); Console.WriteLine(""); Console.WriteLine("====================="); Console.WriteLine("Number of Wireless 802.11 configurations: " + configs.Length.ToString()); int count = 0; foreach (Wireless80211Configuration conf in configs) { Console.WriteLine("Number:" + count.ToString()); count++; Console.WriteLine("ID: " + conf.Id); Console.WriteLine("Authentication: " + conf.Authentication); Console.WriteLine("Encryption: " + conf.Encryption); Console.WriteLine("Radio: " + conf.Radio); Console.WriteLine("SSID: " + conf.Ssid); Console.WriteLine("Password: "******""); } int loopCount = 0; // Loop displaying the current network information for Interfaces available while (true) { loopCount++; // Test code for DHCP and Static adapter configs // Switch every 20 loops between the two if (loopCount % 20 == 0) { Console.WriteLine(""); Console.WriteLine("Switching to DHCP"); Console.WriteLine(""); // switch to DHCP, get test interface NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[networkInterfaceIndex]; ni.EnableAutomaticDns(); ni.EnableDhcp(); } else if (loopCount % 10 == 0) { Console.WriteLine(""); Console.WriteLine("Switching to static IPv4"); Console.WriteLine(""); // switch to static address, get test interface NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[networkInterfaceIndex]; ni.EnableStaticIPv4(staticIPv4Address, staticIPv4SubnetMask, staticIPv4DNSAdress); } Thread.Sleep(1000); } } catch (Exception ex) { // Do whatever please you with the exception caught Console.WriteLine("Exception:" + ex.Message); } }