为什么我的套接字程序无法连接到互联网?
本学期我开始了我的第一门网络课程。 我的问题是,每当我尝试将套接字连接到 LAN 之外的主机时, 返回网络不可达错误。无论它们是 BSD 套接字还是 Java 套接字。 此外,我的 nmap 探针也返回相同的错误。 有一次我在这里问了一个关于 ping 探测的类似问题,答案是我的 LAN 代理拒绝 ICMP 请求。但是建立 TCP 连接时没有 ICMP 请求,对吗? 另一方面,为什么我的浏览器可以连接到任何主机...尽管它使用相同的代理... 另外,(请原谅我这么长的疑问)当我在套接字中将主机名指定为 www.google.com 时,会返回“未知主机”。但我的浏览器很高兴地识别出同一个主机。
先感谢您...
import java.net.*;
import java.io.*;
class Whois {
public static void main(String args[]) throws Exception{
int c;
Socket s=new Socket(args[0],Integer.parseInt((args[1])));
InputStream in=s.getInputStream();
while( (c=in.read()) != -1) {
System.out.print((char) c);
}
s.close();
}
}
I have begun my first course on networking this semester.
My problem is that whenever I try to connect my socket to a host outside my LAN,
network unreachable error is returned. Whether they be BSD sockets or Java sockets.
Moreover, my nmap probes also return the same error.
Once I asked a similar question here about ping probes and the answer was that my LAN proxy is rejecting ICMP requests. But there are no ICMP requests in establishing TCP connections right?
Why, on the other hand, my browser can connect to any host... although that it uses the same proxy...
Also,(please pardon me for this long doubt) when I give host name as www.google.com in my sockets, "unknown host" is returned. But my browser happily recognizes the same host.
Thank you in advance...
import java.net.*;
import java.io.*;
class Whois {
public static void main(String args[]) throws Exception{
int c;
Socket s=new Socket(args[0],Integer.parseInt((args[1])));
InputStream in=s.getInputStream();
while( (c=in.read()) != -1) {
System.out.print((char) c);
}
s.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,造成这种行为的原因可能有很多。
我最好的办法是,您位于防火墙后面,该防火墙阻止任何不前往端口 80 的连接。在这种情况下,您可以尝试将程序连接到同一网络,但端口 80。
不知道为什么您不这样做无法获取谷歌的名称解析。我猜这是您代码中的错误,但无法确定。
希望有帮助。
Unfortunately, there may be many reasons for this behavior.
My best shot is that you're behind a firewall that's blocking any connections that are not going to port 80. In this case, you may try to connect with your program to the same network, but port 80.
Not sure why you wouldn't get name resolution for google. I'm guessing it's a bug in your code, but can't tell for sure.
Hope it helps.
调查并了解您的网络设置。您如何连接到互联网?您的工作站可能有一个默认路由指向某些机器,可能再次执行网络地址转换 (NAT) 和/或运行 防火墙,也许还有代理服务器。了解如何进行名称解析 (DNS)。一旦你弄清楚了这些,你可能会发现需要做什么才能与外部连接。
另一方面,我们在问题中没有看到的代码中的错误也可能是:)
Investigate and understand your network setup. How are you connected to the internet? Your workstation probably has a default route pointing to some machine again probably doing Network Address Translation (NAT) and/or running a firewall and maybe that proxy server. Find out how you do name resolution (DNS). One you figure these out you might find what you need to do to connect outside.
On the other hand, errors in the code we don't see in the question are also likely :)
可能有防火墙阻止与外部主机的所有连接,并且内部 DNS 服务器不查找外部主机名。这就是为什么您的套接字程序既不能查找外部主机名也不能连接到外部服务的原因。
同样的限制也适用于您的浏览器;只是您的浏览器设置为使用代理服务器。这意味着浏览器不会直接查找外部主机名或连接到外部主机 - 它只是查找代理名称并与代理服务器建立 TCP 连接。然后,代理将代表您的浏览器进行主机名查找并与外界建立 TCP 连接。
There is likely a firewall that is blocking all connections to outside hosts, and an internal DNS server that does not lookup external hostnames. THis is why your socket programs can neither lookup outside hostnames nor connect to outside services.
The same restriction applies to your browser; it's just that your browser is set up to use a proxy server. This means that the browser isn't directly looking up outside hostnames or connecting to outside hosts - it is only looking up the proxy name and making TCP connections to the proxy server. The proxy then is doing the hostname lookups and making the TCP connections to the outside world, on behalf of your browser.