通过主机名连接

发布于 2024-11-08 07:21:13 字数 612 浏览 0 评论 0原文

我有一个在本地计算机(Windows 7)上运行的服务器,用于侦听传入的 tcp 套接字连接。在同一台机器上,我通过 IntelliJ 运行 Android 模拟器。

连接建立执行时:

Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress("10.0.2.2", 8082);
socket.connect(address);

但是通过主机名尝试时:

Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress("comp2", 8082);
socket.connect(address);

我得到:

java.net.UnknownHostException: Host is unresolved: comp2:8082

当我使用 Windows 命令提示符来 ping(通过主机名)我的计算机和同一网络上的其他计算机时,我得到回复。
关于如何让它发挥作用有什么想法吗?

I have a server running on my local machine (Windows 7) that listens to incoming tcp sockets connection. On the same machine I'm running Android Emulator through IntelliJ.

The connection gets established When executing:

Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress("10.0.2.2", 8082);
socket.connect(address);

But when trying by hostname:

Socket socket = new Socket();
InetSocketAddress address = new InetSocketAddress("comp2", 8082);
socket.connect(address);

I get:

java.net.UnknownHostException: Host is unresolved: comp2:8082

When I use windows command prompt to ping (by hostname) my computer and other computers over the same network I get replies.
Any idea on how to get it working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

审判长 2024-11-15 07:21:13

我解决了。
显然,与 Windows 中运行的某些服务相比,模拟器不会将名称 comp2 转换为完整的主机名,即 comp2.letre.ltd。
更改

InetSocketAddress address = new InetSocketAddress("comp2", 8082);

InetSocketAddress address = new InetSocketAddress("comp2.letre.ltd", 8082);

修复它

I got it resolved.
Appearently, the emulator, in contrast to some service that runs in windows, does not translate the name comp2 to the full host name which is comp2.letre.ltd.
Changing

InetSocketAddress address = new InetSocketAddress("comp2", 8082);

to

InetSocketAddress address = new InetSocketAddress("comp2.letre.ltd", 8082);

fixed it

临走之时 2024-11-15 07:21:13

请检查解析主机名和域名的 DNS 条目。对应的IP地址。

Please check DNS entries which resolves hostname & corresponding IP address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文