Index: [thread] [date] [subject] [author]
  From: Garrett Mitchener <wgm2@duke.edu>
  To  : 
  Date: 22 Apr 1999 20:28:39 -0400

Re: Using ObjectInput/OutputStreams with Sockets...

Timothy Paul Bain <tpb2@duke.edu> writes:

> I'm having some problems getting the ObjectStreams to work with
> Sockets.  Basically, I'm a little unclear as to exactly what we have to
> do in order to be able to read and write Objects across the network...
> 
> Here's the code that I have:
> 
> InputStream myreader = new ObjectInputStream
> (mySocket.getInputStream());
> InOuttStream mywriter = new ObjectOutputStream
> (mySocket.getOutputStream());
> 
> When I do that, I get errors that sayis:
> 
> java.lang.NullPointerException
>         at java.io.ObjectInputStream.read(ObjectInputStream.java)
>         at java.io.DataInputStream.readShort(DataInputStream.java)
>         at java.io.ObjectInputStream.readShort(ObjectInputStream.java)
>         at
> java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java)
>         at java.io.ObjectInputStream.<init>(ObjectInputStream.java)
>         at Host.AddSockets(Host.java:47)
>         at AddSocketsThread.run(AddSocketsThread.java:16)
> 
> Can someone please tell me what I need to do to make this work?  Thanks!
> 
> Tim

My guess is that mySocket.getInputStream() etc. are returning null
because the socket isn't connected to anything.  Check this-- it
should be easy.

As I understand sockets, on the server side, you first have to start a
new thread and a ServerSocket, then that thread calls
aServerSocket.accept().  This causes the thread to stop and wait for
something to try to connect to it.  When something does, the return
value of .accept is a fully connected Socket that should have valid
input and output streams.  Your listening thread should probably
create another new thread to talk to the client at the other end of
this socket, then call aServerSocket.accept() again, etc.

On the client side, you need to create a plain Socket with the host
name and port number of the server program, and if you call
.getInputStream() and .getOutputStream(), it should work.

	Hope this helps,
	-- Garrett :-)




Index: [thread] [date] [subject] [author]