我有一個 C# 客戶端,他將數據發送到 Java 異步服務器并將數據寫入 GUI,在localhost 中一切正常,但是,當我更改為其他 ip 時,客戶端說我:System.Net.Sockets.SocketException (0x80004005): 無法建立連接,因為目標設備明確拒絕此類連接 192.168.1.172:11000 zh System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) en System.Net .Sockets.Socket.Connect(EndPoint remoteEP)客戶端 C#:public void StartClient(string precio) { // Data buffer for incoming data. byte[] bytes = new byte[1024]; // Connect to a remote device. try { IPAddress ipAddress = IPAddress.Parse("192.168.1.172"); IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000); // Create a TCP/IP socket. Socket sender = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Connect the socket to the remote endpoint. Catch any errors. try { sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString()); // Encode the data string into a byte array. byte[] msg = Encoding.ASCII.GetBytes(precio + "<EOF>"); Console.WriteLine(msg); // Send the data through the socket. int bytesSent = sender.Send(msg); // Release the socket. sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch (ArgumentNullException ane) { Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); } catch (SocketException se) { Console.WriteLine("SocketException : {0}", se.ToString()); }
1 回答

長風秋雁
TA貢獻1757條經驗 獲得超7個贊
那么你只綁定到localhost. 您應該使用要綁定到的實際 IP 地址。
您還可以嘗試綁定到所有 IPv4 接口:
InetSocketAddress sAddr = new InetSocketAddress("0.0.0.0", port);
server.bind(sAddr);
添加回答
舉報
0/150
提交
取消