为什么不触发异步套接字 send.completed 事件?

Why is the async socket send.completed event not fired?

正在尝试发出 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?!
}

它不会被提出的明显原因是 SendToAsync returns false:

false if the I/O operation completed synchronously. In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

由于您目前根本没有检查该值,因此无法知道事件 是否会 在您当前的代码中引发。