在UDP Socket java android上发送几个数据

时间:2015-03-03 05:02:59

标签: java android sockets udp

我有问题在UDP上发送多个数据它发送重复的最后一个

 UDP_Client Client = new UDP_Client();

 Client.Message = "Your message1";
 Client.NachrichtSenden();
 Client.Message = "Your message2";
 Client.NachrichtSenden();
 Client.Message = "Your message3";
 Client.NachrichtSenden();

输出

  

您的留言3

     

您的留言3

     

您的留言3

我使用此来源

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;

public class UDP_Client 
{
private AsyncTask<Void, Void, Void> async_cient;
public String Message;

@SuppressLint("NewApi")
public void NachrichtSenden()
{
    async_cient = new AsyncTask<Void, Void, Void>() 
    {
        @Override
        protected Void doInBackground(Void... params)
        {   
            DatagramSocket ds = null;

            try 
            {
                ds = new DatagramSocket();
                DatagramPacket dp;                          
                dp = new DatagramPacket(Message.getBytes(), Message.length(), Main.BroadcastAddress, Main.SERVER_PORT);
                ds.setBroadcast(true);
                ds.send(dp);
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            finally 
            {
                if (ds != null) 
                {   
                    ds.close();
                }
            }
            return null;
        }

        protected void onPostExecute(Void result) 
        {
           super.onPostExecute(result);
        }
    };

    if (Build.VERSION.SDK_INT >= 11) async_cient.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    else async_cient.execute();
  }
}

(表格:Send and receive data on UDP Socket java android

1 个答案:

答案 0 :(得分:0)

固定

 public class UDP_Client 
 {
     private AsyncTask<Void, Void, Void> async_cient;
     public String Message;

     @SuppressLint("NewApi")
     public void NachrichtSenden(final String word)
     {
         async_cient = new AsyncTask<Void, Void, Void>() 
         {
             @Override
             protected Void doInBackground(Void... params)
             {   

                 DatagramSocket ds = null;

                 try 
                 {
                     ds = new DatagramSocket();
                     DatagramPacket dp;                          
                     dp = new DatagramPacket(word.getBytes(), word.length(), InetAddress.getByName(TXIp), TXPort);
                     ds.setBroadcast(true);
                     ds.send(dp);
                 } 
                 catch (Exception e) 
                 {
                     e.printStackTrace();
                 }
                 finally 
                 {
                     if (ds != null) 
                     {   
                         ds.close();
                     }
                 }
                 return null;
             }

             protected void onPostExecute(Void result) 
             {
                super.onPostExecute(result);
             }
         };

         if (Build.VERSION.SDK_INT >= 11) async_cient.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
         else async_cient.execute();
     }
   }

并使用

   UDP_Client Client = new UDP_Client();
   Client.NachrichtSenden("Your message1");
   Client.NachrichtSenden("Your message2");
   Client.NachrichtSenden("Your message3");