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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:46:19+00:00 2026-05-26T04:46:19+00:00

I have a bunch of DataOutputStream s in Hashtable outputStreams; . There is a

  • 0

I have a bunch of DataOutputStreams in Hashtable outputStreams;. There is a Socket for each of them. I want to loop through them but I want to exclude one particular Socket. I’ve been trying with the following algorithm but it doesn’t seem to work.

for(Enumeration e = outputStreams.elements(); e.hasMoreElements(); ) {

    DataOutputStream dout = (DataOutputStream)e.nextElement();
    OutputStream sdout = null;
    try {
        sdout = socket.getOutputStream();
    } catch (IOException ie) {
        ie.printStackTrace();
    }

    if (dout != sdout) {
        try {
            dout.writeUTF(message);
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
}
  • 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-26T04:46:19+00:00Added an answer on May 26, 2026 at 4:46 am

    It appears that a Hashtable will insert a copy of the instance into its backing structure. Andypandy is right about your problem, but then I wondered if a table of OuputStreams (where you wouldn’t wrap the socket output stream) would fix it and it didn’t.

    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.util.Hashtable;
    
    public class Main
    {
    
        public static void main(String[] args) throws IOException {
    
            Hashtable<Integer, OutputStream> ht = new Hashtable<Integer, OutputStream>();
    
            for(int i = 0; i < 5; i++)
                ht.put( i, new DataOutputStream(new ByteArrayOutputStream()) );
    
            Socket sock = new Socket("google.com", 80);
            ht.put( ht.size(), sock.getOutputStream() );
    
            for(OutputStream dos : ht.values())
                System.out.println(dos);
    
            System.out.println(sock.getOutputStream());
        }
    }
    

    Output:

    java.net.SocketOutputStream@112b853
    java.io.DataOutputStream@36428
    java.io.DataOutputStream@a4b78b
    java.io.DataOutputStream@e3f6d
    java.io.DataOutputStream@1660d22
    java.io.DataOutputStream@e84763
    java.net.SocketOutputStream@1a6c368
    

    The socket’s output stream that is inserted into the table has a different address than that of the original output stream. Since there’s no equals override, there’s no easy way to meaningfully compare streams without having knowledge of their corresponding sockets. You should re-think your design completely, perhaps using the socket descriptor as your key.

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

Sidebar

Related Questions

I have a bunch of classes I want to rename. Some of them have
i have bunch of buttons within linear layout. But there is spaces buttons. I
I have bunch of topics inserted in database, now I want to print them
I have a bunch of png images that I want to feed through ffmpeg
I have bunch of web document and want to remove the html tags from
I want to develop using a framework called Symfony but I do not have
I have bunch of temporal data which I want to convert to RDF format.
I have bunch of hidden columns in my jqGrid, on which I want to
I have bunch of employees, each employee have 7 days for their work schedule.
I have a bunch of modules and for each module I have a unittest

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.