我有一個非常簡單的異步UDP偵聽器,作爲服務設置,它現在一直工作得很好,但它最近在SocketException An existing connection was forcibly closed by the remote host
上崩潰。我有三個問題:C#異步UDP偵聽器SocketException
- 這是什麼原因造成的? (我不認爲UDP套接字有連接)
- 如何複製它,出於測試目的?
- 我該如何幹淨地處理異常,所以一切都會繼續工作?
我的代碼看起來像下面這樣:
private Socket udpSock;
private byte[] buffer;
public void Starter(){
//Setup the socket and message buffer
udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock.Bind(new IPEndPoint(IPAddress.Any, 12345));
buffer = new byte[1024];
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
}
private void DoReceiveFrom(IAsyncResult iar){
try{
//Get the received message.
Socket recvSock = (Socket)iar.AsyncState;
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
int msgLen = recvSock.EndReceiveFrom(iar, ref clientEP);
byte[] localMsg = new byte[msgLen];
Array.Copy(buffer, localMsg, msgLen);
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
//Handle the received message
Console.WriteLine("Recieved {0} bytes from {1}:{2}",
msgLen,
((IPEndPoint)clientEP).Address,
((IPEndPoint)clientEP).Port);
//Do other, more interesting, things with the received message.
} catch (ObjectDisposedException){
//expected termination exception on a closed socket.
// ...I'm open to suggestions on a better way of doing this.
}
}
唯一的例外是在recvSock.EndReceiveFrom()行被拋出。
偉人!嗨同樣的問題接收ICMP消息,並在收到時拋出異常。你的編碼技巧解決了這個問題! – Kevan 2013-03-08 13:02:23
@凱爾,我終於完全測試了這個!這似乎是這個例外的真正根源。作爲參考,我最終使用了答案和Jim,這樣即使有異常,監聽器也會重新啓動。 – chezy525 2013-07-30 21:12:54
Broken Link。此答案應更改爲更新鏈接或包含鏈接指向的信息。 – 2014-03-02 02:23:07