客户端源码:

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define BUFLEN 255

int main(int argc, char **argv)
{
    struct sockaddr_in peeraddr;
    int sockfd;
    char recmsg[BUFLEN + 1];
    unsigned int socklen;

    /* 创建 socket 用于UDP通讯 */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        perror("socket\n");
        exit(errno);
    }
    printf("socket created success!!!\n");
    socklen = sizeof(struct sockaddr_in);

    /* 设置对方的端口和IP信息 */
    memset(&peeraddr, 0, socklen);
    peeraddr.sin_family = AF_INET;
    if (argv[2])
        peeraddr.sin_port = htons(atoi(argv[2]));
    else
        peeraddr.sin_port = htons(4567);

    if (argv[1]) {
        /* 注意这里设置的对方地址是指组播地址,而不是对方的实际IP地址 */
        if (inet_pton(AF_INET, argv[1], &peeraddr.sin_addr) <= 0) {
            printf("wrong group address!\n");
            exit(0);
        }
    } else {
        printf("no group address!\n");
        exit(0);
    }

    /* 循环接受用户输入的消息发送组播消息 */
    for (;;) {
        /* 接受用户输入 */
        bzero(recmsg, BUFLEN + 1);
        if (fgets(recmsg, BUFLEN, stdin) == (char *)EOF)
            exit(0);
        /* 发送消息 */
        if (sendto
            (sockfd, recmsg, strlen(recmsg), 0,
             (struct sockaddr *)&peeraddr, sizeof(struct sockaddr_in)) < 0) {
            printf("sendto error!\n");
            exit(errno);
        }
        printf("client sendto success!\n");
    }

    return 0;
}

服务端源码:

#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>

#define BUFLEN 255

int main(int argc, char **argv)
{
    struct sockaddr_in peeraddr;
    struct in_addr ia;
    int sockfd;
    char recmsg[BUFLEN + 1];
    unsigned int socklen, n;
    struct hostent *group;
    struct ip_mreq mreq;

    /* 创建 socket 用于UDP通讯 */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        perror("socket\n");
        exit(errno);
    }
    printf("socket created success!!!\n");
    /* 设置要加入组播的地址 */
    bzero(&mreq, sizeof(struct ip_mreq));
    if (argv[1]) {
        if ((group = gethostbyname(argv[1])) == (struct hostent *)0) {
            perror("gethostbyname");
            exit(errno);
        }
    } else {
        printf
            ("you should give me a group address, 224.0.0.0-239.255.255.255\n");
        exit(errno);
    }

    bcopy((void *)group->h_addr, (void *)&ia, group->h_length);
    printf("组播地址:%s\n", inet_ntoa(ia));
    /* 设置组地址 */
    bcopy(&ia, &mreq.imr_multiaddr.s_addr, sizeof(struct in_addr));

    /* 设置发送组播消息的源主机的地址信息 */
    mreq.imr_interface.s_addr = htonl(INADDR_ANY);

    /* 把本机加入组播地址,即本机网卡作为组播成员,只有加入组才能收到组播消息 */
    if (setsockopt
        (sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
         sizeof(struct ip_mreq)) == -1) {
        perror("setsockopt");
        exit(errno);
    }

    socklen = sizeof(struct sockaddr_in);
    memset(&peeraddr, 0, socklen);
    peeraddr.sin_family = AF_INET;
    if (argv[2])
        peeraddr.sin_port = htons(atoi(argv[2]));
    else
        peeraddr.sin_port = htons(4567);
    if (argv[1]) {
        if (inet_pton(AF_INET, argv[1], &peeraddr.sin_addr) <= 0) {
            printf("Wrong dest IP address!\n");
            exit(0);
        }
    } else {
        printf("no group address given, 224.0.0.0-239.255.255.255\n");
        exit(errno);
    }

    /* 绑定组播地址的端口和IP信息到socket上 */
    if (bind
        (sockfd, (struct sockaddr *)&peeraddr,
         sizeof(struct sockaddr_in)) == -1) {
        printf("Binded failure\n");
        exit(0);
    } else
        printf("binded success!!!\n");

    /* 循环接收网络上来的组播消息 */
    for (;;) {
        bzero(recmsg, BUFLEN + 1);
        n = recvfrom(sockfd, recmsg, BUFLEN, 0,
                     (struct sockaddr *)&peeraddr, &socklen);
        if (n < 0) {
            printf("recvfrom error in udptalk!\n");
            exit(errno);
        } else {
            /* 成功接收到数据报 */
            recmsg[n] = 0;
            printf("peer info:%s", recmsg);
        }
    }

    return 0;
}

编译:

$ gcc -o client client.c
$ gcc -o server server.c

运行参考样例:

$ ./client 234.1.2.3 4567
socket created success!!!
just to test!
client sendto success!


$ ./server 234.1.2.3 4567
socket created success!!!
组播地址:234.1.2.3
binded success!!!
peer info:just to test!

txgcwm
764 声望71 粉丝

Linux C/C++


引用和评论

0 条评论