Level6day9homework

1.【不定项选择】 以下功能用到UDP协议实现的是 ( A B D )

A. 广播
B. 组播
C.带外数据
D.ping

2.用于同一主机内部进程间通信的socket应使用的地址族是( d )

A. AF_INET
B. AF_NETLINK
C. AF_PACKET
D. AF_UNIX或AF_LOCAL

3. 请编写组播通信的服务器和客户端的代码:

(作业要求:做作业的时候不要再翻看视频上的教程,对函数理解不明白的全部通过man手册去查看,
自己思考框架,使用makefile编译,然后将测试的记录和结果添加到readme.txt文件中提交上来,代码实现完成测试通过后
再提交作业,网络部分学习不写代码不测试看不出问题的,良好的习惯帮助你们快速成长。)

组播发送端示例代码

#include <cstdio>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

#define DEBUG 0

void usage(char* s) {
    printf("\nThis is udp demo!\n");
    printf("\nUsage:\n\t %s serv_ip serv_port", s);
    printf("\n\t serv_ip: udp server ip address");
    printf("\n\t serv_port: udp server port(serv_port > 5000)\n\n");
}

int main(int argc, char* argv[])
{
    int fd = -1;
    int result = 0;
    int SERV_PORT = -1;
    char* SERV_IP_ADDR = NULL;
    struct sockaddr_in mysockaddr;
    char MESSAGE[] = "hELLo wOLd!!!\n";
    char botton[64] = {0};
#if DEBUG
    SERV_PORT = 5050;
    SERV_IP_ADDR = "192.168.0.158";
#else
    if (argc != 3) {
        usage(argv[0]);
        return -1;
    }

    SERV_PORT = atoi(argv[2]);
    if (SERV_PORT <= 5000) {
        usage(argv[0]);
        return -1;
    }
    SERV_IP_ADDR = argv[1];
#endif
    /* 1. 创建socket fd */
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0) {
        perror("socket");
        return -1;
    }
    /*2.连接服务器 写入数据 */

    /*2.1 填充struct sockaddr_in结构体变量 */
    bzero((void*)&mysockaddr, sizeof(mysockaddr));
    mysockaddr.sin_family = AF_INET;
    mysockaddr.sin_port = htons(SERV_PORT); //网络字节序的端口号
#if 0    
    mysockaddr.sin_addr.s_addr = inet_addr(SERV_IP_ADDR);
#else
    result = inet_pton(AF_INET, SERV_IP_ADDR, (void*)&mysockaddr.sin_addr);
    if (result != 1) {
        perror("inet_pton");
        return -1;
    }
#endif
    /*2.2  读写数据*/
    printf("Client staring...OK!\n");
    struct sockaddr_in clientscokaddr;
    socklen_t clientaddrlen;
    clientaddrlen = sizeof(clientscokaddr);

    while (1) {
        do {
            result = sendto(fd, MESSAGE, strlen(MESSAGE), 0, (struct sockaddr*)&mysockaddr, sizeof(mysockaddr));
        } while (result < 0 && EINTR == errno);   //判断是否阻塞了     
        if (result < 0) {
            perror("connect");
            break;
        }
        
        sleep(1);

        bzero((void*)botton, 64);
        bzero((void*)&clientscokaddr, clientaddrlen);
        
        do {
            result = recvfrom(fd, botton, 63, MSG_DONTWAIT, (struct sockaddr*)&clientscokaddr, &clientaddrlen);
        } while (result < 0 && EINTR == errno);
        printf("message is:%s\n", botton);
        
    }

    close(fd);
    return 0;
}

组播接收端示例代码

#include <cstdio>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>

#define SERV_PORT 5050
#define SERV_IP_ADDR "239.0.0.10"

int fun_process(int fd,char* botton,sockaddr_in clientscokaddr, socklen_t clientaddrlen);
void handler(int signo) {   //自动回收子进程 防止僵尸进程

    if (signo == SIGCHLD)
    {
        printf("Recall SIGINT\n");
        waitpid(-1, NULL, WNOHANG);
    }
}

int main()
{
    int fd = -1;
    int result = 0;
    pid_t pid;
    struct sockaddr_in mysockaddr;
    struct sockaddr_in clientscokaddr;
    socklen_t clientaddrlen;
    char botton[64];

    /* 1. 创建socket fd */
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0) {
        perror("socket");
        return -1;
    }

    /*优化4: 允许绑定地址快速重用 */
    int b_reuse = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof(int));
	/* 设置组播 */
	struct ip_mreq mreq;
	bzero(&mreq,sizeof(struct ip_mreq));
	mreq.imr_multiaddr.s_addr = inet_addr("239.0.0.10");
	mreq.imr_interface.s_addr = htonl(INADDR_ANY);
	setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));

    /*2. 绑定 */
    /*2.1 填充struct sockaddr_in结构体变量 */
    bzero((void*)&mysockaddr, sizeof(mysockaddr));
    mysockaddr.sin_family = AF_INET;
    mysockaddr.sin_port = htons(SERV_PORT);//网络字节序的端口号
    mysockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

    /*2.2 绑定 */
    result = bind(fd, (struct sockaddr*)&mysockaddr, sizeof(mysockaddr));
    if (result != 0) {
        perror("bind");
        return -1;
    }

    /*3. 调用recvfrom 接收客户端发来的信息 */
    /*优化2:通过程序获取刚建立连接的socket的客户端的IP地址和端口号*/
    clientaddrlen = sizeof(clientscokaddr);
    signal(SIGCHLD, handler);

    while (1) {
        bzero((void*)botton, 64);
        bzero((void*)&clientscokaddr, clientaddrlen);
       
        do {
            result = recvfrom(fd, botton, 63, 0, (struct sockaddr*)&clientscokaddr, &clientaddrlen);
        } while (result < 0 && EINTR == errno);

        if (-1 == result) {
            perror("recvfrom");
            break;
        }
        pid = fork();
        if (pid < 0) {
            perror("fork");
            break;
        }
        else if (0 == pid) {
            result = fun_process(fd, botton,clientscokaddr, clientaddrlen);
            close(fd);
            return result;
        }     
    }

    close(fd);
    return 0;
}

int fun_process(int fd, char* botton,sockaddr_in clientscokaddr, socklen_t clientaddrlen) {
    char ipv4_addr[16];
    int result;
    char botton_re[64] = "recv success!";
    printf("23333\n");
    if (!inet_ntop(AF_INET, (void*)&clientscokaddr.sin_addr, ipv4_addr, clientaddrlen)) {
        perror("inet_ntop");
        return -1;
    }

    printf("Clinet(%s:%d) is connected!\n", ipv4_addr, ntohs(clientscokaddr.sin_port));

    printf("message is:%s\n", botton);
    do {
        result = sendto(fd, botton_re, strlen(botton_re), 0, (struct sockaddr*)&clientscokaddr, sizeof(clientscokaddr));
    } while (result < 0 && EINTR == errno);   //判断是否阻塞了     
    if (result < 0) {
        perror("connect");
        return -1;
    }
    
    return 0;
}

结果展示: