C#socket uses is incompatible with the requested protocol address????????

  //Start listening
        
        public bool CreatTcpServer(string ip, int listenPort)
        {
            _port = listenPort;
            _listener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
             //= IPAddress.Any;
            foreach (IPAddress address in Dns.GetHostEntry(ip).AddressList)          
           {
               //Console.WriteLine(ip);
                try
                {
                    Console.WriteLine("1aaaaaaaaaaaaa");
                    IPAddress hostIP = address;
                    IPEndPoint ipe = new IPEndPoint(address, _port);/////////////////////////////////////////////////////////////////////////
                    Console.WriteLine("1dddddddlllll");
                   _listener.Bind(ipe);//This sentence is not executed´╝ü
                   Console.WriteLine("11qqqqqqqqqqqqq1");
                    _listener.Listen(_maxConnections);
                    Console.WriteLine("11111wwwwwwwwwwwwww");
                    _listener.BeginAccept(new System.AsyncCallback(ListenTcpClient),_listener  );
                    Console.WriteLine("111cccccccc");
                    break;
                }
                catch (System.Exception e )
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine("11111111111111");
                     return false;
                }
            }
            return true;
        }

Started by Robin at November 12, 2016 - 6:49 PM

if (m_hostName == "localhost")
{
m_hostName = Dns.GetHostName();
}
IPAddress[] addrIP = Dns.GetHostAddresses(m_hostName);
Boolean ipv4 = false;
foreach (IPAddress ip in addrIP)
{
//Select the IPV4 address
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localAddress= ip;
ipv4=true;
break;
}
}
if (!ipv4)
{
localAddress = addrIP[0];
}

Posted by Sarah at November 18, 2016 - 7:42 PM

In the WindowsVista/7/8 and 8.1, pay attention to screen out IPV4 address can only.
The above code is used to screen out the IPV4 address, IPV6 address.

Posted by Sarah at November 19, 2016 - 8:03 PM

You can bind to address: 0.0.0.0 said all network interfaces

Posted by Candice at November 22, 2016 - 8:31 PM

I why want to enumerate the address of the machine, the direct use of IPAddress.Any interception is good, represent arbitrary address.

Posted by Luther at December 04, 2016 - 8:33 PM