[英]Java Socket Client Unable to Connect to C# Socket Server
I have a use case, where I have to send data from a .NET application, to a Java application and I'm trying to do this using sockets. 我有一个用例,我必须将数据从.NET应用程序发送到Java应用程序,而我正在尝试使用套接字来实现。 I have a server created using C# -
我有一个使用C#创建的服务器-
TcpListener tcpListener = null;
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
try
{
// Set the listener on the local IP address
// and specify the port.
tcpListener = new TcpListener(ipAddress, 13);
tcpListener.Start();
Console.WriteLine("The server is running at port 13...");
Console.WriteLine("The local End point is -" +
tcpListener.LocalEndpoint);
output = "Waiting for a connection...";
Console.WriteLine(output);
Socket s = tcpListener.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
}
catch (Exception e)
{
output = "Error: " + e.ToString();
Console.WriteLine(output);
}
}
On the Java side, I have created a socket which listens to the same host and port - 在Java方面,我创建了一个侦听同一主机和端口的套接字-
Socket socket;
try {
socket = new Socket( "localhost", 13);
System.out.println("Connection established");
BufferedReader input =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
String answer = input.readLine();
System.out.println(answer);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
I'm getting the below error - java.net.ConnectException: Connection refused: connect . 我收到以下错误-java.net.ConnectException:连接被拒绝:connect 。 I have used telnet localhost 13, to check if my server is really running, and it is.
我已经使用telnet localhost 13来检查我的服务器是否确实正在运行。 So i don't think it could be an issue with server not running or firewalls, since both are running locally.
因此,我认为这可能与服务器未运行或防火墙无关,因为两者均在本地运行。 Any ideas on how to resolve this?
有关如何解决此问题的任何想法?
I tried your code and I had exactly the same problem. 我尝试了您的代码,但遇到了完全相同的问题。 Your C# TCP Server only binds to the IPv6 interface (check eg the resource monitor listening addresses).
您的C#TCP服务器仅绑定到IPv6接口(请检查例如资源监视器侦听地址)。 If you change your server to
new TcpListener(IPAddress.Any, 13)
it should work. 如果将服务器更改为
new TcpListener(IPAddress.Any, 13)
它将正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.