新聞中心
linux多播編程是一種具有多接收者功能的協(xié)議,它提供了一種有效的應用程序發(fā)送消息的方式。無論接收者在何處,小包就可以到達所有的系統(tǒng),從而節(jié)省了巨大的帶寬消耗。多播是內(nèi)部網(wǎng)絡(luò)(如局域網(wǎng))中一種極具吸引力的編程技術(shù),它允許多個應用程序發(fā)送和接收包數(shù)據(jù)。本文將深入探討Linux的多播編程。

我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、額敏ssl等。為數(shù)千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的額敏網(wǎng)站制作公司
Multi-cast programming under Linux is based on the BSD SOCKETs API and make use of the special IP protocol of the multi-cast protocol. To start using multi-cast programming, a program uses a call to the socket system call, passing the family PF_INET and the associated type. This creates a UDP datagram socket:
// 創(chuàng)建UDP socket
int sockfd = socket(PF_INET, SOCK_DGRAM, 0);
Once the socket has been created, the client can specify a multi-cast address using the bind system call. This designates the local IP address associated with the socket and allows the system to join a given multi-cast GROUP. For example, to join the 224.0.1.5 multi-cast group, one could use the following system call:
// 假設(shè)有機器的IP為192.168.1.100,加入 224.0.1.5多播組
struct sockaddr_in group_addr;
group_addr.sin_family = AF_INET;
group_addr.sin_addr.s_addr = inet_addr(“224.0.1.5”);
group_addr.sin_port = htons(5000);
// 啟動加入多播組
bind(sockfd, (struct sockaddr *)&group_addr, sizeof(group_addr));
Once the socket has been bound, the client can begin to send data packets to the multi-cast group using the sendto system call. Since multi-cast packets are sent over the network one time only, they are often referred to as one-time transmissions. In order to achieve this one-time transmission, the program must use the IP_multi-cast_LOOP option, which is sent along with the IP_ADD_MEMBERSHIP socket option when the socket is created. Finally, since multi-cast transmissions occur over UDP, when sending data to the group one must specify the port on which the data will be sent.
// 給 224.0.1.5多播組發(fā)送UDP數(shù)據(jù)
char buf[100]
struct sockaddr_in group_addr;
group_addr.sin_family = AF_INET;
group_addr.sin_addr.s_addr = inet_addr(“224.0.1.5”);
group_addr.sin_port = htons(5000);
// 啟動設(shè)置發(fā)送多播組
setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &val, sizeof(val));
// 啟動發(fā)送,指定機器192.168.1.100發(fā)送
sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&group_addr, sizeof(group_addr));
Receiving data from multi-cast groups is much the same as sending data. The main difference, of course, is that the client must use the recvfrom system call rather than the sendto system call. After the socket has been bound to the multi-cast group, the client can begin receiving data in a continuous loop using the recvfrom system call:
// 開啟一個接收循環(huán)
while(1)
{
recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL);
}
When a multi-cast client has finished receiving data, it must clean up after itself before exiting by calling the leave_group system call, which instructs the socket to leave the multi-cast group.
// 請求離開多播組
setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &group_addr, sizeof(group_addr));
本文重點介紹了linux多播編程,其中介紹了如何創(chuàng)建UDP socket,如何加入多播組,如何發(fā)送和接收數(shù)據(jù)以及如何離開多播組。多播編程使網(wǎng)絡(luò)編程更加方便,可以高效的發(fā)送數(shù)據(jù),值得學習和研究。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機、虛擬主機、域名注冊、VPS主機、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
文章題目:深入理解Linux多播編程(linux多播編程)
網(wǎng)站鏈接:http://m.5511xx.com/article/coeghji.html


咨詢
建站咨詢
