• Welcome to Valhalla Legends Archive.
 

My pathetic attempt

Started by R.a.B.B.i.T, January 16, 2005, 10:18 AM

Previous topic - Next topic

R.a.B.B.i.T

...at a bot in Java.  I figured that since this is more about HOW to send as opposed to WHAT to send, it belonged here.  So far I've to it so, well, mostly send 0x50, but I'm not sure if I'm doing it right.  I'm using typecasting because I fairly sure it acts the same way in Java as it does in C++, but I get no response.  I'm also not sure if I'm using the right thing to send packets, DataOutputStream, but that's what was in the tutorials.  Also, I'm not sure if I'm recieving correctly.  Anyway, my code:
import java.io.*;
import java.net.Socket;

public class main
{
private static Socket sock;
private static DataInputStream input;
private static DataOutputStream out;

public static void main(String [] args) throws IOException
{
try
{
sock = new Socket("useast.battle.net", 6112);
}
catch (IOException e)
{
System.out.println(e);
KillSock();
}

System.out.println("Socket created.");

try
{
input = new DataInputStream(sock.getInputStream());
}
catch (IOException e)
{
   System.out.println(e);
   KillSock();
}

System.out.println("Input stream created.");

try
{
out = new DataOutputStream(sock.getOutputStream());
}
catch (IOException e)
{
System.out.println(e);
KillSock();
}

System.out.println("Output stream created.");

if(sock != null && input != null && out != null)
{
try
{
out.write((char) 0x01);
out.write((char) 0xFF);
out.write((char) 0x50);
out.write((char) 0x00);
out.write((char) 0x3A);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.writeChars("68XIRATS");
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0xC9);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.write((char) 0x00);
out.writeChars("USA");
out.write((char) 0x00);
out.writeChars("United States");
out.write((char) 0x00);
System.out.println("0x50 sent");
String response;
while(true)
{
response = input.readLine();
if(response != null)
{
System.out.println(response);
response = null;
KillSock();
break;
}
}
}
catch (IOException e)
{
System.out.println(e);
KillSock();
}
}

System.out.println("Chr(1) sent.");
}

private static void KillSock()
{
try
{
sock.close();
input.close();
out.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}

The-FooL

There are many types of Java Streams.  I don't know the details about each, but some perform buffering and different types of character coding that can impact a binary connection.  Generic OutputStream and InputStream worked for me. 

Also, try reading each character individually using .read().

iago

A "char" isn't one byte -- it's 2 bytes.  That might be a problem.

Also, I didn't really look, but make sure you call out.flush() if you're using a Buffered stream.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Kp

Since your reaction to any IOException seems to be printing the error and disconnecting, why not move everything with a common catch into a single try {} block?  Also, KillSock doesn't exit the program, so it'll continue after suffering an error, and probably produce bizarre results.  I'd suggest something like:

class Connection {
    Socket sock;
    // declare in, out as whatever stream types you need
    Connection() throws IOException {
        sock = new Socket("useast.battle.net", 6112);
        // initialize in/out here too, don't bother catching any IOExceptions - let them propagate
    }
}


Then in main(), use a try {} catch (IOException) block around the new Connection, and give up if an IOE occurs.  It'll make the code more compact, and modularize your I/O streams a bit too.

Also, as a more superficial style note, some people advocate not using the globbing functionality of import.  That is, name explicitly each class you want from java.io.  That only affects compile-time requirements (and then barely noticeably for small projects), but it can be nice later when you're trying to figure which package some class comes from.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

iago

Quote from: Kp on January 16, 2005, 12:42 PM
Also, as a more superficial style note, some people advocate not using the globbing functionality of import.  That is, name explicitly each class you want from java.io.  That only affects compile-time requirements (and then barely noticeably for small projects), but it can be nice later when you're trying to figure which package some class comes from.

I completely agree with everything Kp says, particularly that.  Not for speed or compile times, but for finding where classes comes from.  That's one of the major problems I have with C, I have trouble finding where variables come from unless I'm using a bulky IDE.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Kp

Quote from: iago on January 16, 2005, 01:06 PMI have trouble finding where variables come from unless I'm using a bulky IDE.

Exuberant Ctags <http://ctags.sourceforge.net> can be nice for tracking down functions and global-scope variables, though it may not help you much if you're trying to navigate a function with lots of local variables.  Of course, a tags file isn't much use without a tags-aware editor, such as vim <http://www.vim.org/>. :)  This response is geared toward other readers who have similar problems - I'm pretty sure iago already uses some sort of vi.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

R.a.B.B.i.T

#6
Quote from: iago on January 16, 2005, 11:47 AM
A "char" isn't one byte -- it's 2 bytes.  That might be a problem.

Also, I didn't really look, but make sure you call out.flush() if you're using a Buffered stream.
Ugh, so then char is unicode format?  That would cause problems.

Thnks Kp & iago, I'll try that, but I am still relatively new to Java, so I will probably be back.

Next, is DataOutputStream buffered or not?  The tutorial didn't say :'(

[edit]
I don't know what you mean by "KillSock doesn't exit the program."  Do I just use exit() like in PHP, or do I have to do something else?

iago

You can use System.exit(int code);
where code 0 generally indicates success and anything else indicates failure.

DataOutputStream isn't buffered.  I personally used a OutputStream for my bot, since what I'm doing always boils down to a stream of bytes.  I made a buffer similar to the standard Battle.net buffers people use, which, when asked, produces the stream of bytes. and I just use the OutputStream.write(byte []bytes) function.

Incidentally, you want to use "byte" instead of "char".  A byte is guarenteed to be 1 byte.  The same actually goes for any language, assuming a "char" is 1 byte is probably a bad idea, although I know that it's always done :/
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


R.a.B.B.i.T


CrAzY

Try making your own data type(s) using a recourse class.  It also may be helpful to read the Java API.  http://java.sun.com/j2se/1.3/docs/api/
CrAzY

R.a.B.B.i.T

Quote from: CrAzY on January 25, 2005, 02:40 PM
Try making your own data type(s) using a recourse class.  It also may be helpful to read the Java API.  http://java.sun.com/j2se/1.3/docs/api/
I'm not quite THAT far yet...I think I'll stick with predefined types for now.  Thanks for the API listing, though.

Kp

Minor point, but that's an old version.  You can find the v1.4.2 Java API at http://java.sun.com/j2se/1.4/docs/api/
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Zakath

Quote from: iago on January 16, 2005, 01:06 PMI completely agree with everything Kp says, particularly that.  Not for speed or compile times, but for finding where classes comes from.  That's one of the major problems I have with C, I have trouble finding where variables come from unless I'm using a bulky IDE.

*rolls eyes*

This is why I find it silly to write code in a limited environment when you've got a nice IDE handy.

*right click->go to declaration*
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

Kp

Quote from: Zakath on February 03, 2005, 01:05 AMThis is why I find it silly to write code in a limited environment when you've got a nice IDE handy.*right click->go to declaration*

While I'll agree that could be nice, I offer two suggestions:
1) don't require the mouse (use ^] instead!)
2) Why write in a limited IDE when you've got a fast and powerful editor?  Most IDEs I've seen (and all the ones I've used) are extremely limited in their ability to perform code edits quickly and concisely, but most Unix text editors don't survive release if they can't do that (ignoring such bastardizations as Kate, which tries to cater to Windows transplants who can't work a real editor). :)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

iago

Quote from: Kp on January 26, 2005, 09:50 AM
Minor point, but that's an old version.  You can find the v1.4.2 Java API at http://java.sun.com/j2se/1.4/docs/api/

Not to be picky, but that's also an old version.  Try:
http://java.sun.com/j2se/1.5/docs/api/
:-P
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*