英文:
Java - async socket channel cannot connect to remote async server socket channel
问题
我创建并调试了一个简单的网络应用程序,它使用了AsynchronousSocketChannel
和AsynchronousServerSocketChannel
,当通过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具有相同的效果。如果有人能解释一下这个问题,那就太好了,因为我在网上看到的每个例子都绑定到了localhost
。AsynchronousServerSocketChannel
不允许远程访问吗?
英文:
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 AsynchronousServerSocketChannel
s not supposed to be accessed remotely or something?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论