1. Are you ready for the Galaxy S20? Here is everything we know so far!

Multicast Message Loss on Android

Discussion in 'Android Development' started by chandradeor, Jul 25, 2013.

  1. chandradeor

    chandradeor Lurker
    Thread Starter

    Hi friends i am here with a problem which deals with realtime receiving of multicast packets.Below is the code of multicast client which has to receive multicast data of 28 Bytes and display that data in EditText View . The server is sending this 28 byte data at every one second to multicast client.
    my Problem is that the client is missing many messages in between. It is receiving the multicast data but all data are not displayed in EditText. I am not able to find the problem. Any suggestion will be appreciated. Thanking you all in advance

    [HIGH]package com.example.cdttiming; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; import java.net.UnknownHostException; import android.app.Activity; import android.content.Context; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { EditText time; String s; Button button; InetAddress ia = null; byte[] bmessage = new byte[1500]; DatagramPacket dp = new DatagramPacket(bmessage, bmessage.length); MulticastSocket ms = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); time = (EditText) findViewById(R.id.et_time); try { WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); //wm.setWifiEnabled(true); WifiManager.MulticastLock multicastLock = wm.createMulticastLock("multicastLock"); multicastLock.setReferenceCounted(true); multicastLock.acquire(); ia = InetAddress.getByName("226.1.1.1"); try { ms = new MulticastSocket(4321); } catch (IOException e) { e.printStackTrace(); } try { ms.joinGroup(ia); } catch (IOException e) { e.printStackTrace(); } ms.setReuseAddress(true); } catch (UnknownHostException e) { } catch (IOException e) { } } public void startProgress(View view) { Runnable runnable = new Runnable() { @Override public void run() { while(true) { try { ms.receive(dp); s = new String(dp.getData(),0,dp.getLength()); } catch (UnknownHostException e) { } catch (IOException e) { } MainActivity.this.runOnUiThread (new Runnable() { @Override public void run() { time.setText(s); } }); } // while } }; new Thread(runnable).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; } }[/HIGH]
     


Loading...

Share This Page

Loading...