Simply explain the multicast and broadcast implementation of java socket network programming

In Java, there are many ways to send and receive data. Some methods are close to the bottom, some problems need to be solved by programmers themselves, while some methods have a high level of abstraction and can be used conveniently. These data processing methods are from low to high according to the abstraction level:

1. Manual encoding: use bit operation to encode and parse one by one.

2. Use stream to automatically encode: combine OutputStream and bytearrayoutputstream.

3. Serialization: put the data into a data object, directly serialize the object and send it. It is very convenient to use, but pay attention to the loss of efficiency and the receiver should also use Java.

4. RMI: send all the calls to the method, and directly realize the remote call of the method.

In the bottom-level method 1, we need to solve some bottom-level problems ourselves:

1. Integer sending: consider whether it is a large tail or a small tail, an unsigned integer or a signed integer.

2. String sending: coding should be considered.

3. Type without length limit, such as large integer: to be encoded into frame, each frame is distinguished by delimiter or length bit.

Multicast and broadcasting

We can unicast a copy of the data to each recipient, but it can be very inefficient. Only UDP sockets allow broadcasting and multicast. The difference between the two is that broadcasting will be sent to all accessible hosts on the network. Some operating systems may not allow ordinary users to broadcast; Multicast is only sent to interested hosts. Specifically, call joingroup () of multicastsocket to join the host of multicast group.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>