Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Object Stream from Socket

 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:
I am try to create an Object stream from a socket connection- I can not. The client and server connect- I can get the Input ( and Output stream) and the BufferedStream but I can not get the ObjectStream- both on the client and the server. No timeouts or exceptions are thrown the code just sits there.
Any thing special needs to be done to get the object stream from the socket?
Thanks a bunch
Sanjay
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjay,
Can u post the code here, So that the bug can be identified clearly.
Arun
SCJP
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
The code looks like this:
try {

System.out.println("SocketClient:looking for Socket connection on Host: "+HOST+" Port: "+PORT);
socket = new Socket(HOST, PORT);
System.out.println("SocketClient:got Socket connection");
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
System.out.println("SocketClient:got IO streams");
BufferedInputStream bis = new BufferedInputStream (is);
BufferedOutputStream bos = new BufferedOutputStream (os);
System.out.println("SocketClient:got Buffered IO streams");
//******* the code stops after the above print stmt: No time out //*******or exception is thrown
ois = new ObjectInputStream ( bis);
oos = new ObjectOutputStream ( bos);
System.out.println("SocketClient:got Object IO streams...");
startListener();
System.out.println("SocketClient:Ready...");
} catch (Exception e) {
System.out.println("SocketClient:initSockets:exception: "+e.getMessage());
e.printStackTrace();
throw new Exception("SocketClient:exception: e.getMessage());
}
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hello, I have a suggestion for you to solve this problem, in case you haven't figured it out since you last posted. The best way to create a dead lock is to try to read from both ends of a stream at the same time. I assume that the code for your server and your client are both attempting to get the ObjectInputStream before getting the ObjectOutputStream. Basically, all you have to do to fix this problem is put the getObjectOutputStream line BEFORE the getObjectInputStream line..



Hope this helps!



RL

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sanjay -
Flushing the object output stream just after construction of the same solves the problem, even if the underlain stream is a buffered stream:
<code snippet>
...
// -----------------------------------------
// Construct the object output stream - i.e.
// get the 'someOutStream' from your socket.
// -----------------------------------------
ObjectOutputStream oos = new ObjectOutputStream(someBufferedOutStream);
// -----------------------------------------
// Flush the stream just after constuction,
// because the stream's header data is to
// be pushed to the waiting corresponding
// input stream ... There seems to be no
// other way to send the stream's header data:
// -----------------------------------------
oos.flush();
...
</code snippet>
Still the order of the streams being created is important -
hope this helps,
Siegfried
 
Tiger Scott
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
Just to complete this saga-
The ObjectInputStream blocks. So if you do not want to block- you can do .available() on the InputStraem/ BuffredInputStream - for some reason- these calls do not block.
Thanks evrybody,
Sanjay
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic