简体   繁体   中英

Multicast socket server not able to receive from a particular multicast on android mobile

I have an android mobile which acts as client and sends a multicast packet to 239.255.255.253

the code is

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            Thread t=new Thread(new Multi());
        t.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

class Multi implements Runnable
{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
                InetAddress ip=InetAddress.getByName("239.255.255.253");
            int port=4270;
            //Create a Multicast socket
            MulticastSocket sock=new MulticastSocket();
            String msg="Hello All";

            DatagramPacket pack=new DatagramPacket(msg.getBytes(),msg.length(),ip,port);
            sock.send(pack);
            sock.close();
                System.out.println("Packet sent");


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

    }

}

and my android manifest file is

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multicast"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <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.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.multicast.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Android and PC both are connected via router

On my pc iam running the server code which just receives the packets on 239.255.255.253 and 224.0.0.2.

The Server Code is

import java.io.*;
import java.net.*;


class receiver
{

    public static void main(String args[])
    {
        try
        {       
            //get the multicast ip
            InetAddress ip1 = InetAddress.getByName("239.255.255.253");
            InetAddress ip2 = InetAddress.getByName("224.0.0.2");
            int port=4270;

            MulticastSocket sock=new MulticastSocket(port);

            //join the multicast group
            sock.joinGroup(ip1);
            sock.joinGroup(ip2);
            while(true)
            {
            //create a datagram packet in which u will receive the msg  
                byte[] buffer=new byte[100];
                DatagramPacket pack=new DatagramPacket(buffer,buffer.length);
                sock.receive(pack);
                InetAddress ip= pack.getAddress();
                System.out.print(ip+":");
                System.out.println("the message received from the sender is "+new String(buffer));
            }
            //sock.close();     
        }
        catch(Exception e){

        }


    }



}

Iam unable to receive any packet on pc , but when i changed the multicast address of the client to "224.0.0.2" It works fine. Can anyone help me to solve this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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