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 6801373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:05:32+00:00 2026-05-26T19:05:32+00:00

My application has a master and many slaves which respond to the master’s call-over

  • 0

My application has a master and many slaves which respond to the master’s call-over sockets and send the statistics over in an object. Right now, I’m testing the code with one master and two slaves. Code works fine with 1 slave but with 2 slaves, the objects received at the master’s end get populated twice i.e Two copies of the same object.

Maser’s code: The master receives periodically from slaves and hence exceuting using a timer:

public void run() {

    try {
        byte[] recvBuf = new byte[15000];
        DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
        DatagramSocket dSock = new DatagramSocket(4445);
        dSock.receive(packet);
        int byteCount = packet.getLength();
        ByteArrayInputStream byteStream = new ByteArrayInputStream(recvBuf);
        ObjectInputStream is = new ObjectInputStream(new BufferedInputStream(byteStream));
         //receiving the object pm of class PM
        pm1=(PM)is.readObject();
        }
 }

and the slave’s code:

 {
 InetAddress address = InetAddress.getByName("10.129.54.254");
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream(15000);
            os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
            os.flush();
            //sending the object pm of class PM
            os.writeObject((PM)pm);
            os.flush();
            byte[] sendBuf = byteStream.toByteArray();
            DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length, address, 4445);
            int byteCount = packet.getLength();
            DatagramSocket dSock = new DatagramSocket();
            dSock.send(packet);
            os.close();
            dSock.close();
            }
      }

Doubt:
1. Should I store the objects from the two slaves in an array? If so, how do I differentiate between the two received object over the socket so that the same object isn’t stored twice? Assuming that the object sent over has a unique attribute such as id.
ie

class PM{
int uniqueid;
}
  1. I’m trying to write the entire code and hence don’t want to use Jini or other APIs.
    Thanks!
  • 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-26T19:05:33+00:00Added an answer on May 26, 2026 at 7:05 pm

    When you receive

         //receiving the object pm of class PM
        pm1=(PM)is.readObject();
    

    Don’t just store it in a field as the next object from the second slave will also be placed in that fields.

    Instead you should use a local variable, read the contents of the object and act on it. Or if you want another thread to process it, add a task to a single threaded ExecutorService to process the objects.

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

Sidebar

Related Questions

I have a JSP java application that has a name AAA-master so currently my
My application has one activity which starts two services but does not bind them.
Our application has many controls that are created dynamically. For example, a navigation pane
I was just designing this application and made many .aspx files which have my
Has anyone got this working in a web application? No matter what I do
My application has multiple SQLite tables(around 20 with different fields). This I have created
My Application has 3 activities (MainActivity,SampleActivity,TempActivity) and Application have to start from MainActivity because
The application has been put in iCloud since the beginning, so I have the
My application has two layouts A and B. In A a user chooses some
My Application has the following structure: myproject (primary Silverlight project) myproject.Web (website for the

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.