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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sarabandeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sllortElectronic TechnicialAuthor Commented:
Son of a gun.  And I thought this was going to be hard.  Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.