Solved

Sockets and ObjectInputStream

Posted on 2002-05-14
12
246 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 68
noX challenge 17 76
countHi2 challenge 7 44
Java Server Faces parameter pass? 6 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now