Reading C structures from a socket, converting to Java Data types

I need to connect to a socket and then read in data from that socket that was created using C data structures.  Is there a way to do this without writing byte level parsers?  maybe using a java class with the same data definitions such as

c structure
struct{
unsigned char x;
double y;
....
} myStruct;

java class

class myClass{
char x;
double y;
...
};

will this work?  if so, how do I read the data from the socket into the class data?
Also, I can't find an unsigned keyword in Java, how are unsigned data types created and used in Java?

Thanks for the help
Ted
ted_driverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
If you use the java.nio classes, wrapped up in a ByteBuffer then you can do funtions like

bb.getChar() ;
bb.getDouble() ;
0
TimYatesCommented:
Watch out though, "unsigned char" and "char" are not the same type...

"unsigned char" is a single byte, wheras char is 2 bytes
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

TimYatesCommented:
what you probably want to do with that, is just read a single byte, and store it in a "short" (so that it is unsigned)
0
CEHJCommented:
>>Is there a way to do this without writing byte level parsers?  

No

>>maybe using a java class with the same data definitions

You should do that anyway, but it wont affect the parsing

>>Also, I can't find an unsigned keyword in Java, how are unsigned data types created and used in Java?

There is no unsigned keyword and only one unsigned type (char)

The easiest way would be to use a DataInputStream:

MyClass c = new MyClass();
DataInputStream in = new DataInputStream(socket.getInputStream());
c.initFromStream(in);

//====================================
public class MyClass {
      private char x;
      private double y;

      public void setX(char x) {
            this.x = x;
      }

      public char getX() {
            return x;
      }

      public void setY(double y) {
            this.y = y;
      }

      public double getY() {
            return y;
      }

      void readFromStream(DataInputStream in) {
            x = (char)in.readByte();
            y = in.readDouble();
      }
}
0
CEHJCommented:
Correction:

>>x = (char)in.readByte();

should be

         x = (char)(in.readByte() & 0xFF);

or you could just store that in an int

         x = (in.readByte() & 0xFF); // (where 'x' is an int)



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
You can use DataInputStream to read that, it contains methods to read the various primitives.
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.