当前位置: > 其它学习 > 网络基础 >

linux端组播测试代码

时间:2020-04-07 19:04来源:linux.it.net.cn 作者:IT
Linux端 组播网络测试 客户端和服务器端

服务器端循环输入发送

客户端接收并打印

客户端:

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

#define BUFLEN 255

int
main (int argc, char **argv)

    struct sockaddr_in peeraddr,ia; 
    int sockfd;
    char recmsg[BUFLEN + 1];
    unsigned int socklen, n;
    struct ip_mreq mreq;

    /* 创建 socket 用于UDP通讯 */
    sockfd = socket (AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {         
        printf ("socket creating err in udptalk\n");         
        exit (1);       
    }
    /* 设置要加入组播的地址 */
    bzero(&mreq, sizeof (struct ip_mreq));
   
    inet_pton(AF_INET,"226.0.1.192",&ia.sin_addr);
    /* 设置组地址 */
    bcopy (&ia.sin_addr.s_addr, &mreq.imr_multiaddr.s_addr, sizeof (struct in_addr));
    /* 设置发送组播消息的源主机的地址信息 */
    mreq.imr_interface.s_addr = htonl (INADDR_ANY); 
   // mreq.imr_interface.s_addr = inet_addr("192.168.0.88"); 

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

    socklen = sizeof (struct sockaddr_in);
    memset (&peeraddr, 0, socklen);
    peeraddr.sin_family = AF_INET;
    peeraddr.sin_port = htons (7838);
    inet_pton(AF_INET, "226.0.1.192", &peeraddr.sin_addr);

    /* 绑定自己的端口和IP信息到socket上 */
    if (bind(sockfd, (struct sockaddr *) &peeraddr,sizeof (struct sockaddr_in)) == -1)
    {     
        printf ("Bind error\n");     
        exit (0);   
    }
 
    /* 循环接收网络上来的组播消息 */
    for (;;)
    {    
        bzero (recmsg, BUFLEN + 1);    
        n = recvfrom (sockfd, recmsg, BUFLEN, 0, (struct sockaddr *) &peeraddr, &socklen);
        if (n < 0)
        {     
            printf ("recvfrom err in udptalk!\n");     
            exit (4);   
        }
        else{     
        /* 成功接收到数据报 */
            recmsg[n] = 0;     
            printf ("peer:%s", recmsg);   
        }
   
    }

}
服务器端:

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

#define BUFLEN 255

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

    /* 创建 socket 用于UDP通讯 */
    sockfd = socket (AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {     
        printf ("socket creating error\n");    
        exit (1);  
    } 
 
    /* 设置对方的端口和IP信息 */
    memset (&peeraddr, 0, socklen);
    peeraddr.sin_family = AF_INET; 
    peeraddr.sin_port = htons (7838);
   inet_pton (AF_INET, "226.0.1.192", &peeraddr.sin_addr);
  //  inet_pton (AF_INET, "192.168.0.100", &peeraddr.sin_addr);

 
/* 设置自己的端口和IP信息 */
    memset (&myaddr, 0, socklen);
    myaddr.sin_family = AF_INET;
    myaddr.sin_port = htons (23456);
   // myaddr.sin_addr.s_addr = inet_addr("192.168.0.88") ;
    myaddr.sin_addr.s_addr = htonl (INADDR_ANY) ;
 
    /* 绑定自己的端口和IP信息到socket上 */
    if (bind (sockfd, (struct sockaddr *) &myaddr,     sizeof (struct sockaddr_in)) == -1)
    {    
        printf ("Bind error\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 (3);   
        }
         
        printf ("'%s' send ok\n", recmsg);
    }
}
 





(责任编辑:IT)
------分隔线----------------------------