0

Hi everyone this is my first question here so please don't be brutal for me, i also would like to sorry for my bad english in advance. So i am writing a simple app which job is to send and listen to UDP protocol on a specified port, and my problem comes when the app is minimized by home or back button and then brought back to front. After it is brought back my app throws an exception of EADDRINUSE which is caused by my UDP receiver thread:

public void run() {
        try {
            //Opening listening socket
            Log.d("UDP Receiver", "Opening listening socket on port "+LISTENING_PORT+"...");
            DatagramSocket socket = new DatagramSocket(LISTENING_PORT);
            socket.setBroadcast(true);
            socket.setReuseAddress(true);

            while(true){
                //Listening on socket
                Log.d("UDP Receiver", "Listening...");
                byte[] buf = new byte[1024];
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                message_received = new String(packet.getData()).trim();
                Log.d("UDP", "Received: '" + message_received + "'");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pokaztekst = (TextView) findViewById(R.id.textView);
                        pokaztekst.setText(message_received);
                        if(message_received.contains("192.168")){

                            String[] split_message = message_received.split("\"");
                            if (!listaurzadzen.toString().contains(split_message[3])) {

                                listaurzadzen.add(createPlanet("urzadzenie", split_message[3] + "\r\n" + split_message[1]));
                                lv.setAdapter(simpleAdpt);

                            }
                        }

                    }
                });

            }
        } catch (Exception e) {
            Log.e("UDP", "Receiver error", e);
        }
    }
}

and i start the thread in the onCreate method like this:

Thread receiver = new Thread(new Receiver());
@Override
protected void onCreate(Bundle savedInstanceState) {

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

        receiver.start();

The consequence of this error is that the listview field and textview field in which the received messages should be displayed are blank. I hope someone can help me with this.

yole
  • 80,603
  • 15
  • 224
  • 177
Vincenzo
  • 31
  • 5
  • You should close your socket before you try to open a new socket on the same port. The port is already in use. Minimized??? What is that? – greenapps Apr 11 '15 at 18:28
  • Minimized i meant like when the app is sent to background after pressing home or back button. – Vincenzo Apr 11 '15 at 18:32
  • Can you tell me where i should close the socket? Wouldn't it be better to end the thread somehow in the onPause method? and then the receiver thread would be started again when the onCreate method is called after the app is brought back. – Vincenzo Apr 11 '15 at 18:40
  • When you press back button onPause,onStop and finally onDestroy is called. Do you close socket in onDestroy()?. – fukanchik Apr 11 '15 at 18:41
  • I suggest you do simple educational experiment: override all lifecycle methods and add Log.i("onStart()), etc. Also,have a look here: http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – fukanchik Apr 11 '15 at 18:43
  • Ok so i moved the socket declaration before onCreate() method so i have acces to it and i added "protected void onDestroy(){ socket.close(); super.onDestroy(); }" and now everything works fine thanks alot i own you. – Vincenzo Apr 11 '15 at 18:54

0 Answers0