Create a cusomt socket class that inherits from the .net socket class:
public delegate void SocketEventHandler(Socket socket); public class CustomSocket : Socket { private readonly Timer timer; private const int INTERVAL = 1000; public CustomSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) : base(addressFamily, socketType, protocolType) { timer = new Timer { Interval = INTERVAL }; timer.Tick += TimerTick; } public CustomSocket(SocketInformation socketInformation) : base(socketInformation) { timer = new Timer { Interval = INTERVAL }; timer.Tick += TimerTick; } private readonly List<SocketEventHandler> onCloseHandlers = new List<SocketEventHandler>(); public event SocketEventHandler SocketClosed { add { onCloseHandlers.Add(value); } remove { onCloseHandlers.Remove(value); } } public bool EventsEnabled { set { if(value) timer.Start(); else timer.Stop(); } } private void TimerTick(object sender, EventArgs e) { if (!Connected) { foreach (var socketEventHandler in onCloseHandlers) socketEventHandler.Invoke(this); EventsEnabled = false; } }
Then use it as follows:
var socket = new CustomSocket(
I just implemented the Socket close event on each socket. therefore, your application must register event handlers for this event. then socket will tell your application if it was closed;)
If there is a problem with the code, let me know.
source share