Note that there are some explanatory texts on larger screens.

plurals
  1. POC# UdpClient -- Ttl property vs. SetSocketOption
    text
    copied!<p>I've used socket programming extensively in C++, so I understand what all of the socket options are, etc. Now I'm dabbling in C#, and I've come across a problem that I would like an explanation for.</p> <p>I'm using the UdpClient class in a small app that another developer started. I'm sending packets to a Multicast address, so I need to set the TTL for the packets. I look at the documentation here:</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx</a></p> <p>And there is a TTL property that can be set or get. The help on the property says the "default" ttl is 128. If I get the property, it is 128, and after I set the property and get it again, I can verify that the value has changed, however, when I send a packet, its actual TTL is set to 1.</p> <p>Because I know more about networking that the guy that started the app, I tried this line of code (<code>udpRecvClient</code> is the name of the UdpClient):</p> <pre><code>updRecvClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 64); </code></pre> <p>This causes my sent packets to have a correct TTL of 64, but reading the Ttl property still returns an unchanged default value of 128.</p> <p>So what gives? Am I mis-reading the Ttl property? I'd like to know what the problem is so I can try to avoid it when using other C# classes. For now, I'm just going to use SetSocketOption for everything and ignore the 'helpful' Properties.</p> <p>Note that <code>updRecvClient.Client.Ttl</code> is also a property, and it also does not change the actuall TTL on outgoing packets.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload