• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Serialization over a client-server network problem

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am writing a client-server program and want to send objects to and fro between the client and server. I wrote a test program and tested it for sending string messages. I got the InputStream and the OutputStream from the socket.getInputStream() and socket.getOutputStream() methods. I then used the InputStream and the OutputStream to construct a BufferedReader and a PrintWriter objects for both the server and client. This test program worked well. Next I tried using the ObjectInputStream and ObjectOutputStream classes over the input and output streams to create the corresponding objects. I did this so that I could send serialized objects over the network. But the construction of the ObjectInputStream and ObjectOutputStream failed. Both the server and client hangs up at the line where I try to construct these objects. I know this because I put in print statments before and after the construction statements and it never gets past the construction statement. Is it in some way that a InputStream from a socket cannot be used as an argument for a ObjectInputStream class constructor?
I have given some bits of code from the program.
socket = new Socket(addr, 8080);
incoming = socket.getInputStream();
outgoing = socket.getOutputStream();
System.out.println("got hold of the socket streams");
objInput = new ObjectInputStream(incoming);
objOutput = new ObjectOutputStream(outgoing);
System.out.println("Object streams have been constructed");
As I said before, the program hangs up on line number 5 as I never get the print message on line number 7. If I cannot do what I am doing, what else could I do to send objects across a network? Thanks in advance for any help you can provide me with this problem.
K.N. Swamy
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narayana;
Looks like the problem is the order in which you create the object streams. The constructor for <code>ObjectInputStream</code> tries to read the header data and blocks until the corresponding <code>ObjectOutputStream</code> has written it, which it does at construction. You'll probably need to flush the output stream immediately after construction.
jply
 
Narayana Swamy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for your help. Just switching the order of constructing the streams - first the objectOutputstream and second the objectInputstream made it work. I could never have figured it out without your reply. I referred to 3 books before I posted my message on the Moose saloon and the books never mentioned about the objectInputStream getting blocked until the corresponding objectOutputStream has written the header data. In fact in one of the books, a sample code constructs the objectInputStream first.
K.N. Swamy

Originally posted by Jerry Pulley:
Narayana;
Looks like the problem is the order in which you create the object streams. The constructor for <code>ObjectInputStream</code> tries to read the header data and blocks until the corresponding <code>ObjectOutputStream</code> has written it, which it does at construction. You'll probably need to flush the output stream immediately after construction.
jply


 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!