I’m trying to launch my client, but have an error. Server is already running on the same computer. So I’m using “localhost” with GetHostEntry:
IPHostEntry ipHostInfo = System.Net.Dns.GetHostEntry("localhost");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
Sock = new Socket(remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
Sock.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), Sock);
But I have this “no connection could be made because the target machine actively refused it”:
System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it [::1]:7777
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at NotifierClient.AsynchronousClient.ConnectCallback(IAsyncResult ar) in *** :line 156
Line 156 is
client.EndConnect(ar);
What is the reason? Could it be because ipHostInfo.AddressList[0] is an IPv6? How then I can accept Ipv4 address?
You can use the AddressFamily property of the IPAddress class to tell whether the address is IPv4 or IPv6.
This way you can loop through the list of IPAddress-es returned and choose the first one that is an IPv4 address: