Android的搜索服务器自动 [英] Android search for server automatically

查看:147
本文介绍了Android的搜索服务器自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,它(通过网络)与桌面服务器应用程序(无论是在Java的编程和基于图形用户界面)进行通信。现在为了进行测试,我只是有一种指定的android手机的插座。然而,在将来,当我发布应用程序,我希望它能够通过自身来试图连接到服务器。

有些想法我有,到目前为止越来越电话的IP地址,然后通过同一端口的C类地址空间循环,看是否所有的插座连接。难道你们还有什么你可以建议?

此外,什么是让手机的IP地址在Android的最佳方法是什么?我有办法,但它只能在Android 2.3和更低......在ICS它完全触发器,并给出随机乱码。在这里,它是:

 公共字符串getLocalIpAddress(){
    尝试 {
        对于(枚举<的NetworkInterface> EN = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements()){
            NetworkInterface的INTF = en.nextElement();
            对于(枚举< InetAddress类> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements()){
                InetAddress类InetAddress类= enumIpAddr.nextElement();
                如果(!inetAddress.isLoopbackAddress()){
                    返回inetAddress.getHostAddress()的toString()。
                }
            }
        }
    }赶上(例外前){
    }
    返回null;
}
 

解决方案

在循环C类网络不必要的矫枉过正。我不会使用它,除非为一个彻底的最后一招。您的用户可能具有A级网络(10.xxx,在工作场所普及型),或B级,因为......他喜欢这样; - )

更好的方法是利用广播地址(或者,如果你多播preFER,但它更多的工作)。您可以基于UDP数据报实现简单的发现协议的应用程序。在这种情况下,你的服务器监听广播端口,如UDP 192.168.0.255:6666(您选择的端口),并通过发送IP回应任何活动:PORT它听来请求者

 电话>广播:有没有人?
Server1->电话:我在这里跟我说话的192.168.0.123:6666。
Server2->电话:我在这里跟我说话的192.168.0.124:6666。
 

客户端就可以显示可用的服务器,并问哪一个用户想连接,然后连接到的答案提供了TCP端口(我假定客户端 - 服务器连接的TCP,但它可以是任何东西,这个想法是一样的)。

I have an android application which (over the network) communicates with a desktop server application (Both programmed in Java and GUI based). For now to test, i simply have a way to specify the socket on the android phone. However, in the future when i release the application, i want it to be able to attempt connecting to the server by itself.

Some ideas i have had so far is getting the IP address of the phone, and then looping through a class C address space on the same port and seeing if any of those sockets connect. Do you guys have anything else you can suggest?

Also, what is the best way to get the phone's IP Address in Android? I have a way but it only works on android 2.3 and lower...on ICS it completely flops and gives random gibberish. Here it is:

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception ex) {
    }
    return null;
}

解决方案

Looping over C-class network needless overkill. I wouldn't use it unless as an utter last resort. Your user may have A-class network (10.x.x.x, popular in workplaces), or B-class, because... he likes it so ;-)

The better way would be to utilise broadcast address (or multicast if you prefer, but it's a bit more work). You may implement simple discovery protocol for your application based on UDP datagrams. In such case your server listens to the broadcast port, e.g UDP 192.168.0.255:6666 (port of your choosing) and responds to any activity by sending the IP:PORT it's listening to to the requester.

Phone->Broadcast: "Is there anybody?"
Server1->Phone  : "I am here. Talk to me at 192.168.0.123:6666"
Server2->Phone  : "I am here. Talk to me at 192.168.0.124:6666"

Client can then display available servers and ask to which one the user wants to connect, then connect to TCP port provided in the answer (I assume client-server connection is TCP, but it can be anything, the idea is the same).

这篇关于Android的搜索服务器自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆