Java – 异步套接字通道无法连接到远程异步服务器套接字通道。

huangapple go评论77阅读模式
英文:

Java - async socket channel cannot connect to remote async server socket channel

问题

我创建并调试了一个简单的网络应用程序,它使用了AsynchronousSocketChannelAsynchronousServerSocketChannel,当通过localhost访问时,它们工作正常,但即使我将服务器应用程序移到另一台设备上,它仍然拒绝连接。我首次注意到这个问题是当我尝试进行端口转发应用程序时,但当我将它移动到同一网络上的另一台设备并通过IPv4连接时,仍然不起作用。我检查了两台设备,它们都允许Java通过防火墙,并且没有阻止我使用的端口号(我还测试了各种其他端口)。即使客户端尝试连接到位于同一台计算机上并使用该计算机的IPv4的服务器,它仍然无法连接。它只在localhost上工作。有其他人曾经遇到过这个问题吗?

这是服务器通道的打开方式:

serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress("127.0.0.1", port));
英文:

I created and debugged a simple net application that uses AsynchronousSocketChannel and AsynchronousServerSocketChannel and they work fine when accessed via localhost, but even if I move the server application to another device it refuses to connect at all. I first noticed this when I tried to port-forward the application, but upon moving it onto a device on the same network and connecting via the IPv4 it still didn't work. I checked both devices and they both are allowing Java through the firewall and there is nothing blocking the port number I am using (I have also tested various other ports). The client can't even connect to the server if it's on the same machine and you use that machine's IPv4. It literally only works on localhost. Has anyone else ever had this issue?

This is how the server channel is opened:

serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress("127.0.0.1", port));

答案1

得分: 0

通过移除InetSocketAddress中的IP部分来修复了它:

serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress(port));

127.0.0.1替换为机器的IPv4具有相同的效果。如果有人能解释一下这个问题,那就太好了,因为我在网上看到的每个例子都绑定到了localhostAsynchronousServerSocketChannel不允许远程访问吗?

英文:

Fixed it by removing the IP part of the InetSocketAddress:

serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress(port));

Replacing 127.0.0.1 with the IPv4 of the machine has the same effect. If someone could explain this that would be great, because every example I've seen online has binded to localhost. Are AsynchronousServerSocketChannels not supposed to be accessed remotely or something?

huangapple
  • 本文由 发表于 2020年7月30日 19:09:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63171872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定