简体   繁体   English

无法使用主机名连接到Android服务器

[英]Can't connect to Android server using hostname

i'm trying to make a Java application that connects to an Android application. 我正在尝试使Java应用程序连接到Android应用程序。

Both my pc and my phone are connected to the same network. 我的电脑和手机都连接到同一网络。

This is the Java client wich runs on my pc: 这是在我的PC上运行的Java客户端:

client = new Socket("muffin", port);

System.out.println("Connected");

output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());

System.out.println("Streams ready");

And this is the Android application wich works as the server: 这是可以用作服务器的Android应用程序:

server = new ServerSocket(port);
socket = server.accept();

Log.i("Server", "Connected");

output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
input = new ObjectInputStream(socket.getInputStream());

In the manifest i added the permissions: 在清单中,我添加了权限:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

As you can see i'm trying to connect from my pc to the android server by using its hostname (i changed my android hostname to "muffin"), but it doesn't connect and it throws an exception: 如您所见,我正在尝试使用主机名从我的PC连接到android服务器(我将android主机名更改为“ muffin”),但它无法连接,并且抛出异常:

java.net.UnknownHostException: muffin

If instead of the hostname i use the ip address, it works without problems. 如果我使用IP地址而不是主机名,那么它将正常工作。 It looks like it cannot find a device on LAN called "muffin", but you can see from this screenshot of my modem page that the name is right: 看起来它无法在LAN上找到名为“松饼”的设备,但是您可以从我的调制解调器页面的此屏幕截图中看到名称正确:

显示已连接客户端的我的调制解调器页面

That said, i tryed to use the android application as the client and the java program as the server, but it looks like android has some problems because it didn't connect to my pc even by using the ip address instead of the hostname. 就是说,我尝试使用android应用程序作为客户端,使用java程序作为服务器,但是android似乎有一些问题,因为即使使用ip地址而不是主机名,它也没有连接到我的电脑。

Do you have any idea on how to fix this problem? 您对如何解决此问题有任何想法吗?

Thanks in advance and sorry for my english, it's not my mother tongue. 在此先感谢您,我的英语不好,这不是我的母语。

I solved the problem, i've had to allow the traffic on the specific port through the windows firewall. 我解决了这个问题,我不得不允许特定端口上的流量通过Windows防火墙。

Now i can connect to the android application using its hostname. 现在,我可以使用其主机名连接到android应用程序。

You can't do: 您不能:

new Socket("muffin", port);

There is a difference between Hostname, and Host, so: 主机名和主机之间有区别,因此:

java.net.UnknownHostException: muffin

means that the host "muffin" doesn't exist, and that's true: the only existing host is 192.168.1.105, who has a hostname who is "muffin". 表示主机 “ muffin”不存在,这是真的:唯一存在的主机是192.168.1.105,其主机名是“ muffin”。

So you should have done: 因此,您应该这样做:

new Socket("192.168.1.105", port);

It is not possible to get a host from it's hostname : so if it is the only way for you to do it, you will have to do a huge scan of all the local network, and then see which host that is connected has the good hostname . 这是不可能得到一个主机从它的主机名 :所以如果是为你做的唯一途径,你将不得不做所有的本地网络的一个巨大的扫描,然后看哪个连接主机具有良好主机名 As this method is much much harder, i realy recommand you to find another way to do it :) 由于此方法要困难得多,因此我真的建议您找到另一种方法:)

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

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