服务器端代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MCAST_PORT 31241
#define LOCAL_PORT 56171
#define MCAST_ADDR "224.1.2.3"
#define LocalIPAddr "10.10.106.108"
#define MCAST_DATA "111111222223333344444"
unsigned char buffer[]={"111111222223333344444"}; // UDP packet limit size: 15000 bytes
main(int argc, char **argv)
{
int soketfd; //sn?
int err;
int n =0;
int count = 0;
int temp =0x11;
int ttl =150;
struct sockaddr_in mcast_addr;
struct sockaddr_in local_addr; // Sender socket address structure
struct ip_mreq host_madd_ifadd; //struct init instance
soketfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
//soketfd=socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
if(soketfd == -1 )
{
perror("socket()");
return -1;
}
bzero(&local_addr, sizeof(struct sockaddr_in) );
bzero(&mcast_addr, sizeof(struct sockaddr_in) );
mcast_addr.sin_family =AF_INET;
mcast_addr.sin_port=htons(MCAST_PORT);
mcast_addr.sin_addr.s_addr =inet_addr(MCAST_ADDR);
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(LOCAL_PORT);
if(argv[1]!= NULL )
{
if(inet_pton(AF_INET, argv[1], &local_addr.sin_addr.s_addr) <= 0)
{
printf("set ip address error!\n");
exit(0);
}
}
else
{
printf("please give a local address ip !\n");
exit(0);
}
/* if(argv[2]!= NULL )
{
local_addr.sin_port = htons(atoi(argv[2]));
mcast_addr.sin_port= htons(atoi(argv[2]));
printf("port:%d\n",atoi(argv[2]));
}
else
{
local_addr.sin_port = htons(LOCAL_PORT);
mcast_addr.sin_port=htons(MCAST_PORT);
}*/
// local_addr.sin_addr.s_addr =inet_addr(LocalIPAddr);
if(bind(soketfd,(struct sockaddr *)&local_addr, sizeof(local_addr) ) < 0 )
{
printf("bind() error \n");
}
setsockopt(soketfd,IPPROTO_IP,IP_MULTICAST_TTL,&ttl,sizeof(ttl));
// strcpy(buffer,MCAST_DATA);
while(1)
{
n=sendto(soketfd,buffer,sizeof(buffer),0,(struct sockaddr *)&mcast_addr,sizeof(mcast_addr));
if(n<0)
{
perror("sendto()");
return -2;
}
usleep(2);
}
}
客户端代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MCAST_PORT 31241
#define MCAST_ADDR "224.1.2.3"
#define LocalIPAddr "192.168.197.125"
unsigned char buffer[5000]; // UDP packet limit size: 1500 bytes
main(int argc, char **argv)
{
int soketfd; //sn?
int err;
int revLen =0;
int counter =0;
struct sockaddr_in brcast_source_addr;
struct sockaddr_in source_addr;
struct ip_mreq host_madd_ifadd; //struct init instance
int forwardSocketFd; //sn?
int len = sizeof(struct sockaddr_in);
fd_set rfds;
struct timeval tv;
int retval;
soketfd=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(soketfd == -1)
{
perror("socket()");
return -1;
}
//memset(&mcast_addr,0,sizeof(mcast_addr));
bzero(&brcast_source_addr, sizeof(struct sockaddr_in) );
brcast_source_addr.sin_family =AF_INET;
brcast_source_addr.sin_addr.s_addr =inet_addr(MCAST_ADDR);
brcast_source_addr.sin_port=htons(MCAST_PORT);
host_madd_ifadd.imr_multiaddr.s_addr=inet_addr(MCAST_ADDR);
// host_madd_ifadd.imr_interface.s_addr=inet_addr(LocalIPAddr);
// host_madd_ifadd.imr_interface.s_addr= htons(INADDR_ANY);
if(argv[1]!= NULL )
{
if(inet_pton(AF_INET, argv[1], &host_madd_ifadd.imr_interface.s_addr) <= 0)
{
printf("set ip address error!\n");
exit(0);
}
}
else
{
printf("please give a local address ip !\n");
exit(0);
}
if(bind(soketfd,(struct sockaddr *)&brcast_source_addr, sizeof(brcast_source_addr)) < 0 )
{
printf("bind() soketfd error \n");
}
//sever host join into multicast.将本机加入组播组,足有加入组才能接受到消息。
err=setsockopt(soketfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,(void *)&host_madd_ifadd, sizeof( host_madd_ifadd ));
if(err<0)
{
printf("ERR:2_setsockopt():IP_ADD_MEMBERSHIP %d\r\n",err);
}
else
{
printf(" host join group 224.1.2.3 ok............\n");
}
while(1)
{
#if 1
// printf("------------------------------------\n");
revLen=recvfrom(soketfd,buffer,sizeof(buffer),0,(struct sockaddr *)&source_addr,(socklen_t*)&len);
if(revLen<0)
{
perror("recvfrom()");
return -2;
}
else
{
printf("revLen :%d brocast_srouce:%s\t brocast_port:%d\t counter:%d\n",revLen,inet_ntoa(source_addr.sin_addr),ntohs(source_addr.sin_port),counter++);
}
#endif
usleep(1);
}
}