Hello I am working with sockets in Java and I have run into a problem. I have created a DataInputStream and PrintStream in my client side and I'm attempting to create and ObjectOutputStream and ObjectInputStream for the same socket (the PrintStream is for text the ObjectOutputStream and ObjectInputStream are for pieces that will be used in a game I'm making). The program makes an ObjectOutputStream and the Server says a connection was made, however, the client hangs when it gets to ObjectInputStream.Is there anyway I can stop the hanging (it is possible to have multiple input and output streams to the same socket).

Socket sock = new Socket("127.0.0.1", 1716);
           InputStream majora = sock.getInputStream();
           OutputStream mask = sock.getOutputStream();

           input = new DataInputStream(majora);
           printer = new PrintStream(mask);

           System.out.println("Test 1");
           link = new ObjectOutputStream(mask);
           link.flush();
           System.out.println("Test 2");
           zelda = new ObjectInputStream(majora);
           System.out.println("Test 3");

Recommended Answers

All 6 Replies

I don't know if you can have two classes using the same InputStream object.
I've never tried it so it could work.
How would the two different classes not get in each others way? What if one class reads a full buffer of data, but only processes part of it. What would happen if the other class then tried to read from that inputstream?

the client hangs when it gets to ObjectInputStream

Is this the last print you see? Test 2

Yes the last print I see is test 2. I thought of a workaround, but I was wondering if it was possible to have two serversockets on different ports running at the same time.

I was wondering if it was possible to have two serversockets on different ports running at the same time.

I think it should work. Have you tried it?

I tried it and it never worked so I just switched it too DataInputStream and modified accordingly.

it never worked

Is the code working now?

Yeah, it works now. I just have no idea why ObjectInputStream didn't want to run.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.