當前位置:首頁 » 操作系統 » udp可靠傳輸源碼

udp可靠傳輸源碼

發布時間: 2023-07-25 22:21:39

Ⅰ 求一份c語言的RTP音頻傳輸源碼

1.項目前期工作(配置好環境)
2.發送端文件編寫(見下面的send.cpp)
3.接收端文件編寫(見下面的receive.cpp)
4.編譯文件
(1)發送端
g++-osendsend.cpp-I/usr/local/include/jrtplib3/-ljrtp
(2)接收端
g++-oreceivereceive.cpp-I/usr/local/include/jrtplib3/-ljrtp
附錄:
(1)send.cpp
[cpp]
#include"rtpsession.h"
#include"rtppacket.h"
#include"rtpudpv4transmitter.h"
#include"rtpipv4address.h"
#include"rtpsessionparams.h"
#include"rtperrors.h"
#include"rtpmemorymanager.h"
#ifndefWIN32
#include<netinet/in.h>
#include<arpa/inet.h>
#else
#include<winsock2.h>
#endif//WIN32
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string>

//
//.Ifso,itdisplaysanerror
//messageandexists.
//

voidcheckerror(intrtperr)
{
if(rtperr<0)
{
std::cout<<"ERROR:"<<RTPGetErrorString(rtperr)<<std::endl;
exit(-1);
}
}

//
//Themainroutine
//

#ifdefRTP_SUPPORT_THREAD

classMyMemoryManager:publicRTPMemoryManager
{
public:
MyMemoryManager()
{
mutex.Init();
alloccount=0;
freecount=0;
}
~MyMemoryManager()
{
std::cout<<"alloc:"<<alloccount<<"free:"<<freecount<<std::endl;
}
void*AllocateBuffer(size_tnumbytes,intmemtype)
{
mutex.Lock();
void*buf=malloc(numbytes);
std::cout<<"Allocated"<<numbytes<<"bytesatlocation"<<buf<<"(memtype="<<memtype<<")"<<std::endl;
alloccount++;
mutex.Unlock();
returnbuf;
}

voidFreeBuffer(void*p)
{
mutex.Lock();
std::cout<<"Freeingblock"<<p<<std::endl;
freecount++;
free(p);
mutex.Unlock();
}
private:
intalloccount,freecount;
JMutexmutex;
};

#else

classMyMemoryManager:publicRTPMemoryManager
{
public:
MyMemoryManager()
{
alloccount=0;
freecount=0;
}
~MyMemoryManager()
{
std::cout<<"alloc:"<<alloccount<<"free:"<<freecount<<std::endl;
}
void*AllocateBuffer(size_tnumbytes,intmemtype)
{
void*buf=malloc(numbytes);
std::cout<<"Allocated"<<numbytes<<"bytesatlocation"<<buf<<"(memtype="<<memtype<<")"<<std::endl;
alloccount++;
returnbuf;
}

voidFreeBuffer(void*p)
{
std::cout<<"Freeingblock"<<p<<std::endl;
freecount++;
free(p);
}
private:
intalloccount,freecount;
};

#endif//RTP_SUPPORT_THREAD

intmain(void)
{
#ifdefWIN32
WSADATAdat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif//WIN32

MyMemoryManagermgr;
RTPSessionsess(&mgr);
uint16_tportbase,destport;
uint32_tdestip;
std::stringipstr;
intstatus,i,num;

//First,we'

std::cout<<"Enterlocalportbase:"<<std::endl;
std::cin>>portbase;
std::cout<<std::endl;

std::cout<<"EnterthedestinationIPaddress"<<std::endl;
std::cin>>ipstr;
destip=inet_addr(ipstr.c_str());
if(destip==INADDR_NONE)
{
std::cerr<<"BadIPaddressspecified"<<std::endl;
return-1;
}

//Theinet_,but
//,soweuseacallto
//ntohl
destip=ntohl(destip);

std::cout<<"Enterthedestinationport"<<std::endl;
std::cin>>destport;

std::cout<<std::endl;
std::cout<<":"<<std::endl;
std::cin>>num;

//Now,we'llcreateaRTPsession,setthedestination,sendsome
//packetsandpollforincomingdata.

;
RTPSessionParamssessparams;

//IMPORTANT:,otherwise
//
//Inthiscase,we',sowe'll
//putthetimestampunitto(1.0/10.0)
sessparams.SetOwnTimestampUnit(1.0/10.0);

sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status=sess.Create(sessparams,&transparams);
checkerror(status);

RTPIPv4Addressaddr(destip,destport);

status=sess.AddDestination(addr);
checkerror(status);

for(i=1;i<=num;i++)
{
printf(" Sendingpacket%d/%d ",i,num);

//sendthepacket
status=sess.SendPacket((void*)"1234567890",10,0,false,10);
checkerror(status);

sess.BeginDataAccess();

//checkincomingpackets
if(sess.GotoFirstSourceWithData())
{
do
{
RTPPacket*pack;

while((pack=sess.GetNextPacket())!=NULL)
{
//Youcanexaminethedatahere
printf("Gotpacket! ");

//wedon'tlongerneedthepacket,so
//we'lldeleteit
sess.DeletePacket(pack);
}
}while(sess.GotoNextSourceWithData());
}

sess.EndDataAccess();

#ifndefRTP_SUPPORT_THREAD
status=sess.Poll();
checkerror(status);
#endif//RTP_SUPPORT_THREAD

RTPTime::Wait(RTPTime(1,0));
}

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdefWIN32
WSACleanup();
#endif//WIN32
return0;
}
(2)receive.cpp
[cpp]viewplain
#include"rtpsession.h"
#include"rtppacket.h"
#include"rtpudpv4transmitter.h"
#include"rtpipv4address.h"
#include"rtpsessionparams.h"
#include"rtperrors.h"
#ifndefWIN32
#include<netinet/in.h>
#include<arpa/inet.h>
#else
#include<winsock2.h>
#endif//WIN32
#include"rtpsourcedata.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string>

//
//.Ifso,itdisplaysanerror
//messageandexists.
//

voidcheckerror(intrtperr)
{
if(rtperr<0)
{
std::cout<<"ERROR:"<<RTPGetErrorString(rtperr)<<std::endl;
exit(-1);
}
}

//
//Thenewclassroutine
//

classMyRTPSession:publicRTPSession
{
protected:
voidOnNewSource(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
AddDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Addingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}

voidOnBYEPacket(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
DeleteDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Deletingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}

voidOnRemoveSource(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;
if(dat->ReceivedBYE())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
DeleteDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Deletingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}
};

//
//Themainroutine
//

intmain(void)
{
#ifdefWIN32
WSADATAdat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif//WIN32

MyRTPSessionsess;
uint16_tportbase;
std::stringipstr;
intstatus,i,num;

//First,we'

std::cout<<"Enterlocalportbase:"<<std::endl;
std::cin>>portbase;
std::cout<<std::endl;

std::cout<<std::endl;
std::cout<<"Numberofsecondsyouwishtowait:"<<std::endl;
std::cin>>num;

//Now,we'llcreateaRTPsession,setthedestination
//andpollforincomingdata.

;
RTPSessionParamssessparams;

//IMPORTANT:,otherwise
//
//Inthiscase,we'.
sessparams.SetOwnTimestampUnit(1.0/8000.0);

sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status=sess.Create(sessparams,&transparams);
checkerror(status);

for(i=1;i<=num;i++)
{
sess.BeginDataAccess();

//checkincomingpackets
if(sess.GotoFirstSourceWithData())
{
do
{
RTPPacket*pack;

while((pack=sess.GetNextPacket())!=NULL)
{
//Youcanexaminethedatahere
printf("Gotpacket! ");

//wedon'tlongerneedthepacket,so
//we'lldeleteit
sess.DeletePacket(pack);
}
}while(sess.GotoNextSourceWithData());
}

sess.EndDataAccess();

#ifndefRTP_SUPPORT_THREAD
status=sess.Poll();
checkerror(status);
#endif//RTP_SUPPORT_THREAD

RTPTime::Wait(RTPTime(1,0));
}

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdefWIN32
WSACleanup();
#endif//WIN32
return0;
}

Ⅱ 如何用java實現UDP的可靠傳輸

我記憶中可靠的傳輸應該類似TCP的三次握手:
1.發送方向接收方發送一個隨機數。
2.接收方收到隨機數後將其+1,再回傳給發送方。
3.發送方收到隨機數判斷其是否被+1,如果是代表雙方的傳遞線路是通暢的,可以正式開始傳送數據。

熱點內容
linux調試匯編 發布:2025-03-15 08:38:09 瀏覽:106
手機上編寫c語言 發布:2025-03-15 08:17:53 瀏覽:754
上傳迅雷下載速度 發布:2025-03-15 08:07:50 瀏覽:554
好看解壓書 發布:2025-03-15 08:04:18 瀏覽:672
文字頁游源碼 發布:2025-03-15 08:02:29 瀏覽:315
怎麼看自己微信密碼 發布:2025-03-15 07:53:58 瀏覽:791
androidchecked 發布:2025-03-15 07:50:22 瀏覽:551
百度carplay怎麼連接安卓手機 發布:2025-03-15 07:49:39 瀏覽:24
捕捉圖片上傳 發布:2025-03-15 07:49:01 瀏覽:796
手機內核升級編譯 發布:2025-03-15 07:43:22 瀏覽:237