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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:31:26+00:00 2026-05-28T20:31:26+00:00

I am writing a content distribution network in java. I have a Link class

  • 0

I am writing a content distribution network in java. I have a Link class to manage sockets between two nodes in the system. There are two programs, RouterNode and DiscoveryNode.

When a router node starts up, the first thing it does is try to initialize a connection to the discovery node:

public RouterNode(int num)
{
    myNumber = num;
    input = new Scanner(System.in);

    try {
        discoveryServer = new Socket("MONDAY-PC", 60111);
        myServerLink = new Link(this, discoveryServer);
    } catch (IOException e) {
        System.out.println("Socket could not be opened. Node terminating.");
        System.exit(-1);
    }

There is more to the constructor, but my problem keeps my program from getting past this try block.

The constructor of the link class (called on the line ‘myServerLink = new Link(this, discoveryServer); ) looks as such:

public Link(Node n, Socket s)
{
    parentNode = n;
    regSocket = s;
    try {
        out =  new DataOutputStream(regSocket.getOutputStream());
        in = new DataInputStream(regSocket.getInputStream());
    } catch (IOException e) {
        System.out.println("Data Streams could not be created on the link.");
        e.printStackTrace();
    }


    new Thread(new LinkListenerThread(this, in)).run();
}

where the last line of this constructor starts a new thread that is designed to listen on the socket for incoming messages that are being passed.

The run() method in LinkListenerThread stars as follows:

@Override
public void run()
{
    byte[] message;     

    System.out.println("Link now active and running.");
    while(!done)
    {
        System.out.println("attempting to read from socket...");

        try {

            // read now many bytes the following message will be
            byte[] messageLengthBytes = new byte[4];
            in.read(messageLengthBytes, 0, 4);

My problem is that once I instantiate the link from the router node, it’s execution stops from what seems to be the LinkListenerThread blocking it when it calls in.read(). This listener is running on a separate thread so I am not sure if this is actually something strange with threads, or it is just an example of my lack of experience with them.

I have another instance in my program where I am reading on a separate thread

Could this be caused because the node classes aren’t explicitly implementing runnable and therefore are not on their own threads?

Any help is greatly appreciated.

Edit 1: I have made the Node classes implement Runnable and starting them on their own threads, but it still locks up when the in.read() is called;

  • 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-28T20:31:27+00:00Added an answer on May 28, 2026 at 8:31 pm

    You intend to start a thread by calling Thread.run() method instead of Thread.start(). And this makes your program single-threaded, then the I/O ops block the only thread(the main thread) in the try-catch clause if there is no data coming in or the while loop never ends.

    Just use Thread.start() to start a thread can fix your problem.

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

Sidebar

Related Questions

I'm have a StringBuilder that is writing content to a file. Towards the end
I'm writing a simple content management system. I need to store SHA1 hash values
I am writing a script for renaming/copying the content of a file. I have
Is there a way of writing an XPath expression to select the content of
Writing @Url.Content(~/Something/Something.html) in razor renders /AppFolder/Something/Something.html Is there a way to render the full
I'm writing a Content Management System in PHP, and I want it to be
I am writing a content management system using JQuery.ajax & C#. The JQuery calls
What is the best way of writing the some content to the beginning and
Background I am writing and using a very simple CGI-based (Perl) content management tool
I'm writing an C# App (WinForm) with a ListBox having content added by 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.