如何读取正确的IP地址?

时间:2016-04-07 05:15:38

标签: c# get ip-address ipv4

我想读取客户机的IP地址,我在下面的代码行使用 -

    <html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        var i=1;
        var id=1,text;
//      text = '<li><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';

        $("#ok").click(function(){
            var number = document.getElementById("num").value;
            //alert(number);
            for(var $j=i; $j<= number; $j++){
                text = '<li><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';
                $("ol").append(text);
                id++;i++;
            }

        });

        $("#btn1").click(function(){
            text = '<li ><input id="input_'+id+'" type="text"/> <button class="btn2">-</button> </li>';
            $("ol").append(text);
            id++;i++;
        });

        $('.divi').on('click', '.btn2', function() {
             $(this).parents("li").slideUp('medium', function() {
             $(this).remove();
             i--;
          });
       });

         $("#btnprint").click(function(){ 
            var number = $(".divi ol li").length;

            var msg="";
            for(var j=1; j<=number; j++){
                msg += document.getElementById("input_" +j).value + "<br/>";
            }
            document.getElementById("printdiv").innerHTML=msg;
        });

    });
</script>
</head>

<body>
    <input type="number" id="num" autofocus="true"></input>
    <button id="ok">OK</button>
    <br/><br/>
    <button id="btn1">+</button>

    <div class="divi">
        <ol>
        </ol>
    </div>

    <button id="btnprint" >Print Data</button>
    <div id="printdiv"></div>

</body>

</html>

对于某些机器,它返回正确的IP地址,如-10.50.207.154,但对于某些机器,它返回的IP地址如-fe80 :: 25ab:4248:c134:23c6%29

如何获得第一个(例如 - Ex - 10.50.207.154)的IP地址?

2 个答案:

答案 0 :(得分:4)

他们都是IP地址。 “正确”的是IPv4。时间越长IPv6

你可以write a simple regex to detect IPv4,但我觉得你错误地认为IPv6地址不是IP地址,当它们非常如此时。

答案 1 :(得分:1)

获取客户端计算机的IP地址,试试这项工作对我来说,

private string GetIPAddress()
        {
            string IpAddress;
            IpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (IpAddress == "" || IpAddress == null)
            {
                IpAddress = Request.ServerVariables["REMOTE_ADDR"];

                if (IpAddress == "::1" || IpAddress == "localhost")
                {
                    IPAddress[] ipArray = Dns.GetHostAddresses(Dns.GetHostName());
                    foreach (IPAddress ip in ipArray)
                    {
                        //InterNetwork for IPV4
                        if (ip.AddressFamily == AddressFamily.InterNetwork)
                        {
                            IpAddress = ip.ToString();
                            break;
                        }
                    }
                }
            }

            return IpAddress;
        }
相关问题