Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sockets and ObjectInputStream

Posted on 2002-05-14
12
254 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ForLoop Example 3 52
egit plugin on eclipse 8 82
windows explorer path to command prompt 5 54
table example 4 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

839 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