组播的实现

//
//  main.c
//  udpmuticastserver
//
//  Created by 吴珝君 on 2019/5/20.
//  Copyright © 2019年 闲着也是贤者. All rights reserved.
//

#include 
#include 
#include 
#include 
#include 

#define SERVER_PORT 8000
#define CLIENT_PORT 9000
#define MAX 1500
#define GROUP "224.0.2.0"
int main(int argc, const char * argv[]) {
    // insert code here...
    int sfd;
    int i =0;
    int len =0;
    socklen_t s;
    struct sockaddr_in serveraddr, clientaddr,ss;
    struct ip_mreq group;//组播结构题
    sfd = socket(AF_INET, SOCK_DGRAM, 0);
    char buf[MAX] = "这是一个测试文件\n";
    bzero(&serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(SERVER_PORT);
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
    bind(sfd, (struct sockaddr*)(&serveraddr), sizeof(serveraddr));
    //绑定地址和套接字
    inet_pton(AF_INET, GROUP, &(group.imr_multiaddr));//设置组播地址
    inet_pton(AF_INET, "0.0.0.0", &(group.imr_interface));//要获取的组播目的主机ip
    //需要获取网卡吗? 不一定需要
    setsockopt(sfd,IPPROTO_IP, IP_MULTICAST_IF, &group, sizeof(group));
    bzero(&clientaddr, sizeof(clientaddr));
    clientaddr.sin_family = AF_INET;
    inet_pton(AF_INET, GROUP, &clientaddr.sin_addr.s_addr);
    clientaddr.sin_port = htons(CLIENT_PORT);
    while (1) {
        sprintf(buf, "helloworld %d\n", i++);
        //fgets(buf, sizeof(buf), stdin);
        sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&clientaddr, sizeof(clientaddr));
        sleep(1);
    }
    return 0;
}
//
//  main.c
//  udpmuticastclient
//
//  Created by 吴珝君 on 2019/5/20.
//  Copyright © 2019年 闲着也是贤者. All rights reserved.
//

#include 
#include 
#include 
#include 
#include 

#define SERVER_PORT 8000
#define CLIENT_PORT 9000

#define GROUP "224.0.2.0"

int main(int argc, char *argv[])
{
    struct sockaddr_in localaddr,serveraddr,clie_addr;
    int confd;
    ssize_t len;
    char buf[BUFSIZ];
    char str[1024];
    socklen_t s,clie_addr_len;
    int l =0;
    struct ip_mreqn group;                                                  /* 组播结构体 */
    
    confd = socket(AF_INET, SOCK_DGRAM, 0);
    
    bzero(&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));
    
    inet_pton(AF_INET, GROUP, &group.imr_multiaddr);                        /* 设置组地址 */
    inet_pton(AF_INET, "0.0.0.0", &group.imr_address);                      /* 使用本地任意IP添加到组播组 */
    group.imr_ifindex = if_nametoindex("eth0");                             /* 通过网卡名-->编号 ip ad */
    setsockopt(confd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group));/* 设置client 加入多播组 */
    
    while (1) {
  
       l =  recvfrom(confd, buf, BUFSIZ,0, (struct sockaddr *)&clie_addr, &clie_addr_len);
        if (l == -1)
            perror("recvfrom error");
        
        printf("received from %s at PORT %d\n",
               inet_ntop(AF_INET, &clie_addr.sin_addr, str, sizeof(str)),
               ntohs(clie_addr.sin_port));
        write(STDOUT_FILENO, buf, l);
    }
    close(confd);
    
    return 0;
}
/*
这句话至关重要,这个是一个传人参数,说明这个结构题占用的空间 

       clie_addr_len = sizeof(clie_addr);
       l =  recvfrom(confd, buf, BUFSIZ,0, (struct sockaddr *)&clie_addr, &clie_addr_len);

*/

 

你可能感兴趣的:(网络编程)