Valhalla Legends Forums Archive | Java Programming | Input Stream Help

AuthorMessageTime
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.
November 24, 2003, 12:54 AM
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:
[code]
public void run()
{
String s = in.readLine();
while (s != null) {
System.out.println(s);
s = in.readLine();
}
}
[/code]

For this to work, the Class containing the run() method must extend Thread.
Example:
[code]
public class MyNewThread extends Thread
[/code]

Then, you create a new instance of this Object and tell it to start
Example:
[code]
MyNewThread blah = new MyNewThread();
blah.start();
[/code]

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.
November 24, 2003, 4:48 AM
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.
November 24, 2003, 8:27 PM
DaRk-FeAnOr
Thanks Orillion and Iago, I made a new thread with an infinate loop and it works like a charm :P
November 25, 2003, 4:04 AM
hismajesty
[quote author=DaRk-FeAnOr link=board=34;threadid=3800;start=0#msg31305 date=1069733085]
Thanks Orillion and Iago, I made a new thread with an infinate loop and it works like a charm :P
[/quote]

Iago helped?
November 25, 2003, 12:11 PM
DrivE
[quote author=DaRk-FeAnOr link=board=34;threadid=3800;start=0#msg31305 date=1069733085]
Thanks Orillion and Iago, I made a new thread with an infinate loop and it works like a charm :P
[/quote]

Did you create an infinite loop? Or a while loop or a do-while loop?
November 26, 2003, 2:02 AM
DaRk-FeAnOr
a while, that lasts as long as my socket is connected to battle.net ;)
November 26, 2003, 4:51 PM
UserLoser.
Woudln't a do-while loop be better in this case?
November 27, 2003, 12:37 AM
St0rm.iD
For this, you'll want a while loop in a seperate thread that looks something like this:

[code]
try {
while (String data = in.readLine())
handleData(data);
} catch (IOException e) {
handleException(e);
}
[/code]
November 27, 2003, 2:11 AM
DaRk-FeAnOr
[quote author=St0rm.iD link=board=34;threadid=3800;start=0#msg31900 date=1069899104]
For this, you'll want a while loop in a seperate thread that looks something like this:

[code]
try {
while (String data = in.readLine())
handleData(data);
} catch (IOException e) {
handleException(e);
}
[/code]
[/quote]
That is almost exactly what I did
November 27, 2003, 6:30 AM

Search