编辑:我已经在stackoverflow上看了几个解决方案,但没有一个有任何帮助

我正在制作的应用程序连接到设备,然后将选定的SSId和密码发送给它 . 设备(数据发送到的设备)运行UDP服务器并将确认发送回Android设备 .

应用程序成功发送数据包,另一方收到数据包 . 但如果我再尝试发送任何东西,它就会失败 . 此外,我没有收到服务器的任何确认 .

以下是发送数据的代码

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);

        }
    }

此AsyncTask位于recyclerview的适配器中,并在单击单行时执行 .

接收器的代码位于不同的活动中

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

以下是我运行应用程序时的日志

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

从日志中可以看出,代码在socket.receive()处停止 .

任何帮助表示赞赏 .