socket server keep alive timer

Experts;

I have the following loop in a socket server.  The client application sends a keep alive message every 5 seconds while the server is processing a log request.  The while loop needs to receive some input from the client every 7 seconds (either a Keep alive or an ack), or it should time out.  The problem is that the awaited task may take a lot more than 7 seconds to complete.  How can I receive the keep alive messages every 5 seconds within this loop, or is there another approach that might work better?

while (true)
                {
                    string request = await reader.ReadLineAsync();
                    if (request != null)
                    {
                        tcs = new TaskCompletionSource<bool>();
                        if (request.IndexOf("TER") != -1)  //terminate command from client application
                        {
                            isNormalTermination = false;
                            break;
                        }
                        else if (request.IndexOf("KAL") != -1) // inserted to illustrate the use of the keep alive only
                        {
                            RestartTimer();
                        }
                        else
                        {
                            messageRouter.HandleRequest(request);
                            RestartTimer();
                        }
                        RestartTimer();
                        await tcs.Task;
                        await writer.WriteLineAsync(update);
                        RestartTimer();
                        if (update.IndexOf("EOL") != -1)  //End of log
                        {
                            isNormalTermination = true;
                            break;
                        }
                    }
                    else
                    {
                       isNormalTermination = true;
                       break;
                    }
                   
                }
                reader.Dispose();
                writer.Dispose();
                networkStream.Dispose();
                messageRouter.NewMessageAvailable -= MessageRouter_NewOutgoingMessageAvailable;
                TerminateTask(writer, networkStream, tcpClient, clientEndPoint, messageRouter, isNormalTermination);
sllortElectronic TechnicialAsked:
Who is Participating?
 
sarabandeConnect With a Mentor Commented:
actually you don't need keep alive messages between client and server if using c# async programming. if other side is no longer available (connection is broken or process has terminated) any call with await such as await ReadAsync call would throw an ObjectDisposedException.

Sara
0
 
sllortElectronic TechnicialAuthor Commented:
Son of a gun.  And I thought this was going to be hard.  Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.