[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Spawning a new thread



Oops, I just noticed what you were talking about, Gary.  I have three reads in there instead of two.  You are correct.  I need to get rid of the first read.

 

Greg

-----Original Message-----
From:
Gregory A. Lusk
Sent
: Thursday, December 19, 2002 11:53 AM
To: ajug-members@www.ajug.org
Subject: RE: Spawning a new thread

 

Thanks Gary, that’s a good point.  The first read is there because the third byte of data in the stream contains a length field that defines how long the entire packet is before a new one starts.  Hence the second read is designed to get the entire packet into a byte array and ship it off to the RFC class.  So I have changed the first read to:

 

try

                                {

                                                di.read(header,0,3);

                                                li = header(2);

                                }

                                catch (java.io.IOException exc)

                                {

 

                                }

 

Hopefully, this will work.

 

Greg Lusk

 

-----Original Message-----
From: Gary Herndon [mailto:gary.herndon@air2web.com]
Sent: Thursday, December 19, 2002 11:44 AM
To: Gregory A. Lusk
Cc: 'ajug-members@www.ajug.org'
Subject: RE: Spawning a new thread

 

Well, the other thing I notice is that the initial .read() will end up throwing away the first byte of the stream... so if you are only putting 3 bytes on the socket, the initial read() will take the first one.  I would get rid of the first read, and loop over the di.read until 3 bytes were read, then bubble up the data to the RFC class.

 

Gary

-----Original Message-----
From: Gregory A. Lusk [mailto:glusk@mesasub.com]
Sent: Thursday, December 19, 2002 11:37 AM
To: Gary Herndon
Subject: RE: Spawning a new thread

Thanks guys.  I missed the upper-case thingy on the run() method. I fixed that, though, and I am still not executing any of the code below the read() method on _Stream.  My understanding is that code execution should block until data is available on the stream and then execute the remainder of the code when there is data to read.  What am I missing?

Greg Lusk

-----Original Message-----
From: Gary Herndon [mailto:gary.herndon@air2web.com]
Sent: Thursday, December 19, 2002 11:26 AM
To: Gregory A. Lusk
Subject: RE: Spawning a new thread

 

the Run() method should be run() with a lowercare "R".

 

Gary

-----Original Message-----
From: Gregory A. Lusk [mailto:glusk@mesasub.com]
Sent: Thursday, December 19, 2002 11:13 AM
To: ajug-members@www.ajug.org
Subject: Spawning a new thread

I am developing a component that establishes a connection to another network host using a Socket class.  In my component, I want to spawn another thread that will block on the InputStream.read() method until such time as data is available on the stream.  Then, it should call back to the class that spawned the thread, passing the data that was read from the stream.

 

Here is a code snippet that represents what I have so far:

 

CODE FROM THE PARENT CLASS THAT SPAWNS THE NEW THREAD (try/catch statements to be added later):

public void Connect() throws java.io.IOException

                {

                                _Socket = new Socket(_RemoteHostIPAddress,102);

                                _InStream = _Socket.getInputStream();

                                _OutStream = _Socket.getOutputStream();

                                _ListenThread = new ListenerThread(_InStream,this);

                                _ListenThread.setDaemon(true);                   

                                _ListenThread.start();

                  

}

 

CODE FROM THE THREAD CLASS

public class ListenerThread extends java.lang.Thread

{

                protected InputStream _Stream;

                protected RFC1006 _RFC1006;

 

                public ListenerThread(InputStream Stream, RFC1006 obj)

                {

                                super();

                                _Stream = Stream;

                                _RFC1006 = obj;

                }

 

                public void Run() throws java.io.IOException

                {

                                while (true)                                           

                                {

                                                _Stream.read();                                                  

 

                                                //data is available                                               

byte[]  header = new byte[3];          

int li;

                                                DataInputStream di = new DataInputStream(_Stream);

                                                li = di.read(header,0,3);                                     

byte[]  TPDU = new byte[li];   

                                                di.read(TPDU,0,li);                             

                                                _RFC1006.DataArrived(TPDU);

                                }              

}

}

 

The problem that I am having is that the code in the While block is never being executed.  I have written a small test app that sends data back across the socket connection, but nothing is returned to the RFC1006 class.  Can anyone give me any suggestions?

 

Thanks

 

Greg Lusk