Chinaunix首页 | 论坛 | 博客
  • 博客访问: 608718
  • 博文数量: 262
  • 博客积分: 8433
  • 博客等级: 中将
  • 技术积分: 2141
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-31 09:37
文章分类

全部博文(262)

文章存档

2012年(1)

2011年(168)

2010年(92)

2009年(1)

分类: 嵌入式

2011-04-26 14:56:36

作为多播数据的发送端,arm_linux端加入多播组不是必须的,可以加入,也可以不加入。

arm_linux端数据发送程序:(只给出功能函数)

void multicast_thread()
{
RTPSession sess;
int portbase = 6000;
int status;
//int length;
int loop;
bool iscast;
unsigned long destip;
unsigned long localip;
char *buff = "multicasting test";
int length = strlen(buff);
destip = inet_addr("233.0.0.1");
localip = inet_addr("172.29.26.101");
if(destip == INADDR_NONE || localip == INADDR_NONE)
{
   printf("Bad IP address specfied\n");
   return;
}
destip = ntohl(destip);
localip = ntohl(localip);
status = sess.Create(portbase,localip);
checkerror(status);
iscast = sess.SupportsMulticasting();
if(!iscast)
{
   sess.Destroy();
   printf("the rtp library was not complied with multicasting support\n");
   return;
}
/*************************************************************
* as a sender to a multicastgroup, it doesn't have to add to *
* the group.both can send the data successfully.       *
**************************************************************/
//sess.JoinMulticastGroup(destip);
status = sess.AddDestination(destip,portbase);
checkerror(status);
for(loop=0;loop<1000;loop++)
{
   status = sess.SendPacket(buff,length,1,false,25);
   printf("send packet: %d\n",loop+1);
   sleep(1);
}
sess.ClearDestinations();
//sess.LeaveMulticastGroup(destip);
sess.Destroy();
return;
}

windows端接收数据程序,需要加入多播组:

UINT Multicast_Thread(LPVOID)
{
CAdminToolDlg *dlg=(CAdminToolDlg*)AfxGetApp()->GetMainWnd();
RTPSession sess;
int portbase,status,length;
unsigned long destip;
unsigned char *RawData;
bool iscast;
//CString str;
portbase = 6000;
destip = inet_addr(MULTICAST);
if(destip == INADDR_NONE)
{
   AfxMessageBox("多播地址无效");
   return -1;
}
destip = ntohl(destip);
status = sess.Create(portbase);
dlg->checkerror(status);
iscast = sess.SupportsMulticasting();
if(!iscast)
{
   sess.Destroy();
   AfxMessageBox("不支持多播");
   return -1;
}
if(sess.JoinMulticastGroup(destip) < 0)
{
   AfxMessageBox("加入多播组失败");
   return -1;
}
//sess.SetReceiveMode(RECEIVEMODE_ACCEPTSOME);
//sess.AddToAcceptList(destip,1,portbase);
dlg->recvBytes = 0;
dlg->count     = 0;
while(1)
{
   sess.PollData();
   if(sess.GotoFirstSourceWithData())
   {
    //AfxMessageBox("收到数据");
    RTPPacket *pack;
    while((pack = sess.GetNextPacket()) != NULL)
    {
     length = pack->GetPayloadLength();
     RawData = pack->GetPayload();
     for(int i=0;i      {
      dlg->data[dlg->recvBytes] = RawData[i];
      dlg->recvBytes++;
     }
     dlg->count++;
    }
   }
   if(dlg->exitFlag == 1 || purview == 0)
   {
    dlg->m_brtprunning = FALSE;
    sess.Destroy();
    ExitThread(-1);
   
   }
}
sess.LeaveMulticastGroup(destip);
sess.Destroy();
AfxMessageBox("会话结束 \n");
dlg->m_brtprunning = FALSE;
return 0;
}

阅读(2140) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~