开发者

C# UdpClient -- Ttl property vs. SetSocketOption

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.SetSocket开发者_如何学PythonOption(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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜