How to reconnect socket
-
Hi Im working on socket connection. client will send data to the server at some regular intervals. how the client can identify if the ethernet cable is unplugged (client has to show some message "cable disconnected". if cable is plugged again. It should detect it automatically has to continue the task again. Please give ur answers with some sample code/link Thanks and Regards, Srini
-
Hi Im working on socket connection. client will send data to the server at some regular intervals. how the client can identify if the ethernet cable is unplugged (client has to show some message "cable disconnected". if cable is plugged again. It should detect it automatically has to continue the task again. Please give ur answers with some sample code/link Thanks and Regards, Srini
The recommended way to do this with V2.0 of the framework is to use the System.Net.NetworkInformation namespace.. This sample should get you started:
public class NetInfoTest { public static void Main() { try { DumpIPAddresses(); NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged; NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged; Console.ReadLine(); } catch (Exception e) { Console.WriteLine(e); } finally { Console.ForegroundColor = ConsoleColor.Green; Console.BackgroundColor = ConsoleColor.Black; } } public static void OnNetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e) { if (e.IsAvailable) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Network Available"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Network *NOT* Available"); } } public static void OnNetworkAddressChanged(object sender, EventArgs e) { Console.ForegroundColor = ConsoleColor.Red; Console.BackgroundColor = ConsoleColor.Yellow; Console.WriteLine("Address change event at {0}", DateTime.Now.ToString()); Console.ForegroundColor = ConsoleColor.Green; Console.BackgroundColor = ConsoleColor.Black; DumpIPAddresses(); } public static void DumpIPAddresses() { //1. Get All Network Interfaces NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); //2. Loop over the interfaces foreach (NetworkInterface NI in NetworkInterfaces) { //3. Select Ethernet Interface if (NI.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { Console.WriteLine(Environment.NewLine); Console.WriteLine(En