尝试发出uPNP发现请求。希望这个问题很简单。为什么没有SendEvent_Completed
被解雇?
public void Send() {
var request = $"M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 1\r\nST: ssdp:all\r\n";
var requestData = Encoding.UTF8.GetBytes(request);
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SendBufferSize = requestData.Length;
var sendEvent = new SocketAsyncEventArgs();
sendEvent.RemoteEndPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
sendEvent.SetBuffer(requestData, 0, requestData.Length);
sendEvent.Completed += SendEvent_Completed; ;
socket.SendToAsync(sendEvent);
}
private void SendEvent_Completed(object sender, SocketAsyncEventArgs e) {
Console.WriteLine("SendEvent_Completed"); // never called why?!
}
答案 0 :(得分:0)
不予提出的明显原因是如果SendToAsync
返回false
:
false
(如果I / O操作同步完成)。在这种情况下,将不会引发Completed
参数上的e
事件,并且在方法调用返回以检索该方法的结果后,可以立即检查作为参数传递的e
对象。操作。
由于您目前根本没有检查该值,因此无法知道是否会在当前代码中引发事件 。