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
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
posted 18 years ago
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