Results 1 to 11 of 11
  1. #1
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default C like data structures in java ?

    How do i read data from a socket and store it in some form of structure ?

    for example: lets say the server A sends out in order

    1. ID (int - 2 bytes)
    2. Message corresponding to above ID (string - 12 bytes)

    so the total size is 14 bytes.

    So far from what i know i would have to do

    read(2);
    read(12) on the inputdatastream and store them in appropriate data types (int, byte[])

    if i need to read this packet many times .. is there any way to make this easier ? like read the 14 bytes at once (into some custom object of my own - containing 2 data members) so that the values get automatically stored in the right fields...

    something like

    struct a
    {
    int id;
    byte m=new byte[12]
    }

    socket_read(a);

    print a.id;
    print a.m;

    thanks
    -a

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    struct in Java is class very similar.

    Socket is even a class, read docs on it.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    so that the values get automatically stored in the right fields...
    No way to redefine one long field with more than one short fields in java. You'll have to read the fields one by one and store their values where they go.

  4. #4
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    ok ... i started working on it and ran into some problems ...

    the part that seems to be screwing up :

    private int[] readSocket16(int length) throws IOException
    {
    int[] f=new int[length];
    short[] t=new short[length];
    for(int i=0;i<length;i++)
    {

    t[i]=input.readShort();
    f[i]=(int)(t[i]&0xffff);
    System.out.println(Integer.toHexString(f[i]));
    }

    }

    what im doing here is reading a short(16 bits signed) and then storing it in an int to get unsigned effect. this does not seem to be working as when i do readSocket16(1) i should be getting value of 1152(0x0480) .. found this info by looking at the captured packet.

    any ideas why its not working ?

    -ankit

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    this does not seem to be working ...should be getting value of 1152(0x0480) .
    What value are you getting?
    What class is input?

  6. #6
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    im getting 0 ... output from the program

    "Framebuffer width,height = 0,4"

    the value after the "," is a similar call for another unsigned 16bit after the first one which should give 864(0x0360) ...

    ankit
    Last edited by ankitmcgill; 11-09-2008 at 03:08 AM.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I do not understand your last post at all.

    You show a String (in "s). How does that relate to the write statement that is sending the data that is read by the readShort method?
    The value after the , looks like the string "height". How does that become x0360?

  8. #8
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    my bad .. let me explain it properly.

    the actual call to print the output is :

    System.out.println("Framebuffer width,height="+input.readSocket16(1)[0]+","+input.readSocket16(1)[0]);

    the two calls to the function readSocket16(...) should return values 1152 and 864 but instead produce 0 and 4 respectively.


    I checked the actual packet sent by the server using wireshark so i know the server is sending the right data ... its my program is srewing it up.

  9. #9
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    never mind ... took a break from the computer .. the mind was getting clogged up

    replaced with a simpler function :

    Java Code:
    private int[] readSocket16(int length) throws IOException
        {
            int[] f=new int[length];
            char[] t=new char[length];
            for(int i=0;i<length;i++)
            {
                t[i]=input.readChar();
                System.out.println(t[i]);
                f[i]=t[i];
                System.out.println(f[i]);
            }
            return f;
        }
    as for the weird output turns out there are some gaps in the protocol used by the server .. its sending some weird message out of queue ... cant explain it but its not my code for sure :D

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation no unsigned datatype in Java

    Quote Originally Posted by ankitmcgill View Post
    the value after the "," is a similar call for another unsigned 16bit after the first one which should give 864
    There are no unsigned anything in Java that I know of, nor void pointers - typing is just about everywhere. To see how Java does it ( extracting bits ) I suggest reading
    Java Code:
    ${SOURCE_DIRECTORY}java\io\DataInputStream.java
    for example:
    Java Code:
        public final short readShort() throws IOException {
            int ch1 = in.read();
            int ch2 = in.read();
            if ((ch1 | ch2) < 0)
                throw new EOFException();
            return (short)((ch1 << 8) + (ch2 << 0));
        }
    Note reading into an int and testing for EOF (-1), then extracting the short by casting. This is the way to Boogie with the Bean.
    Quote Originally Posted by ankitmcgill View Post
    as for the weird output turns out there are some gaps in the protocol used by the server .. its sending some weird message out of queue
    ?
    Quote Originally Posted by ankitmcgill View Post
    ... cant explain it but its not my code for sure.
    Write your own code?... that often seems to be the simplest way in Java.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    protocol used by the server .. its sending some weird message out of queue .
    How is the server formatting and sending what you expect to read as 1152 and 864? That will determine what you have to do to read it. What if it is sending characters (4 and then 3)? vs short (2 bytes) vs int (4 bytes) The receiving code has to know.

Similar Threads

  1. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  2. Some questiong about Data Structures in Java
    By Javid in forum New To Java
    Replies: 5
    Last Post: 10-10-2008, 07:33 AM
  3. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM
  4. Sux data structures 1.0.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-15-2008, 06:54 PM
  5. Data Structures Help...
    By jac0117 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:31 AM

Posting Permissions

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