printing buffer from ReceiveFrom terminates thread
I have a thread in C# that broadcasts from a UDP socket every 1 second
on a different thread, I have this
whi开发者_如何学运维le (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 ?
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.
精彩评论