Valhalla Legends Archive

Programming => General Programming => Java Programming => Topic started by: Dark-Feanor on November 23, 2003, 06:54 PM

Title: Input Stream Help
Post by: Dark-Feanor on November 23, 2003, 06:54 PM
I am trying to program a bot in java and I got it to connect.  My problem is that everytime I had sent some data, I was immediately reading from the input stream to see what b.net responds with. Once the connection is initialized, I do not know when b.net will be sending me packets, hence I do not know how to be listening for 0x0F chat event packets all the time. Does anybody have any ideas on how to do this? Thank you for the help.
Title: Re:Input Stream Help
Post by: Orillion on November 23, 2003, 10:48 PM
One way to do this is to create a method which continues to read data in from your battle.net connection, whilst allowing the rest of your program to continue. Essentially this is multithreading. In java multithreading is quite easy. One way of doing this is having the code you wish to run in a method called run()
Example:

public void run()
{
   String s = in.readLine();
   while (s != null) {
        System.out.println(s);
        s = in.readLine();
   }
}


For this to work, the Class containing the run() method must extend Thread.
Example:

public class MyNewThread extends Thread


Then, you create a new instance of this Object and tell it to start
Example:

MyNewThread blah = new MyNewThread();
blah.start();


This will mean that your run() method will continue to recieve data as it comes in. But do note, this is only 1 way of many of doing this. Its particularly useful if your wanting a GUI.
Title: Minor comment
Post by: Kp on November 24, 2003, 02:27 PM
While multi-threading your input is a very easy way to do it, it is rarely the most efficient in my experience.  You're much better off doing multiplexed input via select (*nix) or overlapped transfers (Win32).  Even taking into account Java's general inability to use efficient calls, I'm pretty sure there's a better way of doing this. :)  Check out the NIO package - I've heard some good (but vague) things about it.
Title: Re:Input Stream Help
Post by: Dark-Feanor on November 24, 2003, 10:04 PM
Thanks Orillion and iago, I made a new thread with an infinate loop and it works like a charm  :P
Title: Re:Input Stream Help
Post by: hismajesty on November 25, 2003, 06:11 AM
Quote from: DaRk-FeAnOr on November 24, 2003, 10:04 PM
Thanks Orillion and iago, I made a new thread with an infinate loop and it works like a charm  :P

iago helped?
Title: Re:Input Stream Help
Post by: Hazard on November 25, 2003, 08:02 PM
Quote from: DaRk-FeAnOr on November 24, 2003, 10:04 PM
Thanks Orillion and iago, I made a new thread with an infinate loop and it works like a charm  :P

Did you create an infinite loop? Or a while loop or a do-while loop?
Title: Re:Input Stream Help
Post by: Dark-Feanor on November 26, 2003, 10:51 AM
a while, that lasts as long as my socket is connected to battle.net  ;)
Title: Re:Input Stream Help
Post by: UserLoser. on November 26, 2003, 06:37 PM
Woudln't a do-while loop be better in this case?
Title: Re:Input Stream Help
Post by: Banana fanna fo fanna on November 26, 2003, 08:11 PM
For this, you'll want a while loop in a seperate thread that looks something like this:


try {
   while (String data = in.readLine())
       handleData(data);
} catch (IOException e) {
   handleException(e);
}
Title: Re:Input Stream Help
Post by: Dark-Feanor on November 27, 2003, 12:30 AM
Quote from: St0rm.iD on November 26, 2003, 08:11 PM
For this, you'll want a while loop in a seperate thread that looks something like this:


try {
   while (String data = in.readLine())
       handleData(data);
} catch (IOException e) {
   handleException(e);
}

That is almost exactly what I did