Ejemplo n.º 1
0
        public bool ForwardPort(MappingProtocol protocol, int port)
        {
            List <Mapping> mappingsToForward = new List <Mapping>(2);

            if (protocol.HasFlag(MappingProtocol.Tcp))
            {
                mappingsToForward.Add(new Mapping(Protocol.Tcp, port, port));
            }

            if (protocol.HasFlag(MappingProtocol.Udp))
            {
                mappingsToForward.Add(new Mapping(Protocol.Udp, port, port));
            }

            try
            {
                foreach (Mapping mapping in mappingsToForward)
                {
                    _natDevice.CreatePortMap(mapping);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public bool CheckPort(MappingProtocol protocol, string ipAddress, int port)
        {
            bool tcpPortOpen = true;
            bool udpPortOpen = true;

            if (protocol.HasFlag(MappingProtocol.Tcp))
            {
                using (TcpClient client = new TcpClient())
                {
                    try
                    {
                        client.Connect(ipAddress, port);
                        tcpPortOpen = true;
                    }
                    catch
                    {
                        tcpPortOpen = false;
                    }
                }
            }

            if (protocol.HasFlag(MappingProtocol.Udp))
            {
                using (UdpClient client = new UdpClient())
                {
                    try
                    {
                        client.Connect(ipAddress, port);
                        udpPortOpen = true;
                    }
                    catch
                    {
                        udpPortOpen = false;
                    }
                }
            }

            return(udpPortOpen && tcpPortOpen);
        }