TTL 值 - 生存时间

发布于 2024-11-16 18:46:41 字数 2415 浏览 3 评论 0原文

http://searchnetworking.techtarget.com/definition/time-to-live

从这篇文章中,我对这个主题的定义感到困惑。 有没有机会有人帮助我更好地理解正在发生的事情?

斯塔夫。

----------------------------------


编辑:

我尝试从同一台计算机向自己发送消息。 当我将 TTL 值设置为 0 时,我发送的包仍然可以到达。这是为什么? 这是代码:

顺便说一句,发送方法中的这一行。

server.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, 0);


    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    class MAIN
    {
        public static MulticastOption MulticastOption;
        private static MulticastOption CreateGroup()
        {
            MulticastOption = new MulticastOption(IPAddress.Parse("224.100.0.1"));
            return MulticastOption;
        }
        private static void Receive()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            EndPoint ep = (EndPoint)iep;
            sock.Bind(iep);
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, MulticastOption);
            //
            byte[] data = new byte[1024];
            new Thread(new ThreadStart(Send)).Start();
            int recv = sock.ReceiveFrom(data, ref ep);
            String stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
            sock.Close();
        }


   private static void Send()
    {
        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9051);
        IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
        server.Bind(iep);
       server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1")));
     server.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive, 0);
        server.SendTo(Encoding.ASCII.GetBytes("This is a test message"), iep2);
        server.Close();
    }
    public static void Main(String[] args)
    {
        CreateGroup();
        Receive();
        Console.ReadKey();
    }
}

http://searchnetworking.techtarget.com/definition/time-to-live

From this article, im Confused about the defanition about this subject.
is there any chance someone help me undestand better whats going on?

stav.

----------------------------------


EDIT:

i tried to send massage to my self from the same computer.
when i set the TTL value to 0, the pack i send still get to me. why is that?
here is the code:

btw,this line in the Send Method.

server.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastTimeToLive, 0);


    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Threading;
    class MAIN
    {
        public static MulticastOption MulticastOption;
        private static MulticastOption CreateGroup()
        {
            MulticastOption = new MulticastOption(IPAddress.Parse("224.100.0.1"));
            return MulticastOption;
        }
        private static void Receive()
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            EndPoint ep = (EndPoint)iep;
            sock.Bind(iep);
            sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, MulticastOption);
            //
            byte[] data = new byte[1024];
            new Thread(new ThreadStart(Send)).Start();
            int recv = sock.ReceiveFrom(data, ref ep);
            String stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
            sock.Close();
        }


   private static void Send()
    {
        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9051);
        IPEndPoint iep2 = new IPEndPoint(IPAddress.Parse("224.100.0.1"), 9050);
        server.Bind(iep);
       server.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.100.0.1")));
     server.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.MulticastTimeToLive, 0);
        server.SendTo(Encoding.ASCII.GetBytes("This is a test message"), iep2);
        server.Close();
    }
    public static void Main(String[] args)
    {
        CreateGroup();
        Receive();
        Console.ReadKey();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南巷近海 2024-11-23 18:46:41

客户端计算机在生成数据包时设置 TTL 值,以指示允许数据包通过的最大跳数。

每次数据包通过路由器时,路由器都会递减 TTL 值。

如果 TTL 值达到零,路由器将丢弃数据包并返回 ICMP“跳数超出”错误消息。

TTL 字段的主要好处是在出现路由环路(即网络故障导致数据包在同一组路由器之间来回跳动)的情况下永远停止数据包跳动。

Client machines set the TTL value when a packet is generated, to indicate the maximum number of hops the packet is permitted to go through.

Each time a packet passes through a router the router will decrement the TTL value.

If the TTL value reaches zero, the router will drop the packet and return an ICMP "hop count exceeded" error message.

The main benefit of the TTL field is to stop packets bouncing around forever in the event of a routing loop, i.e. a network fault that causes packets to bounce around and around between the same set of routers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文