1

嗨朋友们,我遇到了一个处理实时接收多播数据包的问题。下面是多播客户端的代码,它必须接收 28 字节的多播数据并在 EditText 视图中显示该数据。服务器每隔一秒将这个 28 字节的数据发送到多播客户端。

我的问题是客户端在两者之间缺少许多消息。它正在接收多播数据,但所有数据均未显示在 EditText 中。我无法找到问题所在。任何建议将不胜感激。提前谢谢大家。

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;
    }   

}
4

0 回答 0