UDP组播服务端与客户端代码实例

1.组播服务端代码(数据发送端)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
#define BUF_SIZE 		128
#define PORT     		50001
#define ADDR     		"192.168.1.100"
#define MUNICASTADDR    "235.3.3.3"
 
int main(int argc, char *argv[])
{
    //建立套接字
    int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);           //IPV4,数据报套接字类型,不指定协议
 
    struct sockaddr_in  group_addr;
    memset(&group_addr, 0, sizeof(group_addr));
	group_addr.sin_family = AF_INET,                              //协议类型IPV4
	group_addr.sin_port = htons(PORT),                            //端口号-网络字节序
	group_addr.sin_addr.s_addr = inet_addr(MUNICASTADDR);                 //IP地址-字符串转IP
 
    //发送数据
    char buf[BUF_SIZE] = { 0 };
	int count = 0;
    while

你可能感兴趣的:(c++,udp,网络)