Solved

Sockets and ObjectInputStream

Posted on 2002-05-14
12
252 Views
Last Modified: 2010-08-05
Hey experts, my question is pretty simple.
All im trying to do is send object over a socket i got from ServerSocket.

this is the code::

socketData[su].socket = server.accept();            
socketData[su].objIn = new ObjectInputStream(  socketData[su].socket.getInputStream()  );
socketData[su].objOut = new ObjectOutputStream( socketData[su].socket.getOutputStream() );
           
socketData[su].dataOut = new DataOutputStream( socketData[su].socket.getOutputStream() );
socketData[su].dataIn = new DataInputStream( socketData[su].socket.getInputStream() );

but i always get an error.. or the aplication just hangs.
Oh by the way, im using a JFrame.
thanks for your help!
0
Comment
Question by:GoldStone32767
  • 7
  • 3
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7009924
You can't create an Object and Data stream using the same socket. Just get rid of the last two lines.
0
 

Expert Comment

by:Molchi
ID: 8526520
I experience the same with my application!

try {
      socket = new Socket(host, port);
      //InputStream i = socket.getInputStream();  //this works
      //OutputStream o = socket.getOutputStream(); //this as well
      in = new ObjectInputStream(socket.getInputStream()); //in & out declared somewhere else, this line hangs the process
      out = new ObjectOutputStream(socket.getOutputStream());
    }catch(IOException e) {
      System.out.println("error: " + e);
    }

I'm really frustrated, hope someone has a solution...
0
 

Expert Comment

by:Molchi
ID: 8526766
I experience the same with my application!

try {
      socket = new Socket(host, port);
      //InputStream i = socket.getInputStream();  //this works
      //OutputStream o = socket.getOutputStream(); //this as well
      in = new ObjectInputStream(socket.getInputStream()); //in & out declared somewhere else, this line hangs the process
      out = new ObjectOutputStream(socket.getOutputStream());
    }catch(IOException e) {
      System.out.println("error: " + e);
    }

I'm really frustrated, hope someone has a solution...
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Expert Comment

by:Molchi
ID: 8527271
Now i know what the problem is!
I propose this answer:

Just remove either the Data-Streams or the Object-Streams, you have to choose one of them...
Ok, then the most important thing to do is to swap the 2 lines where you create the streams.
In my case this would be from this:

in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());

to this:

out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
0
 

Expert Comment

by:Molchi
ID: 8527427
I wanted to add some comments:

After I swapped these two lines in my clien-source code, I thought it would be a good idea to do the same with my server-source code. When I changed the sourcecode of the server the initialization of the streams didn't work correctly but when I redid this change it worked fine again.
Just try and see what's better for you and let me hear from you :)
0
 
LVL 92

Expert Comment

by:objects
ID: 8528029
> I propose this answer:
> Just remove either the Data-Streams or the Object-
> Streams, you have to choose one of them...

Sounds basically the same thing as I originally stated.

0
 

Expert Comment

by:Molchi
ID: 8534133
Yes, I'm sorry if I repeated you with other words, but the fact remains that he didn't accept your answer, so I inferred from this that he still had problems. I had similar problems if not exactly the same.
In my case the problem was solved when the output-stream was initialized first, I thought this could help him.
I'm really surprised that the process hang-up was caused by the initialization of the inputstream at the beginning.
Why is the order of the initialization so important?
Do you have an explanation?

In addition I want to say that I still experience problems with my client program. The first time when executed it connects correctly to the server but when I click on the disconnect button and try to connect again the process just hangs.
0
 

Expert Comment

by:Molchi
ID: 8535979
Ok I just solved my problem on my own again, it was an issue with the threads...
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 8537098
> but the fact remains that he didn't accept your answer,
> so I inferred from this that he still had problems.

Not always a valid assumption.
And if he still had problems, then why re-state the previous comment :)

> Why is the order of the initialization so important?

The question did say anything about order.

> Do you have an explanation?

Yes, you can't run an object and data stream over the same stream at the same time.


0
 

Expert Comment

by:Molchi
ID: 8543394
>Not always a valid assumption.

Ok that's true somehow...

>And if he still had problems, then why re-state the previous comment :)

Well, but my proposed answer didn't consist of the similar reproduction of your statement only. I said that most importantly one should initialize the OutputStream first, because otherwise the process would hang.
So if your suggestion helped him right away then you should get the points, but if not then I'm sure you wouldn't mind if I get the points, provided that I could help him.

>The question did say anything about order.

(did you mean "didn't say" ?)
That's true, but apart from the main problem that one cannot run two different streams over the same socket, there was the problem that the process got stuck.
He said: "but i always get an error.. or the aplication just hangs."
This was exactly the same problem I experienced. The solution was to initalize the outputstream first.
Please try to write a small server-client program with objectstreams and initialize the inputstreams first. You will see the program will hang...

>Yes, you can't run an object and data stream over the same stream at the same time.

Yes I do know that, but you misunderstood me. I hope everything got clear with the text above.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8749627
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to objects@idg

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question