Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

trouble with multi-threaded network application

1 view
Skip to first unread message

Jay Eckles

unread,
Oct 18, 2001, 4:05:24 PM10/18/01
to
I've got a pretty simple multi-threaded chat application I'm writing
as an exercise to learn more about the java.net API and
Thread/Runnable. I'm having a bit of trouble, and I'm guessing it's
something simple I'm just not seeing.

One class, JChat, is a server, waiting for a connection on port 5001.
JChatClient is a client, making a connection to port 5001 on host X.
I start JChat on one machine, and it starts and is waiting for a
connection. I start JChatClient on another machine with the address
of the machine running the server.

When the connection is made, both the client and the server start a
new thread for an instance of a class "ConnectionHandler" (code
included below). ConnectionHandler, in turn, createst two threads
running two instances of a class called InOut (code included below).
InOut's constructor takes an InputStream and an OutputStream, then in
an infinite loop reads a byte array from the InputStream and writes it
to the OutputStream. When ConnectionHandler creates the two instances
of InOut, one is given System.in and the socket's OutputStream; the
other is given the socket's InputStream and System.out. So
presumably, when the user of the client types in a line, it is written
to the output stream and goes to the server, where it is written to
standard out (and vice versa).

The problem is that immediately after the connection between the
client and server is made, the ConnectionHandler threads are created
fine on both machines, and when the InOut threads on both machines are
started, both get the following exceptions:
java.lang.NullPointerException
at java.io.FilterInputStream.read(Unknown Source)
at InOut.run(InOut.java:17)
at java.lang.Thread.run(Unknown Source)

I've posted source below, and I'm afraid it's too much, but I don't
know how to isolate the problem (if I did, I think I could fix it).
If you need the code for the client or server, they're both just an
additional 17 lines each, so let me know and I'll post those as well.

If I should narrow my question, guide me in a direction and I'll do
the best I can. I sincerely appreciate your willingness to read
through all this and help.

Jay Eckles
www.jayeckles.com

----lines from JChat.java that creates ConnectionHandler thread----
ServerSocket ss = new ServerSocket( 5001 ) ;
ConnectionHandler ch = new ConnectionHandler( ss.accept() ) ;
System.out.println( "Got a connection; starting
ConnectionHandler thread.\n" ) ;
Thread t = new Thread( ch ) ;
t.start() ;

----ConnectionHandler.java----
import java.net.* ;
import java.io.* ;

public class ConnectionHandler implements Runnable{
Socket s ;

public ConnectionHandler( Socket sock ){
this.s = sock ;
}
public void run(){
try{
System.out.println( "ConnectionHandler started. Getting
in/out streams.\n" ) ;
InputStream in = s.getInputStream() ;
OutputStream out = s.getOutputStream() ;
System.out.println( "Got streams, starting two InOut
threads.\n" ) ;
Thread t = new Thread( new InOut( System.in, out ) ) ;
Thread u = new Thread( new InOut( in, System.out ) ) ;
t.start() ;
u.start() ;
}
catch( Exception e ){
e.printStackTrace() ;
}
}
}

----InOut.java----
import java.io.* ;

public class InOut implements Runnable{
byte[] b = null ;
InputStream in ;
OutputStream out ;

public InOut( InputStream sin, OutputStream sout ){
this.in = sin ;
this.out = sout ;
}
public void run(){
System.out.println( "InOut started.\n" ) ;
boolean go = true ;
while( go ){
try{
in.read( b ) ;
out.write( b ) ;
}
catch( Exception e ){
e.printStackTrace() ;
go = false ;
}
}
}
}

Gordon Beaton

unread,
Oct 18, 2001, 4:22:08 PM10/18/01
to
On 18 Oct 2001 13:05:24 -0700, Jay Eckles wrote:
> java.lang.NullPointerException
[...]
> byte[] b = null ;
[...]
> in.read( b ) ;

Where should read() put any data it reads?

Also, there is no need to create 3 threads - ConnectionHandler itself
doesn't need to run in a separate thread if all it does in its
lifetime is create two other threads. Make it an ordinary class, and
let it create the two InOut objects in its constructor instead.

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

Paul Keeble

unread,
Oct 18, 2001, 4:31:43 PM10/18/01
to

*SNIP*

> ----InOut.java----
> import java.io.* ;
>
> public class InOut implements Runnable{
> byte[] b = null ;
> InputStream in ;
> OutputStream out ;
>
> public InOut( InputStream sin, OutputStream sout ){
> this.in = sin ;
> this.out = sout ;
> }
> public void run(){
> System.out.println( "InOut started.\n" ) ;
> boolean go = true ;
> while( go ){
> try{
> in.read( b ) ;
> out.write( b ) ;
> }
> catch( Exception e ){
> e.printStackTrace() ;
> go = false ;
> }
> }
> }
> }

b above is null....I see no code that constructs it, thus your passing a null
point on the line that reads:-

in.read( b );

regards

P Keeble


0 new messages