Results 1 to 11 of 11
Thread: C like data structures in java ?
- 11-08-2008, 07:24 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 22
- Rep Power
- 0
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
- 11-08-2008, 07:44 PM #2
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
- 11-08-2008, 08:19 PM #3so that the values get automatically stored in the right fields...
- 11-09-2008, 02:08 AM #4
Member
- Join Date
- Nov 2008
- Posts
- 22
- Rep Power
- 0
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
- 11-09-2008, 03:48 AM #5this does not seem to be working ...should be getting value of 1152(0x0480) .
What class is input?
- 11-09-2008, 04:06 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 22
- Rep Power
- 0
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) ...
ankitLast edited by ankitmcgill; 11-09-2008 at 04:08 AM.
- 11-09-2008, 04:15 AM #7
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?
- 11-09-2008, 05:45 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 22
- Rep Power
- 0
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.
- 11-09-2008, 07:24 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 22
- Rep Power
- 0
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; }
- 11-09-2008, 12:29 PM #10
no unsigned datatype in Java
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
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)); }
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-09-2008, 02:53 PM #11protocol used by the server .. its sending some weird message out of queue .
Similar Threads
-
Data Pipeline 2 - Data Transformation Toolkit for Java Released
By dele in forum Java SoftwareReplies: 0Last Post: 10-31-2008, 03:13 PM -
Some questiong about Data Structures in Java
By Javid in forum New To JavaReplies: 5Last Post: 10-10-2008, 08:33 AM -
Data Sorting in a .data file using java
By stutiger99 in forum New To JavaReplies: 2Last Post: 10-08-2008, 03:52 AM -
Sux data structures 1.0.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-15-2008, 07:54 PM -
Data Structures Help...
By jac0117 in forum New To JavaReplies: 1Last Post: 01-12-2008, 08:31 AM
Bookmarks