我想得到本机的IP 写了一段代码:如下,
        string hostname = Dns.GetHostName();
        Response.Write("Hostname: "+hostname);        IPAddress[] _ips = Dns.GetHostAddresses(Dns.GetHostName());
        int i = 0;
        foreach (IPAddress ip in _ips)
        {
            Response.Write((i++).ToString()+":  |");
            Response.Write("    |"+ ip+"|   ");
        }最后输出:Hostname: JZ-xinsiyu0: | |fe80::2000:51:8b18:eb19%13| 1: | |192.168.1.25| 2: | |2001:0:cf2e:3096:2000:51:8b18:eb19| 不知道|fe80::2000:51:8b18:eb19%13 和2001:0:cf2e:3096:2000:51:8b18:eb1分别代表什么啊?

解决方案 »

  1.   

    Hostname://说明,没用...JZ-xinsiyu0//BIOS名,局域网内可用...fe80::2000:51:8b18:eb19//MAC地址,网段内可用...192.168.1.25//IPV4地址,用这个...2001:0:cf2e:3096:2000:51:8b18:eb19//IPV6地址,将来用...
      

  2.   

    更正一下...fe80::2000:51:8b18:eb19%13//这个是IPV6地址...2001:0:cf2e:3096:2000:51:8b18:eb19//这个是本地链接IPV6地址...这两个将来用...
      

  3.   

    判断IPAddress.AddressFamily属性...值是AddressFamily.InterNetwork表示IPV4地址...
      

  4.   


    //验证数据合法性
                if (!ValidateInfo())
                {
                    return;
                }
                int port = int.Parse(myport);
                //向服务器发出连接请求
                TCPConnection conn = new TCPConnection(_ipAddr, port);
                TcpClient _tcpc = conn.Connect();
                if (_tcpc == null)
                {
                    MessageBox.Show("无法连接到服务器,请重试!",
                                    "错误",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                }
                else
                { ........
                 }我想知道 我的 ipAddr 应该添哪个地址了,是{192.168.1.25}吗?谢谢楼上几们的关注。
      

  5.   

    up!
    貌似做socket的时候不能用本机给本机发啊?原来那样识别不出来。。
      

  6.   

    你这个TCPConnection是谁写的?没有重载构造函数吗?TcpListener、TcpClient、Socket绑定本地Socket都不需要本地地址...纯属多此一举...
      

  7.   

    这回我又错了—— 我是看的别人写的代码,没有仔细看..
    好像以下的就可以吧:
     class TCPConnection
        {
            private IPAddress _ip = null;
            private int _port;
            private TcpClient _tcpc = null;//为 TCP 网络服务提供客户端连接        public TCPConnection(IPAddress ip, int port)
            {
                _ip = ip;
                _port = port;
            }        public TcpClient Connect()
            {
                try
                {
                    _tcpc = new TcpClient();
                    _tcpc.Connect(_ip, _port);//连接到服务
                }
                catch (Exception)
                {
                    return null;
                }
                return _tcpc;
            }
        }
    private IPAddress _ipAddr;
    IPAddress.TryParse(myipstring, out _ipAddr);
    int port = int.Parse(svrport_tb.Text);
                //向服务器发出连接请求
                TCPConnection conn = new TCPConnection(_ipAddr, port);
                TcpClient _tcpc = conn.Connect();
                if (_tcpc == null)
                {
                    MessageBox.Show("无法连接到服务器,请重试!",
                                    "错误",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                }
                else
                {
                  ...................
                       }
      

  8.   


    可以放给本地的只不过你获取ip的时候应该这样
    IPAddress[] ipad = Dns.GetHostAddresses("localhost");
    使用的时候直接  ipad[0]就可以了   譬如TcpListener tl = new TcpListener(ipad[0],2000);