Android的3G UDP广播 [英] Android 3G UDP Broadcast

查看:195
本文介绍了Android的3G UDP广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发一个跨平台的UDP服务器客户机的早期阶段。就我而言,客户端发送到它们连接到Android手机广播,通过移动热点选项。

I am in the early stages of developing a cross platform UDP server client. In my case, the clients send a broadcast to an android phone that they are connected to, via the mobile hotspot option.

我目前由具有相同设备上的服务器和客户端测试此。不过,我只知道如何让广播地址,多亏了Boxee的远程应用程序。

I am currently testing this by having the server and client on the same device. However, I only know how to get the broadcast address, thanks to a boxee remote app.

是任何人都知道的一种方式做同样如下code,但要获得移动网络广播地址?

Is anyone aware of a way to do the same as the code below, but to get the Mobile network broadcast address?

所以,这里是code:

So, here is the code:

InetAddress getBroadcastAdd(Context context) throws IOException {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
      quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
}

有关鹰的眼睛的读者,你会聚集,我其实并不需要这样code表示该应用在其他设备的工作将从无线网络获取广播地址,但我确实需要做一些测试上这个装置第一位。

For the eagle eyed readers, you will have gathered that I actually do not need this code for the app to work as the other devices will get a broadcast address from the wifi but I do need to do some testing on this one device first.

在此先感谢!

推荐答案

大多数网络(WiFi和3G)使用 NAT 的。 NAT允许出站连接,但是prevents入站(互联网设备)的连接。

Most networks (Wifi and 3G) use NAT. NAT allows outbound connections, but prevents inbound (internet to device) connections.

当您的服务器和设备都在同一个本地网络(WIFI),那么这工作你不是穿越NAT网关。

When your server and device are both on the same local network (wifi), then this works as you are not traversing NAT gateway.

理由:你正在尝试做的(从互联网连接设备)将不会在大多数网络

Rationale: what you are trying to do (connecting from internet to device) will not work in most networks.

更新

如果您的设备均是同一本地网络中,那么你可以使用的广告/发现服务标准化的方式:卓悦又名零配置。还有的是,据报道,在Android上运行Java实现: http://jmdns.sourceforge.net/

If your devices are both on same local network then you can use a standardised way of advertising/discovering services: Bonjour aka Zeroconf. There is java implementation that is reported to work on android: http://jmdns.sourceforge.net/

另请参见 http://home.heeere.com/tech-androidjmdns.html

这篇关于Android的3G UDP广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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