我有一个 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());
}
长风秋雁
相关分类