4

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.

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:

http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx

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.

Because I know more about networking that the guy that started the app, I tried this line of code (udpRecvClient is the name of the UdpClient):

updRecvClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 64);

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.

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.

Note that updRecvClient.Client.Ttl is also a property, and it also does not change the actuall TTL on outgoing packets.

JPhi1618
  • 783
  • 1
  • 11
  • 21
  • You probably know more about networking than me, but looking at the assembly in reflector I found that setting UdpClient.Ttl (which is the same as setting UdpClient.Client.Ttl) seems to end up calling SetSocketOption(SocketOptionLevel.IP, SocketOptionName.ReuseAddress, (int) value); Now I don't know what this means, but to me it doesn't seem to have anything to do with TTL? Or does it? – DeCaf Sep 10 '11 at 18:46
  • Silly me, it seems SocketOptionName.ReuseAddress is the same as SocketOptionName.IpTimeToLive (both have value 4), so I guess it is related to TTL. – DeCaf Sep 10 '11 at 18:56
  • I guess I don't understand what you're saying. ReuseAddress is a different setting that TTL, so are you saying that the MS code is incorrectly calling ReuseAddress when you set the TTL property? Sorry for the delay in the answer. – JPhi1618 Sep 23 '11 at 14:42
  • No, I interpreted the code incorrectly because both ReuseAddress and IpTimeToLive have the underlying value 4 which means there is no way to distinguish between them. The SetSocketOption method differentiates between them by looking at the specified SocketOptionLevel. If SocketOptionLevel.IP is used, then it is interpreted as IpTimeToLive, and if SocketOptionLevel.Socket is used then it is interpreted as ReuseAddress. Sorry for the confusion there, my bad. The microsoft code is doing the correct thing as far as I can tell. – DeCaf Sep 24 '11 at 12:07

1 Answers1

2

Setting the UdpClient.Ttl property is equivalent to setting UdpClient.Client.Ttl. It ends up calling:

 UdpClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, value)

(or IPv6 depending on the address family).

So I'm guessing you have to directly call SetSocketOption if you want to set SocketOptionName.MulticastTimeToLive since this is different from SocketOptionName.IpTimeToLive.

DeCaf
  • 6,026
  • 1
  • 29
  • 51
  • So does the TTL property call ReuseAddress, or does it call this IpTimeToLive? I wasn't aware of there being two time to live values, so I'm not sure what the difference is between IpTimeToLive and MulticastTimeToLive unless there is a difference in IPv6. – JPhi1618 Sep 23 '11 at 14:44
  • The enumeration value for ReuseAddress is the same as IpTimeToLive, which means that the method being called cannot distinguish between them interestingly enough. The difference is that the IpTimeToLive option is the one that affects SocketOptionLevel.IP, if you want to set ReuseAddress you must specify SocketOptionLevel.Socket. – DeCaf Sep 24 '11 at 12:05
  • See http://social.msdn.microsoft.com/Forums/ar/netfxnetcom/thread/a0215b38-a15e-400b-91c1-fda5d448dc61 for some info about the differences between the MulticastTimeToLive and the IpTimeToLive options. – DeCaf Sep 24 '11 at 12:10