Esempio n. 1
0
		private List<Socket> CreateSockets()
		{
			var clients = new List<Socket>();
			try
			{
                var ips = _ipprovider.UnicastAddresses();

                foreach (var ipAddress in ips)
				{
					try
					{
					    var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                        socket.Bind(new IPEndPoint(ipAddress, 0));
                        clients.Add(socket);
					}
					catch (Exception)
					{
					    continue; // Move on to the next address.
					}
				}
			}
			catch (Exception)
			{
				//clients.Add(new UdpClient(0));
			}
			return clients;
		}
Esempio n. 2
0
        private List <UdpClient> CreateSockets()
        {
            var clients = new List <UdpClient>();

            try
            {
                var ips = _ipprovider.UnicastAddresses();

                foreach (var ipAddress in ips)
                {
                    try
                    {
                        clients.Add(new UdpClient(new IPEndPoint(ipAddress, 0)));
                    }
                    catch (Exception)
                    {
                        continue; // Move on to the next address.
                    }
                }
            }
            catch (Exception)
            {
                clients.Add(new UdpClient(0));
            }
            return(clients);
        }
Esempio n. 3
0
        private void CreateSocketsAndAddGateways()
        {
            Sockets       = new List <UdpClient>();
            _gatewayLists = new Dictionary <UdpClient, IEnumerable <IPEndPoint> >();

            try
            {
                List <IPEndPoint> gatewayList = _ipprovider.GatewayAddresses()
                                                .Select(ip => new IPEndPoint(ip, PmpConstants.ServerPort))
                                                .ToList();

                if (!gatewayList.Any())
                {
                    gatewayList.AddRange(
                        _ipprovider.DnsAddresses()
                        .Select(ip => new IPEndPoint(ip, PmpConstants.ServerPort)));
                }

                if (!gatewayList.Any())
                {
                    return;
                }

                foreach (IPAddress address in _ipprovider.UnicastAddresses())
                {
                    UdpClient client;

                    try
                    {
                        client = new UdpClient(new IPEndPoint(address, 0));
                    }
                    catch (SocketException)
                    {
                        continue; // Move on to the next address.
                    }

                    _gatewayLists.Add(client, gatewayList);
                    Sockets.Add(client);
                }
            }
            catch (Exception e)
            {
                NatDiscoverer.TraceSource.LogError("There was a problem finding gateways: " + e);
                // NAT-PMP does not use multicast, so there isn't really a good fallback.
            }
        }