我正在尝试启动我的客户端,但出现错误。服务器已经在同一台计算机上运行。所以我在 GetHostEntry 中使用“localhost”:
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);
但我有这个“无法建立连接,因为目标机器主动拒绝它”:
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
第 156 行是
client.EndConnect(ar);
是什么原因?可能是因为 ipHostInfo.AddressList[0] 是 IPv6 吗?那我怎么能接受 IPv4 地址呢?