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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:19:22+00:00 2026-06-14T07:19:22+00:00

Using the Socket class in Java, I’m trying to create a network of six

  • 0

Using the Socket class in Java, I’m trying to create a network of six clients that are each connected to each other.

I’ve got decent idea in place so far, I think, I’m just not sure how to do this.

Basically, I’ve got a list of hostnames stored in a String array. I open a ssh connection to each of the machines that I’ll be using, and launch my clients one by one.

The first client finds its hostname with InetAddress.getLocalHost().getHostName(), then compares this to the hostname list and figures out its NodeID:

for(i = 0; i < hostNames.length; i++){
    if(localHostName == hostNames[i]){
        NodeID = i;
        break;
    }
    ...

So here’s the hard part for me: I would at this point connect to the client at hostNames[i]. My plan was to have a different thread for each connection for each client. How should I go about creating these threads? Should I have a thread array set up beforehand and define the threads at this point?

Thread[] connections = new Thread[]();
...
    //in for loop
    connections[i] = new Thread(new ConnectionThread().start(hostNames[i]));
    // ConnectionThread being a tentative name for a custom class

This seems like it would be simple enough, but am I overthinking it? Oversimplifying it?

  • 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-14T07:19:23+00:00Added an answer on June 14, 2026 at 7:19 am

    Right away you’ve committed a newbie blunder:

    if(localHostName == hostNames[i])
    

    will always be false, because in java the == operator compares object identity – ie are the two objects the same object. What you want is:

    if(localHostName.equals(hostNames[i]))
    

    Next, the Thread[] idea is good, but pass in a Runnable to the thread and start the thread. Have your class use an instance field for the hostnames to connect to (the class doesn’t need to know its own hostname).

    Something like this:

    public class MyServer implements Runnable {
    
        private final String[] hostnames;
    
        public MyServer(String... hostnames) {
            this.hostnames = hostnames;
        }
    
        public void run() {
           for (String hostname : hostnames) {
               // connect to hostname
           }
        }
    
    }
    

    Then in your main (simplistically):

    Thread[] threads = new Thread[6];
    threads[0] = new Thread(new MyServer("foo", "bar"));
    threads[1] = new Thread(new MyServer("bar", "dog"));
    ...
    for (Thread thread : threads) {
        thread.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JMagick and have a simple Java class that loops over all images
I'm trying to do some Socket programming in Java, and I'm using the BufferedStreamReader.read(char[])
I'm using the .NET Socket class. Basically my program is to send XML commands
We are using Socket.IO to communicate real-time statistics to connected browsers; however due to
I have Android client app that communicates with server using Socket . On my
Using UNIX socket APIs on Linux, is there any way to guarantee that I
I'm using the socket.makefile method to create a file-like object on a UDP socket
If I create a socket using var socket = new UdpClient(0,AddressFamily.InterNetwork); How do I
While working on a larger project, a few lines of code using java.net.Socket failed
i try creating client-server app using socket. i already succeed doing that with 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.