我有一个 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
提交
取消