Results 1 to 17 of 17
  1. #1
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Not sure I understand your questions.
    do the byte decode method to turn -1 to byte it would be 2 bytes
    The String "-1" is made up of two bytes.
    The int -1 has 4 bytes equal to xFFFFFFFF

  3. #3
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Not sure I understand your questions.

    The String "-1" is made up of two bytes.
    The int -1 has 4 bytes equal to xFFFFFFFF
    the example with -1 refers to the C equivalent which is a signed byte in range of -127 to 128

    but we used instead unsigned bytes which are 0 to 255

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Can you state your question again?

  5. #5
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    you can't have as 1 byte a "-1"
    You're confusing Strings ("-1") with integer -1.
    A byte can contain any of 256 different values from x00 to xFF

    want the packet to have.. and that is "0xFF" + "0xFF" + "0xFF" + "0xFF"
    Confused here about Strings ("0xFF") and binary: 0xFF.
    I guess you want the first four bytes to 0XFFFFFFFF followed by a String

  7. #7
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    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 protocol
    Last edited by g123456; 06-08-2010 at 06:37 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    DatagramPackets are constructed using byte arrays. So you shouldn't have any problems.
    Build an array and fill it up as required.

  9. #9
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    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.

  10. #10
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    Java 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")};
    
    
       }
    }
    wont even compile

  11. #11
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

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

  12. #12
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by iluxa View Post
    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);

    wow thanks couldn't have thought of that alone i guess.. so basically i initialize the datagram and then send the allBytes












    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);
    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..
    Last edited by g123456; 06-09-2010 at 11:12 AM.

  13. #13
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    okey after doing this -->

    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]);
    the outcome is!

    Java Code:
    ?
    r
    c
    o
    n
    while it should be

    -1-1-1-1rcon pass map

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    the outcome is
    Show the code that is reading and displaying the outcome.
    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.

  15. #15
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

    Default

    yeah i have fixed it a bit after playing around the system.arraycopy parameters.. and the outcome now is

    Java Code:
    ?
    ?
    ?
    ?
    r
    c
    o
    n
    
    p
    a
    s
    s
    
    m
    a
    p
    but shouldn't it be -1 instead of ? since in the first 4 places in the array bytes i got 0xFF which is -1

  16. #16
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

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

  17. #17
    g123456 is offline Member
    Join Date
    Dec 2009
    Posts
    34
    Rep Power
    0

Similar Threads

  1. how to extract wireshark capture packet to my java applet
    By firdauskhalid in forum Networking
    Replies: 0
    Last Post: 03-30-2010, 04:19 AM
  2. Web services packet limit?
    By poet in forum Advanced Java
    Replies: 1
    Last Post: 10-21-2009, 01:57 AM
  3. How to identify packet type?
    By priya deshpande in forum Networking
    Replies: 0
    Last Post: 10-11-2009, 06:40 AM
  4. Replies: 2
    Last Post: 12-22-2008, 03:05 AM
  5. fake ARP broadcast packet
    By junkredish in forum Networking
    Replies: 0
    Last Post: 09-06-2008, 06:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •