分类: 嵌入式
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;
}