vendredi 29 mai 2015

How to receive multiple UDP message using AsyncTasks?

I have written a UDP server using AsyncTasks. This is code for receiving UDP packet in Async Backgroung Process. My question is: How to modify this code to receive multiple message? My aim is to write a code which can simultaneously receive multiple message and can send message. thank you.

public class MainActivity extends Activity 
{
    public TextView tv;
    public TextView tv2;
    public TextView tv3;

    public MyAsyncTask myTask;

    int i =0;
    // String s;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button button1 = (Button) findViewById(R.id.button1);
         tv = (TextView) findViewById(R.id.textView1);
         tv2 = (TextView) findViewById(R.id.textView2);
         tv3 = (TextView) findViewById(R.id.textView3);
        //TextView textMessage = (TextView) findViewById(R.id.textView2);

         myTask = new MyAsyncTask(this);
         myTask.execute("ppp");


        button1.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v) 
            {
                /*cheack ping message*/
                //boolean morgan= isOnline();
                //String s = String.valueOf(morgan);
                tv.setText("kkkkkkkkkk");   // print ping message

                Log.d("MyTag@","This is sample log message");

                //new MyAsyncTask(getApplicationContext()).execute("ppppp");

            }

        });




    }



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

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.one)
        {
            Toast.makeText(getApplicationContext(), "Toast Message ONE", Toast.LENGTH_LONG).show();

        }
        if (id == R.id.two)
        {
            Toast.makeText(getApplicationContext(), "Toast Message TWO", Toast.LENGTH_LONG).show();
        }
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }




}

class MyAsyncTask extends AsyncTask <String, Void, String>
{
     private Context context;
     public Activity mactivity;
     static String jj ="helooooooo";
    public MyAsyncTask(Activity a)
    {
        // TODO Auto-generated constructor stub
        this.mactivity = a;
    }

    @Override
    protected String doInBackground(String... params) 
    {
        try
        {
            byte[] inbuf = new byte[1000]; // default size
            DatagramPacket packet = new DatagramPacket(inbuf, inbuf.length);
            Log.d("p2p", "3");
            DatagramSocket socket = new DatagramSocket(6000);

            socket.receive(packet);
            Log.d("p2p", "4");
            int numBytesReceived = packet.getLength();
            //System.out.println(numBytesReceived);
              jj = new String(inbuf);
            //System.out.println(s);
            //System.out.println(inbuf[2]);

            socket.close();


        }
        catch(Exception e)
        {
            Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
            e.printStackTrace() ;
        }


        Log.d("MyHello","doInBackground");
        return jj;
    }

     protected void onPostExecute(String result)
     {
         Log.d("MyHello","onPostExecute "+result);
         //context.;
         ((MainActivity) mactivity).tv3.setText(jj);

     }

}

Aucun commentaire:

Enregistrer un commentaire