Android的MutlicastSocket构造问题问题、Android、MutlicastSocket

由网友(你若安好,那还得了)分享简介:我正在写一个应用程序,通过多播做一些基本的沟通。我遇到一个问题,可以推测的原因。我根据API在构造限定插座,但它实际上没有设定变量指定。这里是一堆多余的东西去掉一个基本的code片断:I'm writing an app to do some basic communication via multicast. I...

我正在写一个应用程序,通过多播做一些基本的沟通。我遇到一个问题,可以推测的原因。我根据API在构造限定插座,但它实际上没有设定变量指定。这里是一堆多余的东西去掉一个基本的code片断:

I'm writing an app to do some basic communication via multicast. I'm running into a problem and can figure why. I'm defining the socket according to the constructor in the API, yet it's not actually setting the variables as specified. Here is a basic code snippet with a bunch of the extra stuff removed:

import java.net.MulticastSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
...

private InetAddress groupInetAddr = InetAddress.getByName("239.42.42.42");;
private int groupPort = 42000;
private MulticastSocket groupSocket;

netInt = NetInfo.getInterface(); 
//This is a custom method that chooses a candidate NetworkInterface 
//from available options.  Returns a NetworkInterface object

try{
   groupSocket = new MulticastSocket(groupPort);
   groupSocket.setNetworkInterface(netInt);
   groupSocket.joinGroup(groupInetAddr);
   groupSocket.setTimeToLive(64);
} 
catch (Exception e){
   Log.i(TAG, "FAILED");
}

我有一些测试code紧随此code,以确认该插座已经被正确创建,这是不是...

I have some test code immediately following this code to confirm that the socket has been created properly, and it isn't...

Log.i(TAG, "groupInetAddr: " + groupInetAddr.toString());
Log.i(TAG, "groupPort: " + groupPort);
Log.i(TAG, "groupSocket.getInetAddress: " + groupSocket.getInetAddress());
Log.i(TAG, "groupSocket.getPort(): " + groupSocket.getPort());

测试的日志结果

The log results of the test:

GroupSender﹕ groupInetAddr: /239.42.42.42
GroupSender﹕ groupPort: 42000
GroupSender﹕ groupSocket.getInetAddress: null
GroupSender﹕ groupSocket.getPort(): -1

所以,你可以看到,InetAddress是否被正确创建,所以这不是问题,但套接字未分配的InetAddress作为目标。此外,当我检查Wireshark的,没有IGMP报文发送通过局域网的地址。

So, as you can see, the InetAddress is being created properly, so that's not the problem, but the socket isn't assigning the InetAddress as the destination. Additionally, when I check Wireshark, there is no IGMP message send out over the LAN to that address.

此外,我已经添加了以下权限AndroidManifest.xml中允许访问所需的服务。

Additionally, I've added the following permissions to the AndroidManifest.xml to allow access to necessary services.

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

<uses-feature android:name="android.hardware.wifi" />

任何想法?但愿我只是失去了一些东西少。

Any ideas? Hopefully I'm just missing something small.

推荐答案

套接字已构建正确。你没有连接,所以它的 getInetAddress()返回null,它的目标端口为-1。

The socket has been constructed correctly. You didn't connect it, so its getInetAddress() returns null and its target port is -1.

这没有什么关系了哪些组播组加入。你的期望是有过错的。

That doesn't have anything to do with which multicast groups it has joined. Your expectations are at fault.

如果这台主机是不是已经是组成员的IGMP报文,只发送。

The IGMP message is only sent if this host isn't already a member of that group.

阅读全文

相关推荐

最新文章