Any point to using "byte" in java

i had put myself into the habit of using "byte" to store numeric values that don't get too large. I later went to learn how to store unsigned byte only to learn that in java bytes are still 32bit like integers. Is there anypoint to using bytes over integers?

Also what about with datastreams over the internet, are only 5bits or are 32 bits sent over the stream when a byte is sent.

If you put your bytes in an array they will take up 8 bits a piece, though the total will always be divideable by 32 (possibly 64 in a 64 bit application on a 64 bit platform). This is of course the same in every language (not just java) that uses a 32 bit or higher address spaces. Keep in mind though, that in Java, arrays are also object (so that's some additional overhead) so there is no gain packing only a small number of bytes together.



If you really want to fiddle around you can always put 4 bytes into an int using bit shifting. However, keep in mind that it's best to spend your time optimizing things that have the most impact. It's a waste to spend hours trying to change a few ints into bytes on objects hardly used. You can use profiling tools to figure out where the "hot spots" for memory usage and allocation in your program are.

bytes are the secret to low bandwidth  :slight_smile:

…it's the secret to JavaGameNetworking API. :-p

ahhh the secret you say… :evil:



well i am planning to use bytes with my multiplayer system. Since we are on the topic of bandwidth, when using a tcp/ip connection which type of stream has the least over head? just a simple input/oustream, I have been using dataStream because of its methods but im thinkins that it must have extra overhead.

Although re-inventing the wheel is a tried and true method to accomplishing a task one must ask why? :o



You really might consider taking a look into the JGN project and if it doesn't provide what you're looking for feel free to give some feedback on what you'd like and you might find it added for you. :wink:



As for TCP for games that require real-time synchronization I would avoid it altogether.  JGN uses UDP for the very reason that TCP is just slow in comparison and JGN has a CertifiedMessage feature that gives you most of the benefits of TCP without the overhead.  Further, it's an entirely Java approach to game networking and you don't have to be concerned with the specifics of networking.



Okay, now that I've got my plug in for JGN, datagram packets are probably the way to go for gaming just in general.  If you're not familiar with it I would suggest reading up on it as it's definitely worthwhile.



darkfrog

Thanks for the JGN plug, i'll look into that



My game's multiplayer is very RTS style. Where all players are linked in a P2P type network. Messages are only sent to send updates of player event's (like ordering a unit to move, or cast a spell). I didn't see the speed benefit of UDP over TCP while i the reliability is definatly needed, since a failed message will cause one or more of the computers to go out of sync.



Thanks for the info though, I'm looking into JGN now…



Also im guessing no, but are filetransfers possible through JGN?

Well, that's what CertifiedMessage is, it's guaranteed delivery over UDP.  This gives you the best of both worlds.  For most things in a game you don't need guaranteed delivery like in position updates because if it's missed there's another one right behind it to fill in the gap, but for something like a chat message, player join, etc. that is crucial to be delivered.  With JGN you simply either extend Message (for send an forget) or CertifiedMessage (that receives confirmation of receipt).



As for filetransfers JGN is capable of doing that now since it allows for arrays now.  What you'd probably want to do is have something like a FilePartCertifiedMessage (just an example name) and just have a file id representing the file, a long representing the position in the file, and a byte array that contains the byte data for that part of the file.  It would be sending in chunks. I've been considering adding an OrderedMessage that would be an extension of CertifiedMessage that would probably be useful here so you never receive chunks out of order.  If you decide to use JGN let me know if you want this and I'll add it for you.



My purpose in writing JGN was to keep everyone from having to re-create basically the same thing for every game that is written and hopefully get some collaboration so we could have one simple framework to do networking which allows us to spend less time worrying about networking and more time working on our games.



And JGN does support P2P in the sense that it has no direct support for any convention (eg. Client/Server).  All the examples use a Client/Server architecture, but it should be pretty easy to do a P2P kind of configuration you'd just have to set up every client like the server is configured in the Client/Server examples.



Feel free to post in the forums for JGN, I've been getting a little big of activity in the PhysicsNetworking forum, but nothing in the JGN forums as of yet. If people don't start talking in there soon I'll start rambling in there…and nobody wants that. :o



darkfrog

this thread will be continued here…

http://www.pyramex.com:88/smf/index.php?topic=5.0