Network Communication: unicast, broadcast, and Multicast
Directory:
I. Classification of network communication, their definition and characteristics.
2. network topology model of unicast, broadcast, and Multicast Transmission Information.
Iii. unicast, broadcast, and multicast programming instances.
I. Classification of network communication, their definition and characteristics.
2. network topology model of unicast, broadcast, and Multicast Transmission Information.
(1) unicast
As shown in Figure 8-1, there is an information sender Source, usera, and userc in the network to propose information requirements. The network transmits information in single-play mode.
Unicast transmission features are summarized as follows:
* Source sends an independent copy to each receiver address: packets for usera; packets for userc.
* The Network creates an independent data transmission path for each worker: Source → routerb → routere → routerd → usera; source → routerb → routere → routerf → userc.
In unicast mode, the amount of information transmitted over the network is directly proportional to the number of users who need the information. When the number of users who need the information is large, multiple identical information flows will appear in the network. In this case, bandwidth becomes an important bottleneck to ensure the quality of network transmission.
Unicast is suitable for networks with few users, which is not conducive to large-scale information transmission.
(2) Broadcast
As shown in Figure 8-2, the information sender Source, usera, and userc in the network propose information requirements, and the network transmits information through broadcast.
The Broadcast Transmission features are summarized as follows:
* Source sends only one packet to the broadcast address of the Network: packets for all the network.
* The Network sends a copy of the packet to all CIDR blocks, ensuring that the information is sent to all routers and users in the network, regardless of whether or not it is necessary. userb also receives a copy.
In broadcast mode, all users in the network can receive this information. When the number of users who need this information in the network is very small, the network resource utilization will be very low and bandwidth waste will be serious. Users who do not need this information will also be affected.
The broadcast mode is suitable for user-intensive networks, and the information security and paid services are not guaranteed.
(3) Multicast
As shown in Figure 8-3, information senders source, usera, and userc in the network propose information requirements, and the network transmits information through multicast.
Multicast Transmission features are summarized as follows:
* A multicast group is called a multicast group and is identified by an IP multicast address. Usera and userc are two message receivers. They are added to the multicast group to receive data sent to the multicast group.
* Source is called a multicast source. It sends only one packet to the multicast group address: packets for the multicast group. During network transmission, the same multicast data stream can have at most one copy on each link. Compared with unicast, when you use multicast to transmit information, the user increase does not significantly increase the network load.
* Based on the distribution of multicast group members, the multicast routing protocol establishes a tree route for multi-destination packet forwarding. Packets are copied and distributed at as far as possible intersections (such as routere) and finally transmitted to multicast group members. Compared with broadcast, multicast data is transmitted only to a recipient without wasting network resources.
* A vro that supports the multicast function in the network is called a multicast router. It not only provides the multicast routing function, it can also provide group member management functions (such as routerd and routerf) on the peripheral network segment connected to network users ). At the same time, you may also be a member of the multicast group.
* The members in the multicast group are dynamic. The user hosts in the network can join or leave the multicast group at any time. Group members may be widely distributed anywhere on the network. A Multicast Source is usually not the receiver of the data it sends, that is, it does not belong to its target multicast group.
* One source can send data to multiple multicast groups at the same time. Multiple sources can send packets to one multicast group at the same time.
* For help, you can watch a program on a TV channel.
* Multicast groups are an agreement between the sender and receiver, just like a TV channel.
* A TV station is a multicast source that sends data to a channel.
* The TV is the receiver's host. When the audience turns on the TV and chooses to watch the program of a channel, the host is added to a multicast group. Then, the TV plays the TV program of the channel, indicates that the host receives the data sent to this group.
* The audience can control the TV switch and channel switch at any time, indicating that the host dynamically joins or exits a multicast group.
Iii. unicast, broadcast, and multicast programming instances.
(1) unicast instance: (one echo server can communicate with multiple users at the same time)
Head. h
<Span style = "font-size: 12px;"> # ifndef _ head_h _ # DEFINE _ head_h _ # include <stdio. h> # include <string. h> # include <strings. h> # include <stdlib. h> # include <sys/types. h> # include <sys/socket. h> # include <ARPA/inet. h> # include <netinet/in. h> # include <error. h> # include <errno. h> # include <unistd. h> // fork # include <sys/Wait. h> # define ser_port 50004 # define ser_ip "192.168.192.128" # define client_port 50000 # define client_ip "192.168.192.128" # define buff_size 100 # define size 20 typedef struct _ User {char name [size]; char passwd [size];} USR; # define sys_err (_ MSG) error (exit_failure, errno, _ MSG)/* errno is a global variable. After the call, if an error occurs, the -- MSG information * Can be output for debugging. **/# endif </span>
Server. c
<Span style = "font-size: 12px;"> # include "head. H "int socket_init () {int sockfd; struct sockaddr_in sockaddr; If (sockfd = socket (af_inet, sock_stream, 0) =-1 )) sys_err ("socket"); bzero (& sockaddr, sizeof (sockaddr); sockaddr. sin_family = af_inet; sockaddr. sin_port = htons (ser_port); sockaddr. sin_addr.s_addr = inet_addr ("127.0.0.1"); printf ("% d = \ n", sockfd); If (BIND (sockfd, (struct sockaddr *) & sockaddr, sizeof (Sockaddr) =-1) sys_err ("bind"); Listen (sockfd, 5); // to understand the role of a listener, it is actually to create a buffer queue, allow the created queue to allow up to five sockets to enter return sockfd;} int do_echo (INT connectfd) {char sbuf [buff_size]; char rbuf [buff_size]; int N; printf ("int the server ===========\ N"); While (1) {printf ("==================== in the do_echo \ n"); bzero (rbuf, buff_size ); if (n = Recv (connectfd, rbuf, buff_size, 0) =-1) sys_err ("Recv"); If (n = 0) // If the accept value is 0, it indicates that the client has been disconnected and return-1 return -1; printf ("server: rbuf = % s \ n", rbuf); sprintf (sbuf, "% S % s", rbuf, "--------- echo "); send (connectfd, sbuf, strlen (sbuf), 0);} return;} int main (INT argc, const char * argv []) {// step: /** 1 create a socket Socket socket (); * 2 bind to the IP address (); * 3 listen to listen (); * 4 accept the link if there is a link, perform three-way handshake accept (); * 5 transmit read (), send (), and * 6 Close socket close (sockfd) with the client; ** note: master the call of each interface * How to Implement interaction between multiple clients and servers **/INT sockfd; // socket descriptor int connectfd with basic attributes; // perform three grip operations The socket descriptor sockfd = socket_init (); // listen (sockfd, 5); // you must understand the role of the listener. In fact, it is to establish a buffer queue, let the created socket descriptor to be // connected into and out/*** the accept () system call is used with connection-based socket types * (sock_stream, sock_seqpacket ). it extracts the first connection request on the * queue of pending connections for the listening socket, sockfd, creates a new ***/pid_t PID; signal (sigchld, sig_ign ); while (1) {connectfd = Accept (sockfd, null, null); // one server can shake hands with multiple client processes multiple times. Remember, the connect () function can also be used in the UDP protocol, instead of the dedicated if (connectfd =-1) sys_err ("accept") of TCP "); if (-1 = (pid = fork () {perror ("fork");} else if (pid = 0) {printf ("do_echo = % d \ n", do_echo (connectfd); close (connectfd); // The socket resource that copies the child process from the parent process, close (sockfd) closed;} Close (connectfd); // The parent process closes its socket close (sockfd); Return 0 ;} </span> <span style = "font-size: 14px;"> </span>
(2) broadcast instance:
Sender. C:
<Span style = "font-size: 14px;"> # include "head. H "int main (INT argc, const char * argv []) {int sockfd; struct sockaddr_in sockaddr; char rbuf [buff_size]; if (-1 = (sockfd = socket (af_inet, sock_dgram, 0) // because TCP only supports one-to-one communication, UDP can be one-to-one, one-to-many, multiple-to-one, multiple-to-many. Because of this broadcast. Yes. Therefore, it can only be sock_dgramperror ("socket"); bzero (& sockaddr, sizeof (sockaddr); sockaddr. sin_family = af_inet; sockaddr. sin_port = htons (braod_port); // # define braod_port 50001sockaddr. topology = inet_addr (braod_ip); // # define braod_ip "192.168.1.255" If (-1 = BIND (sockfd, (struct sockaddr *) & sockaddr, sizeof (sockaddr ))) perror ("bind"); While (1) {bzero (& rbuf, sizeof (rbuf); recvfrom (sockfd, rbuf, buff_size, 0, (struct sockaddr *) & sockaddr, sizeof (sockaddr); printf ("-------- % s ------- \ n", rbuf) ;}close (sockfd); Return 0 ;}</span>
(3) multicast instances:
Sender. C:
<Span style = "font-size: 14px;"> # include "head. H "int main (INT argc, const char * argv []) {/** step for creating a multicast Sender: * Create a socket * set the multicast address (optional) * send data * close socket **/INT sockfd; struct sockaddr_in multiaddr; char sbuf [buff_size] = "Hello, Xiaowang .... \ n "; int N; If (-1 = (sockfd = socket (af_inet, sock_dgram, 0) perror (" socket "); bzero (& multiaddr, sizeof (multiaddr); multiaddr. sin_family = af_inet; multiaddr. sin_port = htons (multi_port); // # define multi_port 50002 multiaddr as long as it is a user-defined port. sin_addr.s_addr = inet_addr (multi_ip); </span>
<Span style = "font-size: 14px;"> // # define multi_ip "224.4.4.4" The D Class address is the multicast address 224.0.0.1 --- 239.0000254while (1) {sleep (1 ); if (-1 = (n = sendto (sockfd, sbuf, strlen (sbuf), 0, (struct sockaddr *) & multiaddr, sizeof (multiaddr )))) perror ("sendto"); printf ("in the sending .......... \ n ") ;}return 0 ;}</span>
Recevier. C:
<Span style = "font-size: 14px;"> # include "head. H "int main (INT argc, const char * argv []) {int sockfd; int N; char rbuf [buff_size]; struct sockaddr_in groupaddr; // create a socket If (-1 = (sockfd = socket (af_inet, sock_dgram, 0) perror ("socket"); // Add the multicast struct ip_mreqn multiaddr; // multiaddr that can be found in the description of signal in Man 2 setsockopt. imr_multiaddr.s_addr = inet_addr (multi_ip); multiaddr. imr_address.s_addr = inet_addr (local_ip); multiaddr. imr_ifindex = 0; // set the property of the socket that allows sending multicast information if (-1 = setsockopt (sockfd, ipproto_ip, ip_add_membership, & multiaddr, sizeof (multiaddr ))) perror ("setsockopt"); // bind the multicast address bzero (& groupaddr, sizeof (groupaddr); groupaddr. sin_family = af_inet; groupaddr. sin_port = htons (multi_port); groupaddr. sin_addr.s_addr = inet_addr (multi_ip); BIND (sockfd, (struct sockaddr *) & groupaddr, sizeof (groupaddr); int t; while (1) {// bzero (& rbuf, sizeof (rbuf); t = sizeof (groupaddr); If (-1 = (n = recvfrom (sockfd, rbuf, buff_size, 0, (struct sockaddr *) & groupaddr, & T) perror ("recvfrom"); sleep (1); printf ("% s ----------- \ n", rbuf);} return 0 ;}</span>
References: http://www.xici.net/d79537500.htm
Reprint prohibited without permission