Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zidane

structs, sockets and Java

This topic is 5370 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]; };

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Yeah, I agree. But time is short at the moment though.

But in time I think I will add some form of encryption into the message .

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!