The Moose and Squirrel Files

August 25, 2008

TCP Keepalives

Filed under: Network — Tags: , — networknerd @ 11:11 pm

Microsoft knowledgebase article 314053 does a good job of describing the configurable parameters of the windows TCP/IP stack.  When it comes to the entry on keepalives though it pays to read the fine print.  The final part of the description says

By default, keepalive packets are not sent. A program can turn on this feature on a connection.

So clearly we need to add the appropriate lines in the code (if we have it) or hope the vendor had enough sense to compile their code with an option to turn it on.  In the .Net framework we can turn it on using a call to the setsocketoption method as shown in the C# code below.

using System;
using System.Net;
using System.Net.Sockets;

namespace keepalive
  class Program
    public static void Main(string[] args)
      Console.WriteLine("Starting client connection in 10 seconds");
      Console.WriteLine("Start wireshark now to capture the connection");

// TODO: Implement Functionality Here
      System.Net.Sockets.TcpClient cli = new TcpClient();
      cli.Connect("", 23);
//Now set the keepalive socket option
      cli.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive,1);

      Console.Write("Press any key to end the connection . . . ");

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: