• Welcome to Valhalla Legends Archive.
 

Input Stream Help

Started by Dark-Feanor, November 23, 2003, 06:54 PM

Previous topic - Next topic

Dark-Feanor

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.
- Feanor[xL]
clan exile
Firebot
iago: "caps lock is like cruise control for cool"

Orillion

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.

Kp

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Dark-Feanor

Thanks Orillion and iago, I made a new thread with an infinate loop and it works like a charm  :P
- Feanor[xL]
clan exile
Firebot
iago: "caps lock is like cruise control for cool"

hismajesty

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?

Hazard

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?

"Courage is being scared to death - but saddling up anyway." --John Wayne

Dark-Feanor

a while, that lasts as long as my socket is connected to battle.net  ;)
- Feanor[xL]
clan exile
Firebot
iago: "caps lock is like cruise control for cool"

UserLoser.

Woudln't a do-while loop be better in this case?

Banana fanna fo fanna

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);
}

Dark-Feanor

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
- Feanor[xL]
clan exile
Firebot
iago: "caps lock is like cruise control for cool"