问题描述
SocketsocketListen=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPAddresshostIP=(Dns.Resolve(IPAddress.Any.ToString())).AddressList[0];//用法已过时IPEndPointep=newIPEndPoint(hostIP,3500);socketListen.Bind(ep);socketListen.Listen(20);求大神帮忙解决一下!!!
解决方案
解决方案二:
可以使用Dns.GetHostAddresses(Dns.GetHostName())不过要注意,不要胡乱选择什么[0]元素。你怎知道它就一定是IPv4地址呢?而且就算是,也可能是个虚拟设备的地址,单纯监听它可能你也得不到任何结果。服务器要使用IPAddress.Any,不要单单监听个别IP。
解决方案三:
只是警告,可以忽略的
解决方案四:
给你我的方法publicstaticstring方法_GetMeIp(){stringrestr="";IPAddress[]ipList=Dns.GetHostAddresses(Dns.GetHostName());foreach(IPAddressipinipList){//获得IPv4if(ip.AddressFamily==AddressFamily.InterNetwork){restr=ip.ToString();if(restr.Length>=9)break;}}returnrestr;}publicstaticstring方法_GetMeIpV4(){return方法_GetMeIp();}publicstaticstring方法_GetMeIpV6(){stringrestr="";IPAddress[]ipList=Dns.GetHostAddresses(Dns.GetHostName());foreach(IPAddressipinipList){//获得IPv6if(ip.AddressFamily==AddressFamily.InterNetworkV6){restr=ip.ToString();break;}}returnrestr;}
解决方案五:
引用3楼yangshengchuan的回复:
给你我的方法publicstaticstring方法_GetMeIp(){stringrestr="";IPAddress[]ipList=Dns.GetHostAddresses(Dns.GetHostName());foreach(IPAddressipinipList){//获得IPv4if(ip.AddressFamily==AddressFamily.InterNetwork){restr=ip.ToString();if(restr.Length>=9)break;}}returnrestr;}publicstaticstring方法_GetMeIpV4(){return方法_GetMeIp();}publicstaticstring方法_GetMeIpV6(){stringrestr="";IPAddress[]ipList=Dns.GetHostAddresses(Dns.GetHostName());foreach(IPAddressipinipList){//获得IPv6if(ip.AddressFamily==AddressFamily.InterNetworkV6){restr=ip.ToString();break;}}returnrestr;}
这个有缺陷,万一有多个IP4的但是,这里一旦找到第一个,就是直接返回了
解决方案六:
TcpListenerlistener=newTcpListener(IPAddress.Any,port);listener.Start();