简体   繁体   English

不同计算机上的Java套接字不起作用

[英]Java Socket on Different Machine Does Not Work

I've tried many examples on web and one of them is this: http://zerioh.tripod.com/ressources/sockets.html 我在网上尝试了许多示例,其中之一是:http: //zerioh.tripod.com/ressources/sockets.html

All of the server-client socket examples work fine when they are tested with 127.0.0.1 所有服务器-客户端套接字示例在使用127.0.0.1进行测试时都可以正常工作

BUT it never ever EVAR works on two different computers with actual raw real IP address ("could not connect to host" on telnet and "connection timed out" when tested on java client - the server program just waits for connection) 但是它永远不会在具有实际原始真实IP地址的两台不同的计算机上运行EVAR(在Java客户端上进行测试时,telnet上的“无法连接至主机”和“连接超时”-服务器程序仅等待连接)

Note: 注意:

  • Firewall is turned off for sure 确保防火墙已关闭
  • IP address from ipconfig didn't work ipconfig中的IP地址不起作用
  • IP address from myipaddress.com (which is totally different for no reason than that from ipconfig) didn't work 来自myipaddress.com的IP地址(与ipconfig没有任何原因完全不同)不起作用

What is it that I'm missing? 我想念的是什么? If I can only figure this out... 如果我只能解决这个问题...

Try binding on 0.0.0.0. 尝试在0.0.0.0上绑定。 This tells your socket to accept connections on every IP your local can accept upon. 这告诉您的套接字接受本地可以接受的每个IP上的连接。

Based on the comment where the the following snippet of code is mentioned: 基于注释,其中提到了以下代码片段:

requestSocket = new Socket("10.0.0.5", 2004); // ip from ipconfig 

it would be better to use the hostname instead of the IP address in the constructor, as the two-parameter Socket constructor with a String argument expects the hostname as the String, and not an IP address. 最好在构造函数中使用主机名而不是IP地址,因为带有String参数两参数Socket构造函数期望将主机名作为String而不是IP地址。 A lookup of the IP address is then performed on the provided hostname. 然后在提供的主机名上查找IP地址。

If you need to pass in an IP address, use the two-parameter constructor that accepts the InetAddress as an argument. 如果需要传递IP地址,请使用接受InetAddress作为参数两参数构造函数。 You can then provide a raw IP address to the InetAddress.getByAddress method, as shown in the following snippet: 然后,您可以向InetAddress.getByAddress方法提供原始IP地址,如以下代码片段所示:

InetAddress addr = InetAddress.getByAddress(new byte[]{10,0,0,5});

You'll need to be careful when specifying arguments via the byte array, as bytes are signed in Java (-127 through +128), and numbers beyond this range (but valid octets of IP addresses) may have to be specified using Integer.byteValue . 通过字节数组指定参数时,您需要注意,因为字节是用Java签名的(-127至+128),并且超出此范围的数字(但IP地址的有效八位字节)可能必须使用Integer.byteValue来指定Integer.byteValue

Finally, it should be noted that it is important to specify the IP address of the remote machine, as visible to the client. 最后,应该注意,指定客户端可见的远程计算机的IP地址非常重要。 The IP address listed at myipaddress.com may be the address of a proxy, as that is the public IP of your entire network as visible to the host server at myipaddress.com. 在myipaddress.com上列出的IP地址可能是代理的地址,因为这是整个网络的公共IP,对于myipaddress.com上的主机服务器是可见的。 Therefore, you ought to be specify the IP address of the remote machine that is visible to your machine and not myipaddress.com. 因此,您应该指定对您的计算机可见的远程计算机的IP地址,而不是myipaddress.com。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM