Results 1 to 11 of 11
Thread: C like data structures in java ?
- 11-08-2008, 06: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, 06: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, 07:19 PM #3
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.so that the values get automatically stored in the right fields...
- 11-09-2008, 01: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, 02:48 AM #5
What value are you getting?this does not seem to be working ...should be getting value of 1152(0x0480) .
What class is input?
- 11-09-2008, 03: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 03:08 AM.
- 11-09-2008, 03: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, 04: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, 06: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 :
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 :DJava 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, 11:29 AM #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
for example:Java Code:${SOURCE_DIRECTORY}java\io\DataInputStream.javaNote reading into an int and testing for EOF (-1), then extracting the short by casting. This is the way to Boogie with the Bean.?Write your own code?... that often seems to be the simplest way in 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, 01:53 PM #11
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.protocol 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, 02:13 PM -
Some questiong about Data Structures in Java
By Javid in forum New To JavaReplies: 5Last Post: 10-10-2008, 07:33 AM -
Data Sorting in a .data file using java
By stutiger99 in forum New To JavaReplies: 2Last Post: 10-08-2008, 02:52 AM -
Sux data structures 1.0.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-15-2008, 06:54 PM -
Data Structures Help...
By jac0117 in forum New To JavaReplies: 1Last Post: 01-12-2008, 07:31 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks