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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:34:45+00:00 2026-05-30T01:34:45+00:00

For my homework assignment, I have a network of Nodes that are passing messages

  • 0

For my homework assignment, I have a network of Nodes that are passing messages to each other. Each Node is connected to a set amount of other Nodes (I’m using 4 for testing). Each Link has a weight, and all the Nodes have computed the shortest path for how they want their messages sent. Every Packet that is sent is composed of the message protocol (a hard-coded int), an integer that tells how many messages have passed through the sending Node, and the routing path for the Packet.

Every Node has a Thread for each of its Links. There is an active Socket in each Link. The Packets are sent by adding a 4-byte int to the beginning of the message telling the message’s length.

Everything works fine until I stress the network. For my test, there are 10 Nodes, and I get 5 of them to send 10000 packets in a simple while() loop with no Thread.sleep(). Without exception, there is always an error at some point during execution at the if(a!=len) statement.

Please let me know if I can clarify anything. Thanks in advance! Here is the code (from the Link Thread; send() and forward() are called from the Node itself):

protected void listen(){
    byte[] b;
    int len;
    try{
        DataInputStream in = new DataInputStream(sock.getInputStream());
        while(true){
            len = in.readInt();
            b = new byte[len];
            int a = in.read(b,0,len);
            if(a!=len){
System.out.println("ERROR: " + a + "!=" + len);
                throw new SocketException(); //may have to fix...this will happen when message is corrupt/incomplete
            }
            Message m = new Message(b);
            int p = m.getProtocol();
            switch (p){
                case CDNP.PACKET:
                    owner.incrementTracker();
                    System.out.print("\n# INCOMMING TRACKER: " + m.getTracker() + "\n>>> ");
                    owner.forward(m);
            }
        }
    }catch (IOException e){
e.printStackTrace();
    }
}

public void send(int tracker){
    String[] message = { Conv.is(CDNP.PACKET), Conv.is(tracker), owner.getMST().toString() };
    Message m = new Message(message);
    forward(m);
}

public synchronized void forward(Message m){
    try{
        OutputStream out = sock.getOutputStream();
        //convert length to byte array of length 4
        ByteBuffer bb = ByteBuffer.allocate(4+m.getLength());
        bb.putInt(m.getLength());
        bb.put(m.getBytes());
        out.write(bb.array());
        out.flush();
    }catch (UnknownHostException e){
System.out.println("ERROR: Could not send to Router at " + sock.getRemoteSocketAddress().toString());
        return;
    }catch (IOException e1){

    }
}
  • 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-30T01:34:47+00:00Added an answer on May 30, 2026 at 1:34 am
    int a = in.read(b,0,len);
    if(a!=len){
    

    That won’t work. The InputStream may not read all the bytes you want, it may read only what is available right now, and return that much without blocking.

    To quote the Javadocs (emphasis mine):

    Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer.

    You need to continue reading in a loop until you have all the data you want (or the stream is finished).

    Or, since you are using a DataInputStream, you can also use

    in.readFully(b, 0, len);
    

    which always reads exactly len bytes (blocking until those have arrived, throwing an exception when there is not enough data).

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

Sidebar

Related Questions

I have a homework assignment in Java that is tested using the commands: make
I have a homework assignment, and i am finished other then one question (see
Good day, Stack Overflow. I have a homework assignment that I'm working on this
For a homework assignment in linear algebra, I have solved the following equation using
I have a homework assignment that I am supposed to write a http server
This is a homework assignment, I have to write a program that reads a
Im almost done with a homework assignment that multiplies polynomials and has to have
So, a long story short, I have a Java homework assignment that requires a
I have a homework assignment that asks to create an order form. The order
I need some ideas for a homework assignment that I have. Consider the following

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.