<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Larry Cleeton's Blog</title><subtitle type="html" /><id>http://blogs.msdn.com/lcleeton/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lcleeton/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/lcleeton/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2006-09-15T00:36:00Z</updated><entry><title>Setting SOL_KEEPALIVE_VALS from managed socket class</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/lcleeton/archive/2006/09/15/754932.aspx" /><id>http://blogs.msdn.com/lcleeton/archive/2006/09/15/754932.aspx</id><published>2006-09-15T02:36:00Z</published><updated>2006-09-15T02:36:00Z</updated><content type="html">&lt;P&gt;A question came me today about if it was possible to do in managed code what the native winsock WSAIoctl(SOL_KEEPALIVE_VALS) function does.&amp;nbsp; The answer is yes.&lt;/P&gt;
&lt;P&gt;First, if you simply want to turn on keep-alives you can use this simple method in the Socket class:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;Socket s;&lt;BR&gt;// first you need to Connect, then...&lt;BR&gt;s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Of course to turn it back off just change the last param to false.&lt;/P&gt;
&lt;P&gt;However if you want to specify the timing parameters for keep-alive you can use the Socket.IOControl method.&amp;nbsp; Here's a function that essentially duplicates what you can do with the native WSAIoctl function:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;static void SetKeepAlive(Socket s, bool on, uint time, uint interval) {&lt;BR&gt;/* the native structure&lt;BR&gt;struct tcp_keepalive {&lt;BR&gt;ULONG onoff;&lt;BR&gt;ULONG keepalivetime;&lt;BR&gt;ULONG keepaliveinterval;&lt;BR&gt;};&lt;BR&gt;*/ &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;// marshal the equivalent of the native structure into a byte array&lt;BR&gt;uint dummy = 0;&lt;BR&gt;byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];&lt;BR&gt;BitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0);&lt;BR&gt;BitConverter.GetBytes((uint)time).CopyTo(inOptionValues, Marshal.SizeOf(dummy));&lt;BR&gt;BitConverter.GetBytes((uint)interval).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2); &lt;BR&gt;// of course there are other ways to marshal up this byte array, this is just one way&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;// call WSAIoctl via IOControl&lt;BR&gt;int ignore = s.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=754932" width="1" height="1"&gt;</content><author><name>lcleeton</name><uri>http://blogs.msdn.com/members/lcleeton.aspx</uri></author></entry></feed>