Linux Learning High-level Concurrent Server Chapter (III)

Source: Internet
Author: User
Tags htons

UDP Multicast Server Multicast

The multicast group can be either permanent or temporary. A portion of the multicast group address, which is officially assigned, is called a permanent multicast group. The permanent multicast group remains the same as its IP address, and the composition of the members in the group can vary. The number of members in a permanent multicast group can be arbitrary or even zero. IP multicast addresses that are not retained for use by permanent multicast groups can be exploited by temporary multicast groups.

224.0.0.0~224.0.  0.255 for the reserved multicast address (Permanent group address), address 224. 0.0. 0 reserved for assignment, other addresses for routing protocol use; 224.0.  1.0~224.0.  224.0.2.0~238.255.  255. 255 The multicast address (Temporary group address) available to the user, valid throughout the network; 239.0.  0.0~239.255.  255.255 manages the multicast address locally and is valid only on a specific local scope. 

IP ad

View Network card number

If_nametoindex

A few computers into a group, the same group itself each computer has its own IP address, the same group has a group number, if you want to send a package to a group, the destination IP is set to the group number. One-to-many data transmission, there is a multicast concept in the IP layer, the client's IP address is meaningless, the client to accept the port number is still meaningful, TCP or UDP encapsulation port number, indicating which process accepted, generally used in the UDP domain, TCP with very little.

If the server is going to send a multicast packet, write (Sockfd,buf,buflen), and introduce a new function setsockopt, you can set the multilayer protocol.

Instance:

/*Server.c*/#include <stdio.h>#include <String.h>#include <netinet/In.h>#include <net/If.h>#include"Wrap.h"#define MAXLINE 80#define Serv_port 8000#define CLIENT_PORT 9000//Port number of the client#define GROUP "239.0.0.2"int main (void){struct sockaddr_in servaddr, cliaddr;//Address used for IPV4Socklen_t Cliaddr_len;int sockfd;//File descriptorCharBuf[maxline];Char Str[inet_addrstrlen];//BytesstructIP_MREQN Group; ssize_t Len;IntI, N;/*To construct a socket for UDP communication*/SOCKFD = socket (af_inet, SOCK_DGRAM,0);//Bzero (&AMP;SERVADDR,sizeof (SERVADDR));//Zeroing the Address//Set address servaddr.sin_family = Af_inet;/*IPv4*/SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);//Number of network bytes, local arbitrary IP servaddr.sin_port =Htons (Serv_port); Bind (SOCKFD, (struct sockaddr *) &servaddr,sizeof(SERVADDR));/*Set the group address*/Inet_pton (af_inet,group,&GROUP.IMR_MULTIADDR);/*Local arbitrary IP*/Inet_pton (Af_inet,"0.0.0.0",&group.imr_address);/*Eth0--> number command: IP ad*/Group.imr_ifindex=if_nametoindex ("Eth0"); SetSockOpt (Sockfd,ipproto_ip,ip_multicast_if,&group,sizeof(group));/*Constructing the client address ip+ port*/Bzero (&AMP;CLIENTADDR,sizeof (CLIENTADDR));//Zeroing the Address//Set address clientaddr.sin_family = Af_inet;/*IPv4*/ Inet_pton (Af_inet,group,&clientaddr.sin_addr.s_  addr);  Clientaddr.sin_port=htons (Client_port); printf ( "accepting connections ... \n" while (1sizeof (BUF), stdin); SendTo (Sockfd,buf,strlen (BUF), 0, (struct sockaddr *)   &clientaddr,sizeof (CLINETADDR));}   Close (SOCKFD); return 0    

/*Client.c*/#include <stdio.h>#include <String.h>#include <unistd.h>#include <net/If.h>#include <netinet/In.h>#include"Wrap.h"#define MAXLINE 4096#define SERVER_PORT 8000#define CLIENT_PORT 9000#define GROUP "239.0.0.2"int main (int argc,char *Argv[]) {structSockaddr_in serveraddr,localaddr;IntCONFD; ssize_t Len; Struck IP_MREQN Group;//Multicast structureCharBuf[maxline];//1. Create a socket Confd=socket (Af_inet,sock_dgram,0);//2. Initialize the local-side address bzero (&AMP;LOCALADDR,sizeof(LOCALADDR)); localaddr.sin_family=Af_inet; Inet_pton (Af_inet,"0.0.0.0",&LOCALADDR.SIN_ADDR.S_ADDR); localaddr.sin_port=Htons (Client_port); Bind (CONFD, (struct sockaddr *) &localaddr,sizeof(LOCALADDR));//Join the multicast group Inet_pton (af_inet,group,&GROUP.IMR_MULTIADDR); Inet_pton (Af_inet,"0.0.0.0group.imr_address); Group.imr_ifindex=if_nametoindex ( "eth0" ); // Set the client join multicast group setsockopt (confd,ipproto_ip,ip_add_ Membership,&group,sizeofwhile (1sizeof (BUF), 0,null,0return 0    
Other common functions

Name and address translation:

Obsolete, only for IPv4, thread insecure gethostbyname  know IP address via URL gethostbyaddr    Know the URL by IP address getservbynamegetservbyport based on the server program name or port number to obtain information, with a few getaddrinfogetnameinfofreeaddrinfo   trends, IPv4 and IPV6 can be processed simultaneously, thread safety

Socket Interface and Address Association

GetSockName according to the SOCKFD returned by the Accept, get the temporary port number getpeername based on the SOCKFD returned by accept, get the port number of the remote link, after exec can obtain the client information
source https://www.cnblogs.com/rainbow1122/p/7891490.html

Linux Learning High-level Concurrent Server Chapter (III)

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.