Android Development: Multicast (multicast) and broadcast

Source: Internet
Author: User
Tags joins set socket

Recently, due to the need to write the same LAN on the Android client and PC to automatically match the communication function of the program, learning and experimenting with the Java multicast and broadcast content, recorded some understanding as follows:

I. Multicast (multicast)
Background: Multicast uses UDP to send the same set of packet to addresses within a range, that is, the message can be sent to multiple recipients at one time, and the main difference from unicast is the form of an address. The IP protocol allocates a range of address spaces to multicast (multicast can only use IP in this range), and the multicast address range in IPV4 is 224.0.0.0 to 239.255.255.255.
Java programming: In Java to communicate through MulticastSocket instances, when used involves a few concepts ①ttl (time to Live), each IP message contains a TTL (is a number), each message is forwarded by a route its TTL minus 1, When the TTL becomes 0 o'clock, the message is discarded ② multicast group (multicast), the recipient only joins this group to get the packets sent to the group (this determines the multicast object)
Java code:
Send side (Android phone):
//-----------------------------------------------------------------------------------------
MulticastSocket msocket = new MulticastSocket (30001);//birth complete socket and bind 30001 port
InetAddress group=inetaddress.getbyname ("239.0.0.1");//Set Multicast IP
byte[] Buff = "QQ". GetBytes ("Utf-8");//Set the data of the multi-broadcast text
Msocket.joingroup (group);//Join a multicast group, the receiver can crawl multiple broadcast messages when the sender and receiver are in the same group
Msocket.settimetolive (4);//Set TTL
Set the UDP message (content, content length, multicast group, port)
Datagrampacket packet = new Datagrampacket (buff,buff.length,group,30001);
Msocket.send (packet);//Send a newspaper
Msocket.close ();//Close socket
//-----------------------------------------------------------------------------------------
Receiving End (PC):
//-----------------------------------------------------------------------------------------
MulticastSocket s = new MulticastSocket (30001);//birth complete socket and bound port
InetAddress Group = inetaddress.getbyname ("239.0.0.1");//Set Multicast IP
S.joingroup (group);//The recipient joins the multicast group and needs to be in the same group as the sender
Datagrampacket packet = new Datagrampacket (buffer, 100);//create receive messages to receive messages delivered over multicast
S.receive (packet);//Receive multi-broadcast text, the program waits until the message is received

S.close ();//Close socket
//-----------------------------------------------------------------------------------------
Precautions:
The media-aware capabilities of TCP/IP in 1.windows systems can cause the group to broadcast errors such as:
Java.net.SocketException:IP_ADD_MEMBERSHIP failed (out of hardware filters?)
The solution in Windows7 environment is as follows:
Use Registry Editor (type regedit in the run) to view the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters ( Tcpip Similar folder icon can expand inside there is parameters, first find for a long time)
Add the following registry value:
Value name: DisableDHCPMediaSense data type: REG_DWORD (DWORD (32-bit) value) after creation, right-click to modify its value to 1, that is, shut down, restart the machine
2. The author uses the PC to connect the broadband open program error, cut off the broadband operation is normal, guess for the router refused to forward multicast, this problem remains to be researched (because I use Windows Hostednetwork to create a notebook's own network, add the phone to test, So the test of the program can still be carried out, this is a reason to discard the use of multicast in the actual program
3. Not all routers support multicast, and some multicast-enabled routers default to the status of off-multicast, so there are real availability issues to consider when using multicast writing programs

Two. Broadcasting
Background knowledge:
With broadcasts, all hosts on the local network receive a copy of the data. Broadcasts use UDP packets, IPv4 use (255.255.255.255) addresses to send broadcasts, and local broadcasts are never forwarded by routers, which means that broadcast information is restricted to the local network.
Java Programming:
UDP unicast and broadcast similar, mainly IP is different, are sent with the Datagramsocket object
Java code:
Send side (Android phone)
//-----------------------------------------------------------------------------------------
byte[] Buff = "QQ". GetBytes ("Utf-8");//Set Message information
Datagramsocket socket=new datagramsocket ();//Set socket, parameter port number is not filled out, the system will automatically assign an available port
Create a message, including the message content, content length, message address (here all 1 addresses are broadcast), port number (the recipient needs to use the port)
Datagrampacket packet=new Datagrampacket (Buff,buff.length,inetaddress.getbyname ("255.255.255.255"), 30000);
Socket.send (packet);//Send a newspaper
Socket.disconnect ();//Disconnect Socket
Socket.close ();//Close socket
//-----------------------------------------------------------------------------------------
Receiving End (PC):
//-----------------------------------------------------------------------------------------
Datagramsocket socket=new Datagramsocket (30000);//Creating sockets
Byte[] buffer;//Create receive string
Buffer=new byte[35];
Datagrampacket packet = new Datagrampacket (buffer, buffer.length);//Create a Receive message to receive a broadcast transmitted over the
System.out.println ("Listening at UDP (30000) ....");
Socket.receive (packet);//Receive the message, the program waits until the message is received
Socket.disconnect ();//Disconnect Socket
Socket.close ();//Close socket
//--

Reprinted from: http://blog.sina.com.cn/s/blog_751eaa830101cigu.html

Android Development: Multicast (multicast) and broadcast

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.