structs, sockets and Java
Hi guys, I was wondering if anyone could give me some advice on a little problem I am having.
I am writing a simple client\server based file sharing program. Now for reasons that you really dont have to know, the client is in Java and the server is C.
The server was written by me some time ago and uses sockets to comminicate. It works by sending structs down the network and the old C client just casted them back to structs when it received the data.
This worked fine, but as the old client was a commandline based program. I decided to update it as a Java app (dont ask why I am not using a c gui api please)
Now I am rather a noob when it comes to sockets in Java but I managed to get the client talking to the server just sending strings. But when it comes to sending the structs down the network my Java client goes insane.
I know the socket api is just sending a stream of bits down the network but is there an easy way of getting the data out of this like i could do with just casting in the old C client. Or am I going to have to read it bit by bit?
For reference the data struct looks like this:
struct s_MessageStruct {
unsigned int messageID;
unsigned int messageDataLength;
char messageData[500];
};
Unfortunately, there is no easy way to read structs from byte arrays in Java.
Given that your structure is rather simple, you could just wrap a DataInputStream around your socket input stream and read the 3 fields. Shouldn''t be that hard
Given that your structure is rather simple, you could just wrap a DataInputStream around your socket input stream and read the 3 fields. Shouldn''t be that hard
Yeah - I was reading up on something like that.
However is there anyways to tell how the structure is ordered?
Is it just in the order that the members are declared ?
However is there anyways to tell how the structure is ordered?
Is it just in the order that the members are declared ?
Well, using Sun''s JVM, reflection gives me the fields of a class in the order they were declared, but I think it is not required by the specs. So the short answer is no
Furthermore you should be careful with unsigned types, as they are not supported by Java. You''ll have to use long instead of unsigned int.
Furthermore you should be careful with unsigned types, as they are not supported by Java. You''ll have to use long instead of unsigned int.
just take the serialization of the struct into your own hands. regardless of whether that''s the way it is or not, manually specify that MessageID will be in the first x bytes, MessageDataLength will be in the next x bytes. Then you can just deserialize it on the Java end manually by picking out the bytes in the byte array
And be on the lookout for padding! Depending on the types of the struct members the C compiler will pad some of those. Unless you told the compiler to pack them tight and you just push the struct in the socket you''ll probably have some unneeded bytes inside the stream which you need to skip.
I have avoided the problem by ssprintf''ing the members into a string and sendign that down the network and using javas StringTokenizer to get the data and it works in a similar fasion the other way round as well.
Is this a good idea in your opinions?
Is this a good idea in your opinions?
well is good idea, but didn´t forget that any people can to do one string and send for your server.
And didn´t forget that everyone can get the messages between the client/server.
And didn´t forget that everyone can get the messages between the client/server.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement