Ejemplo n.º 1
0
 public void Start(int port = DEFAULT_PORT)
 {
     if (Interlocked.Exchange(ref started, 1) == 0)
     {
         IPAddress address = LocalAddresses.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork && !IsIntranetAddress(p));
         if (address == null && UpnpEnabled && UPnP.Discover())
         {
             try
             {
                 address = UPnP.GetExternalIP();
                 UPnP.ForwardPort(port, ProtocolType.Tcp, "AntShares");
                 LocalAddresses.Add(address);
             }
             catch { }
         }
         listener = new TcpListener(IPAddress.Any, port);
         try
         {
             listener.Start();
             Port = (ushort)port;
         }
         catch (SocketException) { }
         connectThread.Start();
         if (Port > 0)
         {
             listenerThread.Start();
         }
     }
 }
Ejemplo n.º 2
0
 public void Start(int port = DEFAULT_PORT)
 {
     if (Interlocked.Exchange(ref started, 1) == 0)
     {
         IPHostEntry localhost = null;
         try
         {
             localhost = Dns.GetHostEntry(Dns.GetHostName());
         }
         catch (SocketException) { }
         IPAddress address = localhost?.AddressList.FirstOrDefault(p => p.AddressFamily == AddressFamily.InterNetwork && !IPAddress.IsLoopback(p) && !IsIntranetAddress(p));
         if (address == null && UpnpEnabled && UPnP.Discover())
         {
             try
             {
                 address = UPnP.GetExternalIP();
                 UPnP.ForwardPort(port, ProtocolType.Tcp, "AntShares");
             }
             catch
             {
                 address = null;
             }
         }
         if (address != null && !IsIntranetAddress(address))
         {
             listener = new TcpListener(IPAddress.Any, port);
             try
             {
                 listener.Start();
                 LocalEndpoint = new IPEndPoint(address.MapToIPv6(), port);
             }
             catch (SocketException) { }
         }
         connectThread.Start();
         if (LocalEndpoint == null)
         {
             return;
         }
         listenerThread.Start();
     }
 }