Author | Message | Time |
---|---|---|
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: [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); } } }[/code] | January 16, 2005, 4:18 PM |
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(). | January 16, 2005, 5:06 PM |
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. | January 16, 2005, 5:47 PM |
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: [code]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 } }[/code] 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. | January 16, 2005, 6:42 PM |
iago | [quote author=Kp link=topic=10212.msg95425#msg95425 date=1105900960] 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. [/quote] 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. | January 16, 2005, 7:06 PM |
Kp | [quote author=iago link=topic=10212.msg95426#msg95426 date=1105902409]I have trouble finding where variables come from unless I'm using a bulky IDE.[/quote] 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. | January 16, 2005, 7:27 PM |
R.a.B.B.i.T | [quote author=iago link=topic=10212.msg95418#msg95418 date=1105897657] 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. [/quote]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? | January 16, 2005, 7:45 PM |
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 :/ | January 16, 2005, 8:08 PM |
R.a.B.B.i.T | OK then, thanks. | January 19, 2005, 4:22 AM |
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/ | January 25, 2005, 8:40 PM |
R.a.B.B.i.T | [quote author=CrAzY link=topic=10212.msg96597#msg96597 date=1106685646] 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/ [/quote]I'm not quite THAT far yet...I think I'll stick with predefined types for now. Thanks for the API listing, though. | January 26, 2005, 3:22 AM |
Kp | Minor point, but that's an old version. You can find the v1.4.2 Java API at [url]http://java.sun.com/j2se/1.4/docs/api/[/url] | January 26, 2005, 3:50 PM |
Zakath | [quote author=iago link=topic=10212.msg95426#msg95426 date=1105902409]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.[/quote] *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* | February 3, 2005, 7:05 AM |
Kp | [quote author=Zakath link=topic=10212.msg98029#msg98029 date=1107414312]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] 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). :) | February 3, 2005, 9:40 PM |
iago | [quote author=Kp link=topic=10212.msg96759#msg96759 date=1106754604] Minor point, but that's an old version. You can find the v1.4.2 Java API at [url]http://java.sun.com/j2se/1.4/docs/api/[/url] [/quote] Not to be picky, but that's also an old version. Try: [url]http://java.sun.com/j2se/1.5/docs/api/[/url] :-P | February 4, 2005, 2:05 AM |
Kp | [quote author=iago link=topic=10212.msg98146#msg98146 date=1107482752]Not to be picky, but that's also an old version.[/quote] True, but v1.4 based JVMs / Java compilers are still fairly common (at least in my area). :) So, it seemed appropriate to point him at that one since it'd a good bet that he'll have access to that, whereas 1.5 is (afaik) new enough that not everyone will have it installed yet. | February 4, 2005, 5:44 PM |
iago | [quote author=Kp link=topic=10212.msg98212#msg98212 date=1107539090] True, but v1.4 based JVMs / Java compilers are still fairly common (at least in my area). :) So, it seemed appropriate to point him at that one since it'd a good bet that he'll have access to that, whereas 1.5 is (afaik) new enough that not everyone will have it installed yet. [/quote] Yes, I agree, I still point people at 1.4.2 as well. However, it was worthwhile noting that you were recommending against an old version for that reason, yet still prompted an old version :) | February 7, 2005, 2:18 AM |