来自 ReceiveFrom 的打印缓冲区终止线程
从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于旁注。我相信我的同事上周也遇到了同样的问题。如果您的客户端与服务器在同一台计算机上,您将不会在 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.