Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:24:18+00:00 2026-05-31T13:24:18+00:00

I have a service which runs two asynctasks. One is for receiving multicast packets

  • 0

I have a service which runs two asynctasks. One is for receiving multicast packets and another for receiving udp packets. I have registered a wifi state listener and want to stop listening for packets when connection to an access point is lost. But when I try closing either of the the sockets and try to end the while loop, the service itself shuts down. I want the service to be running during the entire time until my activity exits.
Following are the relevant parts of my code. Brackets might be misplaced…

public class ReceiveService extends Service {

private Receive r;
private ReceiveMulticast rm;
private boolean running = true;

private boolean receive = false, receivemulti = false;


@Override
public IBinder onBind(Intent arg0) {
    // return binder;
    return null;
}

@Override
public void onCreate() {

    try {

        r = new Receive();
        rm = new ReceiveMulticast();


        if (wifi.checkwifi(this)) {//check if connected to access point
            wifiInit();
            receive();
            receivemulti();
        }

    } catch (Exception e) {
        Log.d("test", "exception in oncreate");
        //e.printStackTrace();
    }

}

public void wifiInit() {
    wm = wifi.getWifiManager(this);
    myip = wifi.getmyip(wm);
    wifi.acquirelock(wm);
}

public void receive() {
    r.execute();
    receive = true;

}

public void receivemulti() {
    rm.execute();
    receivemulti = true;

}

private BroadcastReceiver WifiStateChangedReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        //monitor changes in wifi state
    }
};

@Override
public void onDestroy() {
    try {
        if (!r.ds1.isClosed())
            r.ds1.close();
        if (!rm.s.isClosed()) {
            rm.s.leaveGroup(rm.group);
            rm.s.close();
        }

        super.onDestroy();
    } catch (Exception e) {
        Log.d("test", "Exception in destroy");
    }
}

private class Receive extends AsyncTask<Void, String, Void> {

    private DatagramSocket ds1;
    private DatagramPacket p;

    protected void onPreExecute() {

    }

    protected Void doInBackground(Void... params) {

        try {
            ds1 = new DatagramSocket(7777);
        } catch (SocketException e) {
            Log.d("test", "Exception in new datagram");
            //e.printStackTrace();
        }
        int buffer_size = 1024;
        byte buffer1[] = new byte[buffer_size];
        p = new DatagramPacket(buffer1, buffer1.length);

        while (running) {
            try {
                ds1.receive(p);
                String sip = p.getAddress().getHostAddress();
                String rec = new String(p.getData(), 0, p.getLength());

                //publishProgress(sip + "+" + rec);
            } catch (Exception e) {

                running = false;
            }
        }
        return null;

    }

    protected void onProgressUpdate(String... progress) {
        //do stuff

    }

    protected void onPostExecute(Void result) {
        stopSelf();
    }

}

private class ReceiveMulticast extends AsyncTask<Void, String, Void> {

    private String ip = "224.0.0.10";
    private int port = 6789;
    private MulticastSocket s;
    private DatagramPacket mp;
    private InetAddress group;

    protected void onPreExecute() {

    }

    protected Void doInBackground(Void... params) {

        try {
            group = InetAddress.getByName(ip);
            s = new MulticastSocket(port);
            s.joinGroup(group);
        } catch (UnknownHostException e1) {
            Log.d("test", "exception unknown host rm");

            //e1.printStackTrace();
        } catch (IOException e) {
            Log.d("test", "exception io rm");

            //e.printStackTrace();
        }

        byte[] buffer2 = new byte[1024];
        mp = new DatagramPacket(buffer2, buffer2.length);

        while (running) {

            try {
                s.receive(mp);
                String sip = mp.getAddress().getHostAddress();
                String rec = new String(mp.getData(), 0, mp.getLength());

                //publishProgress(sip + "+" + rec);
            } catch (Exception e) {

                running = false;

            }
        }
        return null;
    }

    protected void onProgressUpdate(String... progress) {

        //do stuff


        }

    }

    protected void onPostExecute(Void result) {
        stopSelf();
    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:24:18+00:00Added an answer on May 31, 2026 at 1:24 pm

    Stopping normally or stopping with an exception?

    You are calling stopSelf() in the postExecute() of one of the AsyncTasks. your while(true) … loop will exit if you get an exception, then postExecute() is called, which calls stopSelf(). This is likely the cause, but there’s no way of knowing since you don’t log the exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I have to following problem. I have a WCF Service which runs under
I have implemented a small Windows Service which runs every 9 minutes and writes
I have a Windows Service written in Delphi which runs a number of programs.
I have a medium sized application that runs as a .net web-service which I
I have a service which has one endpoint , I have defined this endpoint
I have two apps, one runs in namespace com.gtosoft.voyager and the other is com.gtosoft.dash.
I have a windows service which runs like a scheduler, it extends an interface
I currently have a Windows Service which continually runs throughout the day. It has
We have a Windows Service which runs on 2003 Server. It opens a source
I have a WCF service method which takes more than two hours to execute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.