来自 ReceiveFrom 的打印缓冲区终止线程

发布于 2024-10-12 23:54:04 字数 771 浏览 2 评论 0原文

从 UDP 套接字广播一次

我在 C# 中有一个线程,每 1 秒在另一个线程上

 while (true)
 {
  if (m_UdpReceiveSocket.Poll(0, SelectMode.SelectRead))
  {
   EndPoint ep = new IPEndPoint(IPAddress.Any, s_BroadcastPort);
   byte[] buffer = new byte[1024];

   m_UdpReceiveSocket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref ep);

   Console.WriteLine("testing");
   Console.WriteLine( ASCIIEncoding.ASCII.GetString(buffer) + "  " + ((IPEndPoint) ep).Address + ":" + ((IPEndPoint) ep).Port);
  }

  Thread.Sleep(1);
 }

,如果我注释掉对 Console.WriteLine 的第二次调用,一切正常,另一个线程广播并且该线程接收信息,但如果我使用第二个 Console.WriteLine (即使没有 priting EndPoint),那么第二次调用 Console.WriteLine 时线程将毫无例外地退出,

谢谢

问题解决了,我只需要考虑在转换为之前我收到了多少字符串

作为一个附带问题为什么我在 Wireshark 中看不到我的数据包?

I have a thread in C# that broadcasts from a UDP socket every 1 second

on a different thread, I have this

 while (true)
 {
  if (m_UdpReceiveSocket.Poll(0, SelectMode.SelectRead))
  {
   EndPoint ep = new IPEndPoint(IPAddress.Any, s_BroadcastPort);
   byte[] buffer = new byte[1024];

   m_UdpReceiveSocket.ReceiveFrom(buffer, 0, 1024, SocketFlags.None, ref ep);

   Console.WriteLine("testing");
   Console.WriteLine( ASCIIEncoding.ASCII.GetString(buffer) + "  " + ((IPEndPoint) ep).Address + ":" + ((IPEndPoint) ep).Port);
  }

  Thread.Sleep(1);
 }

If I comment out the second call to Console.WriteLine, everything works fine, the other thread broadcasts and this thread receives the information, but if I use the second Console.WriteLine (even without priting the EndPoint) then the thread quits without any exception the second time Console.WriteLine is called

thank you

problem solved, I just needed to take into consideration how much I receive before converting to string

as a side question why can't I see my packets in Wireshark ?

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

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

发布评论

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

评论(1

相守太难 2024-10-19 23:54:04

至于旁注。我相信我的同事上周也遇到了同样的问题。如果您的客户端与服务器在同一台计算机上,您将不会在 Wireshark 中看到数据包。

As for the sidenote. I believe my colleague had the same issue the other week. You will not see packets in Wireshark if your client is the same machine as the server.

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