使用以下代码,是否有更好的方法来设置UDP监听而不是while(true)
Thread.Sleep(10)
?
public void Start()
{
socket.Bind(ip);
while (true)
{
data = new byte[1024];
receivedDataLength = socket.ReceiveFrom(data, ref Remote);
raw = Encoding.ASCII.GetString(data, 0, receivedDataLength);
row = new LogRow(raw);
//Console.WriteLine(row.ClientIp);
row_queue.Enqueue(row);
Thread.Sleep(10);
}
}
答案 0 :(得分:3)
ReceiveFrom
正在阻止。线程将暂停,直到数据到达。除非Socket
处于非阻止模式。