UDP information transmission method
single broadcast (unicast):
refers to a method of transmission in the transmission of the packet in the transmission of the computer network. The destination address is a single target. It is the most widely used in network applications today. Most of the network protocols or services used in generally use single -broadcast transmission, such as all TCP -based protocols.
Multicast:
Also called multi -broadcast, multi -point broadcast or group broadcast. Refers to passing the information to a set of destination addresses at the same time. It uses the strategy the most efficient, because the message only needs to be passed once on each network link, and the message will be copied only when the link is fork.
Multi -broadcast group specifies through the DP address and standard UDP port number. Class D IP address is within the range of 224.0.0.0 and 239.255.255.255 (including two). Address 224.0.0.0 is retained and should not be used.
Radio (Broadcast):
refers to a method of transmission of all devices in the network when the packet is transmitted in the computer network. In fact, the “all equipment” mentioned here is also limited to a scope, called “broadcast domain”.
Detailed introduction (from Wikipedia)
Single broadcast:
There are only two entities to communicate with each other at a time, and the sending end and receiving end are unique.
In the IPv4 network, 0.0.0.0 to 223.255.255.255 belongs to a single -broadcast address.
You shouted “Xiao Yueyue” to Xiaoyue Yue, so only Xiaoyue Yue turned back to promise you.
Circular:
“Drate” is usually used to refer to IP broadcast. IP broadcast is an agreement to send data to multiple receivers on the TCP/IP network by using a transliterate address at the same time. In addition, it is often used to combine audio and video protocols such as RTP.
Internet architect Dave Clark described the IP program: “If you put the packet from one end, the Internet will try to pass them to those who want to get them.”
The destination address of the
Broadcasting packet uses the D type of IP address, and the type D address cannot appear in the source IP address field of the IP message.
You shouted “Beauty” on the street, there will be a group of women looking back at you.
Broadcast address (refer to IANA)
The
orite can be permanent or temporary. In the address of the broadcasting group, some of them are officially assigned, called permanent group broadcasting group. The permanent group broadcasting group remains unchanged is its IP address, and the member composition in the group can change. The number of members in the permanent group can be arbitrarily or even zero. Those IP broadcast addresses that have not been retained for permanent broadcast groups can be used by the temporary broadcast group.
- 224.0.0.0 ~ 224.0.0.255 is the reserved programming address (permanent group address), the address 224.0.0.0 is retained without allocating, and other addresses are used for routing protocols;
- 224.0.1.0 ~ 224.0.1.255 is the public bother address, Internetwork Control Block;
- 224.0.2.0 ~ 238.255.255.255 is the operating address (temporary group address) that can be available for users, which is valid on the entire network;
- 239.0.0.0 ~ 239.255.255.255 is the local management bile address, which is effective within a specific local area.
Permanent programming address:
- 224.0.0.0 benchmark address (reserved)
- 224.0.0.1 The address of all hosts (including all routers)
- 224.0.0.2 The address of all the operators
- 224.0.0.3 No allocation
- 224.0.0.4 DVMRP router
- 224.0.0.5 All OSPF routers
- 224.0.0.6 ospf DR/BDR
- 224.0.0.7 ST router
- 224.0.0.8 ST host
- 224.0.0.9 RIP-2 router
- 224.0.0.10 EIGRP router
- 224.0.0.11 Activity proxy
- 224.0.0.12 DHCP server/relay agent
- 224.0.0.13 All PIM routers
- 224.0.0.14 RSVP package
- 224.0.0.15 All CBT routers
- 224.0.0.16 Specify SBM
- 224.0.0.17 All SBMS
- 224.0.0.18 vrrp
When the Ethernet transmits a single -broadcast IP message, the target MAC address uses the MAC address of the receiver. However, during the transmission group, the transmission purpose is no longer a specific receiver, but a group that members are uncertain, so the MAC address is used. The MAC address is corresponding to the IP address of the broadcast. IANA (Internet Assigned Number Authority) stipulates that the high 24bit of the MAC address is 0x01005e, and the low 23bit of MAC address is the low 23bit of the broadcast IP address.
Due to the latter 28 bits of the IP programming address, only 23 digits are mapped to the MAC address, so 32 IP -ide link addresses will be mapped to the same MAC address.
Broadcast:
Not all computer networks support broadcasting. For example, the X.25 network and frame relay do not support broadcasting, and there is no broadcast in the “entire Internet”. IPv6 does not support broadcasting, and the corresponding functions of the broadcast are replaced by the broadcast.
Generally, broadcasting is limited to the local area network, such as Ethernet or token ring network. Because the impact of broadcasting in the local area network is much smaller than the wide area network.
The
Ethernet and IPv4 network use the address of all 1 to represent the broadcast, which are FF: FF: FF: FF: FF: FF: FF: FF and 255.255.255.255.
The
token ring network uses IEEE 802.2 to control a special value in the control domain to represent broadcasting.
You shouted “Holiday” in the company, all colleagues responded and yelled to death.
java Socket programming based on UDP
Single broadcast:
ChatDemo.java
- package udp;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- /**
- *
- *
- *Writing a chat tool
- *There is a data part, and the data sends data part
- *These two are not performed at the same time
- *Multi -threaded technology
- *A thread control collection, a thread control sending
- *
- *Because it is inconsistent with the collection and mobility, two methods must be defined
- *and these two methods need to be encapsulated into different categories
- *
- *@author words 6
- *@date 2017/12/6 PM 8:25
- */
- class Send implements Runnable {
- private DatagramSocket socket;
- public Send(DatagramSocket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line = bufferedReader.readLine()) != null) {
- byte[] buff = line.getBytes();
- DatagramPacket packet = new DatagramPacket(buff,buff.length, InetAddress.getByName(“192.168.168.106”),10001);
- socket.send(packet);
- if(“bye”.equals(line)) {
- break;
- }
- }
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class Rece implements Runnable {
- private DatagramSocket socket;
- public Rece(DatagramSocket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- while(true) {
- byte[] buff = new byte[1024];
- DatagramPacket packet = new DatagramPacket(buff,buff.length);
- socket.receive(packet);
- String ip = packet.getAddress().getHostAddress();
- String data = new String(packet.getData(),0,packet.getLength());
- if(“bye”.equals(data)) {
- System.out.println(ip+“Already left”);
- continue;
- }
- System.out.println(ip+“Say”+data);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public class ChatDemo {
- /**
- *can be continuously sent to 192.168.168.106:10001
- *Send to port 10001 of the receiver, the recipient needs to be accepted at port 10001
- */
- public static void main(String args[]) throws SocketException {
- DatagramSocket sendSocket = new DatagramSocket();
- DatagramSocket reveSocket = new DatagramSocket(10001);
- new Thread(new Send(sendSocket)).start();
- new Thread(new Rece(reveSocket)).start();
- }
- }
Instructions
I have two computers here
One IP: 192.168.168.107 MAC
A computer: 192.168.168.106 Windows
The code above is the code of MAC, that is, 192.168.168.107 continuously sends data to the port of 192.168.168.106
192.168.168.106 received at 10001
MAC’s IDEA console sends data and Enter the car
Windows Idea Console Receive Data
ChatDemo2.java
- package udp;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.*;
- /**
- *
- *
- *Writing a chat tool
- *There is a data part, and the data sends data part
- *These two are not performed at the same time
- *requires multi -threaded technology
- *A thread control collection, a thread control sending
- *
- *Because it is inconsistent with the collection and mobility, two methods must be defined
- *and these two methods need to be encapsulated into different categories
- *
- *@author words 8
- *@date 2017/12/6 PM 8:25
- */
- class Send2 implements Runnable {
- private MulticastSocket socket;
- public Send2(MulticastSocket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- InetAddress group = InetAddress.getByName(“228.5.6.7”);
- socket.joinGroup(group);
- String line = null;
- while((line = reader.readLine())!= null) {
- byte[] buff = line.getBytes();
- DatagramPacket packet = new DatagramPacket(buff,buff.length,group,6789);
- socket.send(packet);
- if(“bey”.equals(line)) {
- break;
- }
- }
- socket.close();
- } catch (IOException e) {
- throw new RuntimeException(“Send end failure”);
- }
- }
- }
- class Rece2 implements Runnable {
- private MulticastSocket socket;
- public Rece2(MulticastSocket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- byte[] buff = new byte[1024];
- InetAddress group = InetAddress.getByName(“228.5.6.7”);
- socket.joinGroup(group);
- while(true) {
- DatagramPacket packet = new DatagramPacket(buff,buff.length);
- socket.receive(packet);
- String data = new String(packet.getData(),0,packet.getLength());
- String ip = packet.getAddress().getHostAddress();
- if(“bye”.equals(data)) {
- System.out.println(ip+“Leave”);
- continue;
- }
- System.out.println(“ip:”+ip+“Say:”+data);
- }
- } catch (IOException e) {
- throw new RuntimeException(“Accepting end failure”);
- }
- }
- }
- public class ChatDemo2 {
- public static void main(String args[]) throws IOException {
- MulticastSocket sendSocket = new MulticastSocket();
- MulticastSocket receSocket = new MulticastSocket(6789);
- new Thread(new Send2(sendSocket)).start();
- new Thread(new Rece2(receSocket)).start();
- }
- }
Instructions
or two -day machine, run the code above. A group with an IP of 228.5.6.7 is added to receive data on port 6789.
Effect chart
MAC’s IDEA console
Windows Idea console
Radio
ChatDemo3.java
- package udp;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.SocketException;
- /**
- *
- *
- *Writing a chat tool
- *There is a data part, and the data sends data part
- *These two are not performed at the same time
- *Need multi -threaded technology
- *A thread control collection, a thread control sending
- *
- *Because it is inconsistent with the collection and mobility, two methods must be defined
- *and these two methods need to be encapsulated into different categories
- *
- *@author words 10
- *@date 2017/12/6 PM 8:25
- */
- class Send3 implements Runnable {
- private DatagramSocket socket;
- public Send3(DatagramSocket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- while((line = bufferedReader.readLine()) != null) {
- byte[] buff = line.getBytes();
- DatagramPacket packet = new DatagramPacket(buff,buff.length, InetAddress.getByName(“192.168.168.255”),10001);
- socket.send(packet);
- if(“bye”.equals(line)) {
- break;
- }
- }
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- class Rece3 implements Runnable {
- private DatagramSocket socket;
- public Rece3(DatagramSocket socket) {
- this.socket = socket;
- }
- @Override
- public void run() {
- try {
- while(true) {
- byte[] buff = new byte[1024];
- DatagramPacket packet = new DatagramPacket(buff,buff.length);
- socket.receive(packet);
- String ip = packet.getAddress().getHostAddress();
- String data = new String(packet.getData(),0,packet.getLength());
- if(“bye”.equals(data)) {
- System.out.println(ip+“Leave”);
- }
- System.out.println(ip+“Say”+data);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public class ChatDemo3 {
- public static void main(String args[]) throws SocketException {
- DatagramSocket sendSocket = new DatagramSocket();
- DatagramSocket reveSocket = new DatagramSocket(10001);
- new Thread(new Send3(sendSocket)).start();
- new Thread(new Rece3(reveSocket)).start();
- }
- }
Instructions
Broadcasting is better to understand, my LAN network address here is 192.168.168.0
So the IP is 192.168.168.1 -192.168.168.254 is an effective IP address in this local area network
192.168.168.255 is called the broadcast address. As long as the broadcast address is sent, everyone in the local area network can receive it.
Running results
MAC version of Idea’s console
Windows version of IDEA’s console