組播:傳送的資訊,小組內所有成員都能同時接受到。
組播發送端跟接收端的具體流程為:
傳送方:
a.建立socket
socket_fd=socket(af_inet,sock_dgram,0);
if(socket_fd < 0)
b.設定傳送的群組的位址
/*set up the destination address*/
memset(&group_addr,0,sizeof(struct sockaddr_in));
group_addr.sin_family = af_inet;
group_addr.sin_port = htons(group_port);
group_addr.sin_addr.s_addr = inet_addr("230.1.1.1");
c.傳送,呼叫sendto()函式
if (sendto (socket_fd, recmsg, strlen(recmsg), 0, (struct sockaddr *) &group_addr,
sizeof(struct sockaddr_in)) < 0)
接收方:
1.建立socket
socket_fd=socket(af_inet,sock_dgram,0);
if(socket_fd < 0)
2.(可選)設定多個socket公用同一埠號,如果在同一臺電腦中需要多個程式接受的話,需要設定。
/* allow multiple sockets to use the same port number */
if(setsockopt(socket_fd,sol_socket,so_reuseaddr,&yes,sizeof(yes))
< 0)
3.設定本地位址資訊
/*set up the local address*/
memset(&local_addr,0,sizeof(local_addr));
local_addr.sin_family = af_inet;
local_addr.sin_addr.s_addr = htonl(inaddr_any);
local_addr.sin_port = htons(group_port); //this port must be the group port
/*bind
local address*/
if(bind(socket_fd,(struct sockaddr *)&local_addr,sizeof(local_addr)) == -1)
5.設定群組位址與埠
/*use the setsocketopt() to request joining the multicast group*/
mreq.imr_multiaddr.s_addr=inet_addr(group_ip);
mreq.imr_inte***ce.s_addr=htonl(inaddr_any);
6.加入群組
if(setsockopt(socket_fd,ipproto_ip,ip_add_membership,&mreq,sizeof(mreq))
< 0)
7.接受訊息
bzero(recmsg, sizeof(recmsg)-1);
n = recvfrom(socket_fd, recmsg, sizeof(recmsg)-1, 0,(struct sockaddr *)&group_addr, &addr_len);
if (n < 0) else
完整**如下:
傳送方:
#include
#include
#include
#include
#include
#include
#include
#include
#include
using
namespace
std;
int main(void)
/* allow multiple sockets to use the same port number */
if (setsockopt(socket_fd,sol_socket,so_reuseaddr,&yes,sizeof(yes)) < 0)
/*set up the destination address*/
memset(&group_addr,0,sizeof(struct sockaddr_in));
group_addr.sin_family = af_inet;
group_addr.sin_port = htons(group_port);
group_addr.sin_addr.s_addr = inet_addr("230.1.1.1");
/*set up the local address*/
memset(&local_addr,0,sizeof(local_addr));
local_addr.sin_family = af_inet;
local_addr.sin_addr.s_addr = htonl(inaddr_any);
local_addr.sin_port = htons(group_port); //this port must be the group port
/*bind local address*/
if(bind(socket_fd,(struct sockaddr *)&local_addr,sizeof(local_addr)) == -1)
/*use the setsocketopt() to request joining the multicast group*/
mreq.imr_multiaddr.s_addr=inet_addr(group_ip);
mreq.imr_inte***ce.s_addr=htonl(inaddr_any);
if (setsockopt(socket_fd,ipproto_ip,ip_add_membership,&mreq,sizeof(mreq)) < 0)
/*loop to send or recieve*/
while(1)
if (sendto (socket_fd, recmsg, strlen(recmsg), 0, (struct sockaddr *) &group_addr,
sizeof(struct sockaddr_in)) < 0)
printf("'%s' send ok\n", recmsg);
}}
接收方:
#include
#include
#include
#include
#include
#include
#include
#include
#include
using
namespace
std;
int main(void)
/* allow multiple sockets to use the same port number */
if (setsockopt(socket_fd,sol_socket,so_reuseaddr,&yes,sizeof(yes)) < 0)
/*set up the destination address*/
memset(&group_addr,0,sizeof(struct sockaddr_in));
group_addr.sin_family = af_inet;
group_addr.sin_port = htons(group_port);
group_addr.sin_addr.s_addr = inet_addr("230.1.1.1");
/*set up the local address*/
memset(&local_addr,0,sizeof(local_addr));
local_addr.sin_family = af_inet;
local_addr.sin_addr.s_addr = htonl(inaddr_any);
local_addr.sin_port = htons(group_port); //this port must be the group port
/*bind local address*/
if(bind(socket_fd,(struct sockaddr *)&local_addr,sizeof(local_addr)) == -1)
/*use the setsocketopt() to request joining the multicast group*/
mreq.imr_multiaddr.s_addr=inet_addr(group_ip);
mreq.imr_inte***ce.s_addr=htonl(inaddr_any);
if (setsockopt(socket_fd,ipproto_ip,ip_add_membership,&mreq,sizeof(mreq)) < 0)
/*loop to send or recieve*/
while(1)
else
}}
udp組播通訊實現 c
前幾天需要做乙個和第三方平台配套的單向通訊,反向隔離的通訊介面,原本確定是採用udp組播方式實現,通訊裝置中間加正向網路隔離裝置防止反通訊。我想著趕緊先行測試驗證一下,哪知我好不容易花了乙個下午編譯測試後,對接的廠家卻反饋說不再使用udp通訊,他們要用http方式,並不考慮兩個系統通訊隔離問題。我只...
linux程式設計udp組播
linux程式設計udp通訊 linux程式設計udp廣播 使用範圍 結構體struct ip mreqn struct in addr 組播位址 224.0.0.0 224.0.0.255 預留的組播位址 永久組位址 位址224.0.0.0保留 不做分配,其它位址供路由協議使用 224.0.1.0...
linux下組播的實現
目前有三種通訊方式 單播 unicast 廣播 broadcast 組播 multicast 單播解決了點對點通訊的需求 廣播是點對多點的通訊,其存在兩個缺點 1 只能在同一網段內實現廣播 2 不能指定目的主機,所有網段內的主機都將收到廣播報文,存在頻寬浪費。組播組可以是永久的也可以是臨時的。組播組...