這周主要做了udp組播的測試
//伺服器傳送端
#include #include #include #include #include #include #include #include #include #include #include #include #include #include std::string hello_group = "224.0.31.128";
int hello_port = 12345;
bool brunning = true;
void handler(int signo)
}int main(int argc, char *ar**)
else
if (argc > 2)
signal(sigpipe, handler);
signal(sigint, handler);
signal(sighup, handler);
signal(sigterm, handler);
signal(sigchld, handler);
signal(sigtstp, handler);
signal(sigquit, handler);
struct sockaddr_in addr;
int fd, cnt;
struct ip_mreq mreq;
char *message = "hello, world!";
/* create what looks like an ordinary udp socket */
if ((fd = socket(af_inet, sock_dgram, 0)) < 0)
//設定指定網絡卡名
if (inte***ce_name.size() > 0) }
/* set up destination address */
memset(&addr, 0, sizeof(addr));
addr.sin_family = af_inet;
if (hello_group.length() > 0)
else
addr.sin_port = htons(hello_port);
printf("send udp group data start, ip = %s, port = %d\n", hello_group.c_str(), hello_port);
/* now just sendto() our destination! */
std::uint64_t count = 0;
while (brunning)
printf("send %d byte to group count: %ld\n", strlen(message), ++count);
sleep(1);
} close(fd);
printf("send udp group data end, ip = %s, port = %d\n", hello_group.c_str(), hello_port);
return 0;
}
//客戶端**
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include std::string hello_group = "224.0.31.172";
int hello_port = 14382;
#define msgbufsize 8000
const long min_kb = 1024;
const long min_mb = 1024 * min_kb;
const long min_gb = 1024 * min_mb;
const long min_tb = 1024 * min_gb;
int g_filed = -1; // fd 就是file descriptor,檔案描述符
bool brunning = true;
void getsize(long datasize, char* sret)
; if (datasize < min_kb)
sprintf(sret, "%ld b", datasize);
else if (datasize < min_mb)
sprintf(sret, "%.4lf kb", (double)(datasize * 1000 / min_kb) / 1000);
else if (datasize < min_gb)
sprintf(sret, "%.4lf mb", (double)(datasize * 1000 / min_mb) / 1000);
else if (datasize < min_tb)
sprintf(sret, "%.4lf gb", (double)(datasize * 1000 / min_gb) / 1000);
else if (datasize >= min_tb)
sprintf(sret, "%.4lf tb", (double)(datasize * 1000 / min_tb) / 1000);
strcpy(sret, sret);
}void getdatetime(char* sret)
void handler(int signo)
}#define recv_loop_count 5
int recv_within_time(int fd, char *buf, size_t buf_n, struct sockaddr* addr, socklen_t *len, unsigned int sec, unsigned usec)
} }return -1;
}int main(int argc, char *ar**)
; u_int yes = 1; /*** modification to original */
/* create what looks like an ordinary udp socket */
if ((fd = socket(af_inet, sock_dgram, 0)) < 0)
/**** modification to original */
/* allow multiple sockets to use the same port number */
if (setsockopt(fd, sol_socket, so_reuseaddr, &yes, sizeof(yes)) < 0)
/*** end of modification to original */
/* set up destination address */
memset(&addr, 0, sizeof(addr));
addr.sin_family = af_inet;
addr.sin_addr.s_addr = htonl(inaddr_any); /* n.b.: differs from sender */
addr.sin_port = htons(hello_port);
/* bind to receive address */
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
std::string inte***ce_addr = "";
/* use setsockopt() to request that the kernel join a multicast group */
mreq.imr_multiaddr.s_addr = inet_addr(hello_group.c_str());
if (inte***ce_addr.size() > 0)
else
if (setsockopt(fd, ipproto_ip, ip_add_membership, &mreq, sizeof(mreq)) < 0)
/* now just enter a read-print loop */
printf("receive udp group data start, ip = %s, port = %d\n", hello_group.c_str(), hello_port);
// open data file
char stime[30] = ;
getdatetime(stime);
char sdatasize[100] = ;
long datasize = 0;
int ret = 0;
std::uint64_t count = 0;
while (brunning)
datasize += nbytes;
getdatetime(stime);
getsize(datasize, sdatasize);
printf("%s receive data size: %d, total size: %s, count: %ld\n", stime, nbytes, sdatasize, ++count);
}close(fd);
printf("receive udp group data end, ip = %s, port = %d\n", hello_group.c_str(), hello_port);
return 0;
}
udp組播需要三層交換機的支援,沒有三層交換機沒辦法做組播測試
udp組播會發生概率丟包,要減少丟包率,見
UDP組播接收
網路中的一台主機如果希望能夠接收到來自網路中其它主機發往某乙個組播組的資料報,那麼這麼主機必須先加入該組播組,然後就可以從組位址接收資料報。在廣域網中,還涉及到路由器支援組播路由等,但本文希望以乙個最為簡單的例子解釋清楚協議棧關於組播的乙個最為簡單明瞭的工作過程,甚至,我們不希望涉及到 igmp包。...
UDP的組播與多播
在前面已經記錄過udp的單播模式,即客戶端只能與服務端單獨對接。接下來介紹udp的另外兩種通訊方式 udpsocket newqudpsocket this udpsocket bind qhostaddress 192.168.1.100 45454 qbytearray datagram mul...
Linux C C 程式設計 Udp組播(多播)
ip組播通訊必須依賴於ip多播位址,在ipv4中它是乙個d類ip位址,範圍從224.0.0.0到239.255.255.255,並被劃分為區域性鏈結多播位址 預留多播位址和管理許可權多播位址三類 預留多播位址為 224.0.1.0 238.255.255.255,可用於全球範圍 如internet ...