Implementation of Linux UDP unicast multicast multicast __linux

Source: Internet
Author: User
Tags switches

Reprint http://blog.sina.com.cn/s/blog_4fc7368a0100okbq.html


1, multicast and broadcast need to be in the LAN to achieve, and also to see whether the Linux system support multicast and broadcast: Ifconfig up broadcast multicast mtu:1500 metric: 1
Explains that the NIC supports
2, send multicast packet host need to set the gateway, otherwise run sendto () will appear "Network is unreachable", network card can be arbitrarily set, but must be set. Also add route 240.0.0.0, which is:
Route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0
Route add default GW "192.168.40.1" Dev eth0
3, appeared: "Setsockopt:no such Device". The prompt, explained that the multicast IP setting has the problem, the system needs the uint32_t format Network address the beginning is not 1110, the test pass. Workaround: Use Htonl (Inet_network ("*.*.*.*")) or Inet_aton function when converting the address string "*.*.*.*" to uint32_t (Inet_aton, GRUPO Addr

In addition there are articles: http://unix-cd.com/unixcd12/article_5577.html

11.3 Multicast

Unicast is used for end-to-end communication between two hosts, which is used for data communication of a host on all hosts on the local network. Unicast and broadcast are two extremes, either communicating to a single host or communicating to a host on the entire LAN. In practice, it is often necessary to communicate with a specific set of hosts, rather than all hosts on the entire LAN, which is the purpose of multicasting. 11.3.1 Multicast Concept

Multicast, also known as "multicast", the network of the same business type of host logically grouped, the data sent and received only in the same group, other hosts did not join this group can not send and receive the corresponding data.

When broadcasting on a wide area network, the switches and routers only copy and forward data to the host that needs to obtain the data. A host can request to join or exit a group from a router, and routers and switches in the network selectively replicate and transfer data, transferring data only to hosts within the group. This feature of multicast allows data to be sent to multiple hosts at once, while ensuring that no other communication is affected by other hosts that do not need to be joined to the group.

Compared to the traditional one-to-one unicast, multicasting has the following advantages:

Q hosts with the same business join the same data stream, share the same channel, save bandwidth and the advantages of the server, with the advantages of broadcasting without the bandwidth required for broadcast.

The total bandwidth of the Q server is not limited by client bandwidth. Because the multicast protocol is required by the receiver to determine whether to forward the data stream, the server-side bandwidth is constant, regardless of the number of clients.

Q As with unicast, multicast is allowed to be transmitted over the WAN, the Internet, and broadcasts are only on the same LAN.

The disadvantage of multicast:

Q Multicast has no error correction mechanism compared with unicast, but it is difficult to compensate when errors occur, but it can be implemented in the application layer.

There are some defects in the network support of Q multicast, which need the support of router and network protocol stack.

Multicast applications mainly include online video, online conferencing and so on. Multicast for 11.3.2 Wan

Multicast addresses are specific and Class D addresses are used for multicast. Class D IP address is a multicast IP address, that is, the IP address between 224.0.0.0 and 239.255.255.255, and is divided into local connection multicast addresses, reserved multicast addresses and administrative rights multicast address 3 categories:

Q Local Multicast address: Between 224.0.0.0~224.0.0.255, this is the address reserved for routing protocols and other purposes, and routers do not forward IP packets belonging to this range.

Q Reserved Multicast addresses: Between 224.0.1.0~238.255.255.255, available on a global scale (such as the Internet) or network protocols.

Q Admin Rights multicast address: Between 239.0.0.0~239.255.255.255, can be used within the organization, similar to private IP address, not to the Internet, can limit the multicast scope. 11.3.3 Multicast Programming

Multicast programming is implemented using the setsockopt () function and the getsockopt () function, and the multicast option is the IP layer, and its option values and meanings are shown in 11.5.

Table 11.5 Multicast-related options

Options for getsockopt ()/setsockopt ( )

Meaning

Ip_multicast_ttl

Set TTL values for multicast group data

Ip_add_membership

Join a multicast group on the specified interface

Ip_drop_membership

Quit a multicast group

Ip_multicast_if

Get default interface or set interface

Ip_multicast_loop

Prohibit Multicast data loopback

1. Option Ip_multicase_ttl

Option Ip_multicast_ttl allows you to set a timeout TTL that ranges from any value between 0~255, for example:

unsigned char ttl=255;

SetSockOpt (s,ipproto_ip,ip_multicast_ttl,&ttl,sizeof (TTL)); 2. Option Ip_multicast_if

Option ip_multicast_if is used to set the default default network interface for multicast, which is sent from a given network interface, and the other network interface ignores this data. For example:

struct IN_ADDR addr;

SetSockOpt (s,ipproto_ip,ip_multicast_if,&addr,sizeof (addr));

The parameter addr is the IP address of the desired multicast output interface, and the Inaddr_any address is used to echo to the default interface.

By default, when the native sends multicast data to a network interface, at the IP layer, the data is echoed to the local loopback interface, and the option Ip_multicast_loop is used to control whether the data is loopback to the local loopback interface. For example:

unsigned char loop;

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.