我正在寫一個ServerLocator基本上廣播端口找到一個服務器,將與IPEndPoint響應,我需要的搜索能夠超時,如果沒有發現當前IPHost然後繼續下一個。處理Socket.ReceiveFrom與超時沒有垃圾郵件控制檯
現在我做這樣的事情(我已刪除了該代碼的一些部分,它不僅包括需要什麼,以顯示我的問題。也有一些客戶端綁定回事)
string serverIp = string.Empty;
while(string.isNullOrEmpty(serverIp))
{
foreach (IPAddress adress in ipHosts.AddressList)
{
using(Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
{
try
{
client.ReceiveFrom(buffer, ref tempRemoteEP);
//Get server IP
serverIp = tempRemoteEP.ToString().Split(":".ToCharArray(), 2)[0];
break;
}
catch(SocketException e)
{
// We expect connection attempt to time out if we cant find any server on this port and nic. Just continue with the next
if (e.SocketErrorCode == SocketError.TimedOut)
{
continue;
}
}
}
}
}
這工作正常,除了控制檯獲取與垃圾郵件:
型「System.Net.Sockets.SocketException」的第一次機會異常出現在System.dll中
有沒有一種很好的方式來處理這樣的異常,而不濫發控制檯?或者我能否以其他方式處理這個問題,以避免超時需要異常?
謝謝。
當您在RELESE配置中構建項目時不會發生這種情況。 – Cipi 2011-02-01 12:05:38
這是真的,但如果它是垃圾郵件太多,它很難與 – Bakery 2011-02-01 12:37:20