UDPClient 第一次使用 -- 有什么问题吗?

发布于 2024-08-24 20:03:45 字数 2018 浏览 6 评论 0原文

我在学校时想测试 UDPClient 类。 我连接到学校的无线网络,它有严格的防火墙。

与此示例相比,此代码看起来非常可靠。 (http://msdn.microsoft.com/en -us/library/system.net.sockets.udpclient.aspx) 但是当我打开wireshark时,我看不到任何数据包(当我过滤UDP数据包或其他数据包时)。

关于我的代码可能有什么问题有什么想法吗?我认为它被学校的防火墙阻止了,但我不确定。

  public static void CallBack(IAsyncResult result)
        {
            UdpClient myClient = result.AsyncState as UdpClient;
            int sent = myClient.EndSend(result);
            Console.WriteLine("Sent " + sent.ToString() + " bytes");
        }
        static void Main(string[] args)
        {
            UdpClient myClient = new UdpClient(57422);
            try
            {
                myClient.Connect(IPAddress.Parse("127.0.0.1"), 57422);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

                int b = 1;
                b = IPAddress.HostToNetworkOrder(b);
                string myName = "ALEX";

                int lengthOfB = System.BitConverter.GetBytes(b).Length;
                int lengthOfName = NUEncoder.GetByteCount(myName);

                Byte[] intBytes = System.BitConverter.GetBytes(b);
                Byte[] nameBytes = NUEncoder.GetBytes(myName);

                Byte[] bytesToSend = new Byte[lengthOfB + lengthOfName];

                int i = 0;
                for (i = 0; i < lengthOfName; i++)
                {
                    bytesToSend[i] = nameBytes[i];
                }

                for (int k = 0; k < lengthOfB; k++)
                {
                    bytesToSend[i] = intBytes[k];
                    i++;
                }

                myClient.BeginSend(bytesToSend, bytesToSend.Length, CallBack, myClient);

                Console.WriteLine("Sleeping...");
                Thread.Sleep(50);
                Console.WriteLine("Done");
            }
        }

Wanted to test the UDPClient class out while I was at school.
I am connected to the school's wireless which has a strict firewall.

This code seems pretty solid when compared to this example. (http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx)
But when I open up wireshark I don't see any of my packets (when I am filtering for UDP packets or otherwise).

Any ideas on what could be wrong with my code? I am thinking it's being blocked by the school's firewall but I'm not sure.

  public static void CallBack(IAsyncResult result)
        {
            UdpClient myClient = result.AsyncState as UdpClient;
            int sent = myClient.EndSend(result);
            Console.WriteLine("Sent " + sent.ToString() + " bytes");
        }
        static void Main(string[] args)
        {
            UdpClient myClient = new UdpClient(57422);
            try
            {
                myClient.Connect(IPAddress.Parse("127.0.0.1"), 57422);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

                int b = 1;
                b = IPAddress.HostToNetworkOrder(b);
                string myName = "ALEX";

                int lengthOfB = System.BitConverter.GetBytes(b).Length;
                int lengthOfName = NUEncoder.GetByteCount(myName);

                Byte[] intBytes = System.BitConverter.GetBytes(b);
                Byte[] nameBytes = NUEncoder.GetBytes(myName);

                Byte[] bytesToSend = new Byte[lengthOfB + lengthOfName];

                int i = 0;
                for (i = 0; i < lengthOfName; i++)
                {
                    bytesToSend[i] = nameBytes[i];
                }

                for (int k = 0; k < lengthOfB; k++)
                {
                    bytesToSend[i] = intBytes[k];
                    i++;
                }

                myClient.BeginSend(bytesToSend, bytesToSend.Length, CallBack, myClient);

                Console.WriteLine("Sleeping...");
                Thread.Sleep(50);
                Console.WriteLine("Done");
            }
        }

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

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

发布评论

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

评论(1

一直在等你来 2024-08-31 20:03:45

您正在向 PC (127.0.0.1) 发送数据。我认为这就是为什么您在 Wireshark 中看不到任何内容的原因。

You are sending data to your PC (127.0.0.1). I think that this is why you don't see anything with Wireshark.

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