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

  • Home
  • SEARCH
  • 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 9113371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:56:56+00:00 2026-06-17T03:56:56+00:00

I’m writing a class SipProvider that could receive and send datagramPacket. This class is

  • 0

I’m writing a class SipProvider that could receive and send datagramPacket. This class is part of a SIP Stack i’m working on.

The API i have been provided doesn’t make SipProvider Implement Runnable or Extend Thread.

The solution I’m trying to implement is to create a new class inside SipProvider which will extend thread. I want to give argument to the thread and i have a problem of compilation (No enclosing instance of type SipProvider is accessible), the thread cannot be instantiated because it is related to a SipProvider (i found it should be static but didn’t know how to do it).

I’ve looked up the Internet on how implementing a thread inside a class but didn’t find a solution. Is there a known way to do it.

this is a snapshot of what i’ve been trying to do. This is only a part of the class.

public class SipProvider {

    //startOn is the method which allow the user to listen on a port 
    //so the user don't have to bother creating a thread and so on
public static SipProvider startOn(listeningPoint) {
        SipProvider sipProvider = new SipProvider();
        thread.sipProvider = sipProvider;
        thread.run();
        return sipProvider; 
}

    //this is the thread i want to handle the listening process 
public class ReceiveThread extends Thread{

    public SipProvider sipProvider;

    public ReceiveThread(SipProvider sipProvider){
        this.sipProvider = sipProvider;
    }

    @Override
    public void run(){
        try {

            int MAX_LEN = 200;
            DatagramSocket datagramSocket = new DatagramSocket(
                    listeningPoint.getPort());
            sipProvider.datagramSocket = datagramSocket;
            byte[] buffer = new byte[MAX_LEN];
            DatagramPacket packet = new DatagramPacket(buffer, MAX_LEN);
            while (!datagramSocket.isClosed()) {
                sipProvider.setSipListener(sipListener);
                datagramSocket.receive(packet);
                                    //handle packet content

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}

EDITH:

One other solution i found besides the one proposed (which works perfectly) is the instantiate a new thread and define its run() method at the same time. the method startOn in my example starts the listening on a listeningPoint (which contains information about port address and protocol used).

public static SipProvider startOn(final ListeningPoint listeningPoint) throws   SocketException {
    final SipProvider sipProvider = new SipProvider(listeningPoint);

    new Thread() {
        @Override
        public void run() {
            try {

                while (true) {
DatagramPacket packet = newDatagramPacket(new byte[200], 200);
sipProvider.datagramSocket.receive(packet);
String content = new String(packet.getData(), 0, packet.getLength());
sipProvider.sipListener.processContent(content);
                }

            } catch (Exception e) {
            }
        }
    }.start();
    return sipProvider;
} 
  • 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-06-17T03:56:57+00:00Added an answer on June 17, 2026 at 3:56 am

    This code snippet may help you:

    public class SipJob implements Runnable {
    
        private SipProvider sipProvider;
    
        public SipJob(SipProvider sipProvider) {
                      // pass other parameters, if you need
            this.sipProvider=sipProvider;
        }
    
        @Override
        public void run() {
            // do the code, use SipProvider
    
        }
    }
    

    This will contain the code you want to run. You can add as many parameters to the constructor as you want, you can use them from your business logic. I recommend not to pass resources (e.g. sockets), because they must be closed, and it is not wise to separate the opening and closing code (the same entity must be responsible for both).

    Now you need a Thread to execute:

    SipProvider sipProvider = ...;
    Runnable job = new SipJob(sipProvider);
    Thread runner = new Thread(job);
    runner.setDaemon(true);
    runner.start();
    

    Notice that – as far as I understood – this will run in the background, so I set setDaemon(true).

    I’m not sure, but you may have internal stuff in the SipProvider what you want to use in the thread. This way I recommend first to extend the SipProvider class, enabling access to those internal variables/methods, the use the child class in the SipJob class.

    EDIT: Another way to access the internals of SipProvider is to extend the class, and to define the SipJob class as an inner class (not inner static class, but rather instance class) in the descendant. This way you will automatically have access to the instance variables of the containing SipProvider – you even don’t have to pass it as an argument. Try this from the inner instance class: SipProvider.this.whateverField.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.