Android 安卓tcp连接名称

Android 安卓tcp连接名称,android,networking,tcp,connection,hostname,Android,Networking,Tcp,Connection,Hostname,我正试图打开一个TCP连接,从我的android应用程序到运行在流动网络WiFly上的服务器。WiFly在我的路由器上显示为主机名WiFly,ip地址为192.168.1.4 当我使用ip地址连接时,连接会正确打开,但如果我使用主机名连接,则会出现错误,因此: 无法打开主机“wifly”:没有与主机名关联的地址 下面是使用的一段代码: try { InetAddress serverAddr = InetAddress.getByName("wifly");

我正试图打开一个TCP连接,从我的android应用程序到运行在流动网络WiFly上的服务器。WiFly在我的路由器上显示为主机名WiFly,ip地址为192.168.1.4

当我使用ip地址连接时,连接会正确打开,但如果我使用主机名连接,则会出现错误,因此:

无法打开主机“wifly”:没有与主机名关联的地址

下面是使用的一段代码:

    try 
    {
        InetAddress serverAddr = InetAddress.getByName("wifly");

        Log.d("TCP Client", "C: Connecting...");

        //create a socket to make the connection with the server
        Socket socket = new Socket(serverAddr, SERVERPORT);

        .......
    }

有什么想法吗?

你应该检查一下名字是否正确 您可以使用列出所有主机

import java.io.*;
import java.net.*;
import java.util.*;
import static java.lang.System.out;

public class ListNets {

    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))
            displayInterfaceInformation(netint);
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %s\n", inetAddress);
        }
        out.printf("\n");
     }
}  
import java.io.*;
导入java.net。*;
导入java.util.*;
导入静态java.lang.System.out;
公共类列表网{
publicstaticvoidmain(字符串args[])抛出SocketException{
枚举网络=NetworkInterface.getNetworkInterfaces();
用于(网络接口netint:Collections.list(nets))
显示接口信息(netint);
}
静态无效显示接口信息(NetworkInterface netint)引发SocketException{
out.printf(“显示名称:%s\n”,netint.getDisplayName());
out.printf(“名称:%s\n”,netint.getName());
枚举inetAddresses=netint.getInetAddresses();
用于(InetAddress:Collections.list(inetAddresses)){
out.printf(“InetAddress:%s\n”,InetAddress);
}
out.printf(“\n”);
}
}  
代码取自


查看它的实际名称,然后使用它。

我修改了代码,使其在android上运行,但结果如下