Results 1 to 17 of 17
Thread: Java Packet sending UDP
- 06-08-2010, 05:02 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
Java Packet sending UDP
Here's the deal.. While practicing in my free time Networking in java
I had an idea of making a RCON manipulator for a Quake 3 server which is written in C and has its very own quake language as well
The packets it receives and sends are encoded in C ANSI
So my questions are
Should i be using Datagram? Been trying with it though not achieving anything practically it wouldn't even compile though i have been going blindly with out really being sure of datagram's usage
In C its like
(using the winapi library)NET_SendPacket( addr, "\xFF\xFF\xFF\xFFrcon pass map blabla" );
"\xFF\xFF\xFF\xFFrcon pass map blabla" is the message that i want to send to the server, quake 3 machine uses UDP protocol
xFF is byte for -1 which is the formula the quake 3 machine based server i have uses, so basically its like saying "-1-1-1-1rcon pass map blabla" but in theory if i do
lets say
String a = "-1";
and try to do the byte decode method to turn -1 to byte it would be 2 bytes..
so basically there should be something equivalent.. I have seen many JAVA programs interfering with ansi packet using programs. I do understand that java uses UTF - 8 by default if not mistaken while i need it encoded in ansi..
Basically forgive me if i made several mistakes in the way i am trying to give you to understand my problem but i am so confused.. I just read so many things so many posts about byte handling and other things that my brain is currently dis organised.. Also please have in mind that i am not any C specialist or Java specialist none the less i do thing that such an issue goes to the java advanced forum.. I am merely using java 1 year now.
- 06-08-2010, 05:41 PM #2
Not sure I understand your questions.
The String "-1" is made up of two bytes.do the byte decode method to turn -1 to byte it would be 2 bytes
The int -1 has 4 bytes equal to xFFFFFFFF
- 06-08-2010, 05:56 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
- 06-08-2010, 06:08 PM #4
Can you state your question again?
- 06-08-2010, 06:18 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
i will and i found a solution for the signed value... Basicaly the quake 3 server that is written in C... the packages it receives must have a specific formula inside them the 4x -1
of course these vary from function to function you want to execute in your server so i just now thought of
since you can't have as 1 byte a "-1" for example value in java what i can do is translate the -1 to binary code (0xFF which is 11111111 in binary) and include it inside the packet's "content"
BUT my major problem is creating the packet and sending it... through datagram or what else.. and when saying creating.. I know what i want the packet to have.. and that is "0xFF" + "0xFF" + "0xFF" + "0xFF" + "rcon password cmd"; appending bytes with the "rcon password cmd" string and that will be the packet
- 06-08-2010, 06:31 PM #6
You're confusing Strings ("-1") with integer -1.you can't have as 1 byte a "-1"
A byte can contain any of 256 different values from x00 to xFF
Confused here about Strings ("0xFF") and binary: 0xFF.want the packet to have.. and that is "0xFF" + "0xFF" + "0xFF" + "0xFF"
I guess you want the first four bytes to 0XFFFFFFFF followed by a String
- 06-08-2010, 06:32 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
exactly
and what i want to say is that the 0xFF must be sent as bytes and not strings though being in the same object appended with a string
EDIT@
someone confused me badly.. Java uses SIGNED bytes so there wouldn't be any problem having -1 as one byte 0xFF
so now lets go the appending byte with string part and the method i should use to send the packet through UDP protocolLast edited by g123456; 06-08-2010 at 06:37 PM.
- 06-08-2010, 06:40 PM #8
DatagramPackets are constructed using byte arrays. So you shouldn't have any problems.
Build an array and fill it up as required.
- 06-08-2010, 06:42 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
yeah i had a problem with it... should i fill a byte array or? and if i am going to fill a byte array or a char array datagram uses bytearray so basically how am i going to add the string too could you possibly give me an example which shows the usage of both string and array appended inside a bytearray or what ever that datagram uses? Because i have failed the last time i tried before some minutes..
Last edited by g123456; 06-08-2010 at 07:13 PM.
- 06-08-2010, 07:19 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
wont even compileJava Code:public class test { static void main(String []args) throws IOException { String source = "rcon pass cmd"; byte[] byteArray = new byte[]{(byte) 0xFF,(byte) 0xFF,(byte) 0xFF,(byte) 0xFF,(byte) source.getBytes("ANSI")}; } }
- 06-08-2010, 07:20 PM #11
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Java Code:byte[] stringBytes = theString.getBytes(); byte[] allBytes = new byte[stringBytes.length + 4]; allBytes[0] = 0xFF; allBytes[1] = 0xFF; allBytes[2] = 0xFF; allBytes[3] = 0xFF; System.arrayCopy(from stringBytes to allBytes, look up the arguments in API docs);
- 06-09-2010, 10:52 AM #12
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
wow thanks couldn't have thought of that alone i guess.. so basically i initialize the datagram and then send the allBytes
this should work but my question is this .. Is the string "theString" supposed to take 1 srcPos ? More than that how can i test the outcome? Is there any way to check if the outcome is the desired one by using system.out.println ? Cause it seems like it wont really work. I have just tried sending the packet to to the server and it wont recognise it..Java Code:String theString = "rcon pass cmd"; byte[] stringBytes = theString.getBytes(); byte[] allBytes = new byte[stringBytes.length + 4]; allBytes[0] = (byte) 0xFF; allBytes[1] = (byte) 0xFF; allBytes[2] = (byte) 0xFF; allBytes[3] = (byte) 0xFF; System.arraycopy(stringBytes, 0, allBytes, 1, 4);Last edited by g123456; 06-09-2010 at 11:12 AM.
- 06-09-2010, 09:08 PM #13
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
okey after doing this -->
the outcome is!Java Code:String theString = "rcon pass map"; byte[] stringBytes = theString.getBytes(); byte[] allBytes = new byte[stringBytes.length + 4]; allBytes[0] = (byte) 0xFF; allBytes[1] = (byte) 0xFF; allBytes[2] = (byte) 0xFF; allBytes[3] = (byte) 0xFF; System.arraycopy(stringBytes, 0, allBytes, 1, 4); DatagramSocket socket = new DatagramSocket(); for(int iByte = 0; iByte < allBytes.length; iByte++) System.out.println((char)allBytes[iByte]); InetAddress address = InetAddress.getByName("192.168.1.5"); DatagramPacket packet = new DatagramPacket(allBytes, allBytes.length, address, 28070); socket.send(packet); for(int iByte = 0; iByte < allBytes.length; iByte++) System.out.println((char)allBytes[iByte]);
while it should beJava Code:? r c o n
-1-1-1-1rcon pass map
- 06-09-2010, 09:59 PM #14
Show the code that is reading and displaying the outcome.the outcome is
I'm assuming that you used arrayCopy() correctly.
I was wrong. Read the API specs for the arrayCopy() method again!Last edited by Norm; 06-09-2010 at 10:33 PM.
- 06-09-2010, 11:07 PM #15
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
yeah i have fixed it a bit after playing around the system.arraycopy parameters.. and the outcome now is
but shouldn't it be -1 instead of ? since in the first 4 places in the array bytes i got 0xFF which is -1Java Code:? ? ? ? r c o n p a s s m a p
- 06-09-2010, 11:37 PM #16
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
you cant print bytes out like that. "-1" byte value doesn't convert to a character nicely, so java substitutes "?".
do this:
for(int iByte = 0; iByte < allBytes.length; iByte++)
System.out.println("int value: " + (int)allBytes[iByte] + "; char value: " + (char)allBytes[iByte]);
- 06-10-2010, 03:38 PM #17
Member
- Join Date
- Dec 2009
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
how to extract wireshark capture packet to my java applet
By firdauskhalid in forum NetworkingReplies: 0Last Post: 03-30-2010, 04:19 AM -
Web services packet limit?
By poet in forum Advanced JavaReplies: 1Last Post: 10-21-2009, 01:57 AM -
How to identify packet type?
By priya deshpande in forum NetworkingReplies: 0Last Post: 10-11-2009, 06:40 AM -
Can't Stop packet from transmitting and unable to understand packet format
By khajalid in forum NetworkingReplies: 2Last Post: 12-22-2008, 03:05 AM -
fake ARP broadcast packet
By junkredish in forum NetworkingReplies: 0Last Post: 09-06-2008, 06:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks