private void OnAccept(IAsyncResult asyncResult) { Socket listener = (Socket)asyncResult.AsyncState; Socket accepted = null; try { accepted = listener.EndAccept(asyncResult); } catch (SocketException ex) { throw new NotImplementedException("GESTISCI L'ECCEZIONE", ex); } catch (ObjectDisposedException) { return; } if (accepted != null) { if (handleSocket(accepted)) { Enqueue(accepted); } else { Release(accepted); } } try { listener.BeginAccept(SocketPool.AcquireSocket(), 0, m_OnAccept, listener); } catch (SocketException ex) { throw new NotImplementedException("GESTISCI L'ECCEZIONE", ex); } catch (ObjectDisposedException) { } }
private Socket Bind(IPEndPoint ipep) { Socket s = SocketPool.AcquireSocket(); try { s.LingerState.Enabled = false; s.ExclusiveAddressUse = false; s.Bind(ipep); s.Listen(8); if (ipep.Address.Equals(IPAddress.Any)) { try { Console.WriteLine("Listening: {0}:{1}", IPAddress.Loopback, ipep.Port); IPHostEntry iphe = Dns.GetHostEntry(Dns.GetHostName()); IPAddress[] ip = iphe.AddressList; for (int i = 0; i < ip.Length; ++i) { Console.WriteLine("Listening: {0}:{1}", ip[i], ipep.Port); } } catch { } } else { Console.WriteLine("Listening: {0}:{1}", ipep.Address, ipep.Port); } IAsyncResult res = s.BeginAccept(SocketPool.AcquireSocket(), 0, m_OnAccept, s); return(s); } catch (Exception e) { if (e is SocketException) { SocketException se = (SocketException)e; if (se.ErrorCode == 10048) { // WSAEADDRINUSE Console.WriteLine("Listener Failed: {0}:{1} (In Use)", ipep.Address, ipep.Port); } else if (se.ErrorCode == 10049) { // WSAEADDRNOTAVAIL Console.WriteLine("Listener Failed: {0}:{1} (Unavailable)", ipep.Address, ipep.Port); } else { Console.WriteLine("Listener Exception:"); Console.WriteLine(e); } } return(null); } }