3

I have an application android who send data to the server (PC) but I don't receive any data from the PC to the application. And, how can I do a listener for incoming UDP messages ?

because I need an app to be running all the time even if the app is closed. How would I ensure that my 'listener' service is always running? I would like to receive a notification when a message arrive from the server to the smartphone.

Here is my code :

public class MainActivity extends Activity implements TextWatcher, OnClickListener  {

Client client;
EnvoyerMess envoyermessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    client = new Client();
    client.execute();
           lancer.setOnClickListener( new OnClickListener() {

         @Override
         public void onClick(View v)  {

             envoyermessage=new EnvoyerMess();
             envoyermessage.execute();

       }

      });

}

class Client extends AsyncTask<Void,Void,String>{

     DatagramSocket client;
     String test ;


    public String doInBackground(Void...params){         
        String result=null;
        int port=4000;

        DatagramSocket clientSocket = null;
        byte[] receiveData = new byte[256];
        DatagramPacket packet = new DatagramPacket(receiveData, receiveData.length);
            try{ 
                InetAddress adresse = InetAddress.getByName("10.0.2.2");
                clientSocket = new DatagramSocket(port);
                clientSocket.receive(packet);
                result = new String(packet.getData());                          
                           Log.d("","Received :) ");
        }catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (clientSocket != null) {
                clientSocket.close();             
            }
        }
         return result;
    }


    public void onPostExecute(String result){ 
         if (result != null) { 
             //createNotify(); 
                 TextView tv = (TextView) MainActivity.this.findViewById(R.id.textView1);
              tv.setText(result);

    }
}

thanks in advance..

user3654295
  • 45
  • 2
  • 8
  • make it work in a client/server on your computer, to your computer first - and when that works, move it to the android. network comm from android to home machine has many pitfalls. (for example, are you using wifi inside your house or are you using the carriers network to your home? if the latter, at can safely posit that they code you have won't work - because 10.0.0.2 is not going to be accessible from that origination) – KevinDTimm May 20 '14 at 19:49

1 Answers1

1

I would start by ensuring that the PC is in fact sending the packets to the correct IP address. Do this by using wireshark, http://www.wireshark.org/ , or a similar tool.

To ensure that your task is running even though the application is is closed you need a service, http://developer.android.com/guide/components/services.html.

Edit:

I you are using an emulator you must first enable redirection in the avd router. This link provides instructions on how to accomplish this developer.android.com/tools/devices/emulator.html#redirection (I cant publish links so you'll have to copy-paste).

If you are developing in windows you would also need to enable telnet. This link describes the steps to accomplish that social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-client.aspx (again, copy-paste)

  • Thanks for your answer, the PC is sending packets to the correct IP address. I don't know how work services. Do you have exemple of application with client/server ? – user3654295 May 20 '14 at 20:06
  • I you don't know how to work with services this is the perfect opportunity to learn. Read the link and try. I've edited my answer to inclcude intructions on how to enable networking with the emulator. – Oscar Olsson May 20 '14 at 22:44