Binary vs non-binary socketing java
I hear'd that exist binary communication. I'm b开发者_运维知识库eginner in java, I use plain text based which I learn from java.sun.com tutorials for sockets. So I want to know what benefits have binary socketing? Why I should use that? And what resources you can suggest about binary communication?
I would advise you to look at Object stream
which allow simple binary passing of Objects
over a Socket connection from one Java VM to another (across a network if required).
The Java Sun Tutorial is a good resource to start. See the Basic I/O section.
Sending binary data across the socket would obviously be more efficient in terms of bandwidth usage. However, it also increases the complexity of having to marshal and unmarshal data across the socket. Sending textual data (XML, JSON, etc.) is more intuitive and easier to develop in many circumstances if performance is not critical.
You would only work directly with binary data over sockets if you had a specific reason to do so - such as extreme performance. If you had such a reason you would know it, and using binary data direct over sockets does not guarantee high performance.
There are many abstractions provided in Java and libraries to facilitate network communication whilst protecting the programmer from the labour intensive and error-prone work at that low level:
- Remote Method Invocation
- Web Services
- UrlConnection
- Object Serialization
are just some examples.
As mentioned in the other answers, you should avoid raw socket communication unless absolutely necessary. I would suggest XML as a communication data format instead of objects as it allows you easily communicate with clients running other languages as needed and is much easier to debug. XStream is a great library to facilitate object<-->xml conversion with minimal supporting code.
精彩评论