System.Net.IPAddress.MapToIPv4 C# (CSharp) Method

MapToIPv4() public method

public MapToIPv4 ( ) : IPAddress
return IPAddress
        public IPAddress MapToIPv4()
        {
            if (IsIPv4)
            {
                return this;
            }

            // Cast the ushort values to a uint and mask with unsigned literal before bit shifting.
            // Otherwise, we can end up getting a negative value for any IPv4 address that ends with
            // a byte higher than 127 due to sign extension of the most significant 1 bit.
            long address = ((((uint)_numbers[6] & 0x0000FF00u) >> 8) | (((uint)_numbers[6] & 0x000000FFu) << 8)) |
                    (((((uint)_numbers[7] & 0x0000FF00u) >> 8) | (((uint)_numbers[7] & 0x000000FFu) << 8)) << 16);

            return new IPAddress(address);
        }
    }

Usage Example

Esempio n. 1
0
        public void Connect(int aPort, IPAddress Ip, string userName, string password, string nick)
        {
            int attempts = 5;
            _nick = nick;

            // TODO: validar esse objeto, pois após a desconexão ocorre o dispose() dele
            _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            while (!_clientSocket.Connected && attempts > 0)
            {
                try
                {
                    OnRaiseMessage(new MessageEventArgs(String.Format("Attempt {0} to connect at server {1}:{2}", attempts, Ip.MapToIPv4().Address, aPort)));
                    _clientSocket.Connect(Ip ,aPort);
                    OnRaiseMessage(new MessageEventArgs("Connected!"));

                    //solicita autenticação
                    Authenticate(userName, password);

                    //inicia recebimento assíncrono de mensagens
                    _clientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), _clientSocket);

                }
                catch (Exception ex)
                {
                    OnRaiseMessage(new MessageEventArgs("Connection failed."));
                    Thread.Sleep(1000);
                    attempts--;
                }
            }
        }
All Usage Examples Of System.Net.IPAddress::MapToIPv4