I am using a system.Timers Timer in Asynchronous Socket listener console app. I want the timer to run every 40 seconds and check connection status of client devices.
Once the timer starts Server socket is not listening any more(not waiting for more connections) and only data can be received all the time by 1st device that was connected. When I removed the device from network and again connected there is nothing happening. No other function is running. Here is the code for timer-
IPStatusTimer = new System.Timers.Timer(35000);
IPStatusTimer.Elapsed += IPStatusTimer_Elapsed;
IPStatusTimer.AutoReset = true;
IPStatusTimer.Enabled = true;
private static void IPStatusTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Inside Timer");
if (connectedClient > 0)
{
foreach (KeyValuePair<string, int> i in IPStatus)
{
if (i.Value == 1)
{
IPStatus[i.Key] = 0;
}
else if (i.Value == 0)
{
Decode dr = new Decode();
string m = dr.offlineMessage();
if (Clients.Count > 0)
{
foreach (KeyValuePair<int, StateObject> c in Clients)
{
if (((IPEndPoint)c.Value.workSocket.RemoteEndPoint).Address.ToString() == i.Key)
{
Clients.Remove(c.Key);
}
}
SendMessage(i.Key, m);
}
}
}
}
}
The timer should not affect my other functions. It should quietly run in background till the app is working.
EDIT: (In above code)Added if CLients.Count>0 then only will remove item from CLients So that there wont be any exception there.
Whenever there is a connection I am adding the Stateobject to dictionary so that I can broadcast data to all the clients when require.
public static void AcceptCallback(IAsyncResult ar)
{
connectedClient++;
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
string ip = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
Console.WriteLine("ip " + ip);
// Create the state object.
StateObject state = new StateObject();
state.buffer = new byte[StateObject.BufferSize];
Clients.Add(connectedClient, state);
if (IPStatus.ContainsKey(ip))
IPStatus[ip] = 1;
else
IPStatus.Add(ip, 1);
Console.WriteLine(" Total Connected client : " + connectedClient);
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
So as you can see.. If a client is back in network and server is always in listening mode it should accept connection and state object should be added to Client(dictionary object) again. It should not be a problem. And as soon as client gets connected I am giving IPStatus(dictionary object) a value 1 as it means client is online.
Then in my readCallBack(), whenever I receive data from any client, then in my dictionary , I am again setting its value to 1 in IPStatus.
public static void ReadCallback(IAsyncResult ar)
{
string content = string.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
int i = 0;
string ip = ((IPEndPoint)handler.RemoteEndPoint).Address.ToString();
if (IPStatus.ContainsKey(ip))
{
IPStatus[ip] = 1;
}
}
And when Timer runs , If Value of any IP is 1 I am setting it to 0. So that next time if timer runs and it gets value 0 that means it had not received any data from client in last 35 seconds which means client is not online anymore. ReadCallback and all is happening using StateObject not with Clients(dictinary Object). So if I am removing any item from Clients, I am adding to it again when Any new connection starts.
I hope my scenario is getting a bit more clear to you guys.