Is there any socket functions to connect automatically a client to a server, when the server switches off and on again?


It would be a great help if i get an idea for my problem. I am using a server and client application. I am using Berkley Sockets . My problem is now when my server switches off or disconnects from the client , my client throws out an exception.

"System.Net.Sockets.SocketException (0x80004005): An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlag socketFlags, AsyncCallback callback, Object state)
at CCLink_Structure.Form1.Send_Message_Timer_Tick(Object sender, EventArgs e) in C:\Users\yilia\source\repos\CCLink Structure\CCLink Structure\Form1.cs:line 494 at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

My client wants to remain stable even when the server switches off and my client should connect back to the server automatically when the server switches on.
I am developing a user interface program in c# windows form application. In my program i use form. timer to send the data continuously to the server , the interval time between data packets is 50 ms . My only problem now is that i want my client to standstill without throwing any exceptions and should reconnect when the server switches on.

    toolStripStatusLabel1.Text = "Connecting...";
    Socket newsock = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);
    String IP = System.Convert.ToString("");
    int Port = System.Convert.ToInt32("54601");
    IPEndPoint iep = new IPEndPoint(IPAddress.Parse(IP),
    newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
 catch (Exception ex) { MessageBox.Show(ex.Message); }```
asked on Stack Overflow May 7, 2020 by Helan • edited May 7, 2020 by jason.kaisersmith

1 Answer


What you are talking about is resilience and retry functionality. This you need to code yourself into your program.

You could try to use 3PP libraries such as Polly to help you with this.

But I agree with the comments, that you should probably look at why the Server is going down. If this is planned maintenance, then you should look at a High-Availability solution where you have your server deployed in a resilient 1+1 or N+1 manner.

answered on Stack Overflow May 7, 2020 by jason.kaisersmith

User contributions licensed under CC BY-SA 3.0