0

EDIT: I have already looked at several solutions on stackoverflow and none of them were of any help

The app I am making connects to a device and then sends the selected SSId and password to it. The device(to which the data was sent) runs a UDP server and sends an acknowledgement back to the android device.

The app successfully sends the packet and the packet is received on the other side. But if I try to send anything again it fails. Also I do not receive any acknowledgement from the server.

The following is the code to send the data

private class UdpSendTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            DatagramPacket packet;
            DatagramSocket socket = null;
            String msg = ssid + "/" + password;
            int port = 2390;
////            Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
            byte[] buff = (msg.getBytes());
            try {

                socket = new DatagramSocket(port);
                socket.setBroadcast(true);
                Log.d(TAG, "sendData: Socket Created");
                Log.d(TAG, "sendData: Data created");
//                InetAddress ip = InetAddress.getByName("192.168.1.7");
                InetAddress ip = InetAddress.getByName(ipAddress);
                packet = new DatagramPacket(buff, buff.length, ip, port);
                Log.d(TAG, "sendData: Packet Created");
                socket.send(packet);
                Log.d(TAG, "sendData: Packet Sent");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    socket.close();
                    Log.d(TAG, "sendData: Socket Closed");
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            activity.startActivity(intent);

        }
    }

This AsyncTask is located in a recyclerview's adapter and is executed when a single row is clicked.

The code for the receiver is located in a different activity

private class UdpReceiveTask extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(Void... voids) {

            boolean running = true;
            String data = null;
            DatagramPacket packet;
            DatagramSocket socket = null;
            byte buff[] = new byte[1024];

            try {
                Log.d(TAG, "doInBackground: Inside try");
                while (running) {
                    Log.d(TAG, "doInBackground: Inside while");
                    socket = new DatagramSocket(2390);
                    Log.d(TAG, "doInBackground: Socket Created");
                    packet = new DatagramPacket(buff, buff.length);
                    Log.d(TAG, "doInBackground: Packet Created");
                    socket.receive(packet);
                    socket.setBroadcast(true);
                    Log.d(TAG, "doInBackground: Packet received");
                    data = new String(packet.getData(), 0, packet.getLength());

                    Log.d(TAG, "doInBackground: Data == " + data);
                    if(!data.equals(null))
                        running = false;
                }


            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                if (socket != null) {
                    socket.close();
                    Log.d(TAG, "doInBackground: Socket Closed");
                }
            }
            return data;
        }

        @Override
        protected void onPostExecute(final String s) {
            super.onPostExecute(s);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
                    editTextIPAddress.setText(s);
                }
            });
        }
    }

The following is the log when i run the app

09-15 20:34:11.337 5538-5647/thefusionera.com.wifimoduletest D/:::::::::::::::::::: sendData: Socket Created
09-15 20:34:11.338 5538-5647/thefusionera.com.wifimoduletest D/:::::::::::::::::::: sendData: Data created
09-15 20:34:11.338 5538-5647/thefusionera.com.wifimoduletest D/:::::::::::::::::::: sendData: Packet Created
09-15 20:34:11.338 5538-5647/thefusionera.com.wifimoduletest D/:::::::::::::::::::: sendData: Packet Sent
09-15 20:34:11.338 5538-5647/thefusionera.com.wifimoduletest D/:::::::::::::::::::: sendData: Socket Closed
09-15 20:34:11.352 5538-5568/thefusionera.com.wifimoduletest D/AppTracker: App Event: stop
09-15 20:34:11.380 5538-5595/thefusionera.com.wifimoduletest D/--------------------: doInBackground: Inside try
09-15 20:34:11.380 5538-5595/thefusionera.com.wifimoduletest D/--------------------: doInBackground: Inside while
09-15 20:34:11.380 5538-5595/thefusionera.com.wifimoduletest D/--------------------: doInBackground: Socket Created
09-15 20:34:11.380 5538-5595/thefusionera.com.wifimoduletest D/--------------------: doInBackground: Packet Created

As evident from the log, the code stops at socket.receive().

Any help is appreciated.

Neeraj Athalye
  • 508
  • 5
  • 26
  • 1
    Closing the socket after send,, then recreating it to receive might lead to loss of data if the other side replies fast enough. The standard approach is to use one socket for the duration of the communication. – C. Gonzalez Sep 15 '17 at 15:34
  • @C.Gonzalez oh... Ok are you suggesting that I write the code for receiving the packet in the first asynctask it self?? – Neeraj Athalye Sep 15 '17 at 17:25
  • Either that or pass the same socket on to your receiving method. – C. Gonzalez Sep 15 '17 at 17:37
  • Seems like a packet wasn't actually sent and since [`DatagramSocket.receive()`](https://stackoverflow.com/q/40055175/3290339) is a blocking call it waits forever not letting any logs appear after the call. – Onik Sep 15 '17 at 17:50
  • @C.Gonzalez I will try that out. One more thing. I am not able to send also multiple times. It works only once and to send again I need to restart the app. Any help with that? – Neeraj Athalye Sep 16 '17 at 06:56
  • @C.Gonzalez I tried out what you said and it has fixed my problem. Thanks for your help! – Neeraj Athalye Sep 19 '17 at 12:39

0 Answers0