开发者

Is MulticastSocket supported in Android 2.3?

开发者 https://www.devze.com 2023-02-03 15:27 出处:网络
Is MulticastSocket supported in Android 2.3? I have a module on Java Server. This server will send information uninterprupted. And I have a module Android Client (on emulator virtual device). I will

Is MulticastSocket supported in Android 2.3?

I have a module on Java Server. This server will send information uninterprupted. And I have a module Android Client (on emulator virtual device). I will start android client first for it to wait to receive server's information Then I will start Java server to send information. Finally I wanna my emulator can receive info from java server. PS: I will be successful if 2 modules are all java.

This is my server module (very simple)

  public class Server {

     public Server() throws IOException, InterruptedException{

        DatagramSocket socket = new DatagramSocket();

        byte[] b = new byte[1024]; 

        DatagramPacket dgram = new DatagramPacket(b, b.length,InetAddress.getByName
  ("239.0.0.1"), 5000);  
        System.err.println("Sending "+b.length+" bytes to "+dgram.getAddress()+':'+
  dgram.getPort()); 
        while(true) {   

           System.err.print(".");   
           socket.send(dgram);   

           Thread.sleep(1000); 
        } 
     }

  }

This is my android emulator (also very simple)

  public class ClientForm extends Activity {
     private EditText edcontentview;
     @Override
     public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clientform);
        edcontentview = (EditText)findViewById(R.id.edtcontentview);
        MulticastLock _wifiMulticastLock = ((WifiManager)this.getSystemService(Activity.WIFI_SERVICE)).createMulticastLock("_wifiMulticastLock");
          _wifiMulticastLock.setReferenceCounted(true);    
          _wifiMulticastLock.acquire();
        new Thread(new Runnable(){             
           @Override
           public void run() {
              byte[] b = new byte[1024];
              final DatagramPacket dgram = new DatagramPacket(b, b.length); 
              MulticastSocket socket = null;
              try {
                 socket = new MulticastSocket(5000);
       开发者_如何转开发          socket.joinGroup(InetAddress.getByName("239.0.0.1"));
              } catch (IOException e) {
                 e.printStackTrace();
              }  
              while(true) {   
                 try {
                    socket.receive(dgram);
                    Thread.sleep(1000);
                 } catch (IOException e) {
                    e.printStackTrace();
                 } catch (InterruptedException e) {
                    e.printStackTrace();
                 }   
                 new Handler().post(new Runnable() {
                 @Override
                    public void run() {
                       edcontentview.append("Received " + dgram.getLength() +     " bytes from " + dgram.getAddress());                                    
                    }
                 });
                 dgram.setLength(b.length);
              }
           }
        }).start();
     }
  }


Yes. Isn't this what you were looking for?


Possibly not. It appears that Multicast support in Android is not as solid as some of use might hope. See http://codeisland.org/2012/udp-multicast-on-android/

0

精彩评论

暂无评论...
验证码 换一张
取 消