Implementation of multicast code sending and receiving end in Jrtplib

Source: Internet
Author: User
Tags htons

</pre> article in the main is to send the end of the class receive the implementation of the code to come out, and important functions of the source paste out of the auxiliary learning, see online someone said a long time do not come out, suggest to see the corresponding to join the source of multicast, attention to details, words not much to say, code directly posted out, Friends have a lot of opinions to communicate. <p></p><p> send end: </p><pre code_snippet_id= "1677192" Snippet_file_name= "blog_20160510_2_ 5720022 "name=" code "class=" CPP "> #include" rtpsession.h "#include" rtpudpv4transmitter.h "#include" Rtpipv4address.h "#include" rtpsessionparams.h "#include" rtperrors.h "#include" rtppacket.h "#ifndef win32#include <netinet/in.h> #include <arpa/inet.h> #else # include <winsock2.h> #include <windows.h> #endif/ /win32#include <stdlib.h> #include <stdio.h> #include <iostream> #include <string>using Namespace Jrtplib;using namespace std;void checkerror (int rtperr) {if (rtperr<0) {cout<< "ERROR:" << Rtpgeterrorstring (Rtperr) <<endl;exit (-1);}} int main () {#ifdef win32wsadata dat; WSAStartup (Makeword (2,2), &dat); #endif//Win32list<uint32_t> localip; Rtpsession sess;uint16_t portbase=6666,destport=8888;uint32_t Destip, srcip;string ipstr= "239.255.0.1"; string ipsrc= "114.212.118.99"; int status,i,num;destip=inet_addr (IPSTR.C_STR ()) ;//Convert the string to a 32-bit network byte order. Srcip=inet_addr (Ipsrc.c_str ()); if (destip==inaddr_none) {cout<< "Bad Destip Address" <<endl;exit (-1);} Destip=ntohl (Destip);//convert 32-bit network byte order to host byte order Srcip=ntohl (SRCIP); Localip.push_back (SRCIP);// Set transmission parameters and session parameters Rtpudpv4transmissionparams Transparams; Rtpsessionparams sessparams;//important:the Local timestamp unit must BES set, otherwise//RTCP Sender Report Info'll be calculated wrong//in this case, we'll be sending all samples second, so we ' ll//put the timestamp unit t O (1.0/10.0) sessparams. Setowntimestampunit (1.0/8000); Sessparams. Setacceptownpackets (True); Transparams. Setportbase (portbase); Transparams. Setlocaliplist (Localip); Transparams. Setmulticastinterfaceip (SRCIP); Transparams. Setmulticastttl (255); status = Sess. Create (Sessparams,&transparams); checkerror (status); Rtpipv4address addr (destip,destport); status=sess. Adddestination (addr); ChecKerror (status); if (Sess. SupportsMulticasting ()) {cout<< "Support Multicast" &LT;&LT;ENDL;} Char *p=new char[100];i=0;sess. Setdefaultpayloadtype (Sess); Setdefaultmark (false); Sess. Setdefaulttimestampincrement, while (1) {i++;memset (p,0,100); sprintf (P, "bababba:%d", I); status=sess. Sendpacket (p,100); cout<<i<<endl;;} Sess. Byedestroy (Rtptime (10,0), 0,0); return 0;}
</pre ><pre code_snippet_id= "1677192" snippet_file_name= "blog_20160510_4_7431889" name= "code" class= "CPP" > Receive side: 
<pre name= "code" class= "CPP" > #include "rtpsession.h" #include "rtpudpv4transmitter.h" #include " Rtpipv4address.h "#include" rtpsessionparams.h "#include" rtperrors.h "#include" rtppacket.h "#ifndef win32#include <netinet/in.h> #include <arpa/inet.h> #else # include <winsock2.h> #include <windows.h> #endif/ /win32#include <stdlib.h> #include <stdio.h> #include <iostream> #include <string>using Namespace Jrtplib;using namespace std;void checkerror (int rtperr) {if (rtperr<0) {cout<< "ERROR:" << Rtpgeterrorstring (Rtperr) <<endl;exit (-1);}} int main () {#ifdef win32wsadata dat; WSAStartup (Makeword (2,2), &dat); #endif//Win32rtpudpv4transmissionparams transparams; Rtpsessionparams sessparams;list<uint32_t> Localip; Rtpsession sess;uint16_t portbase=8888;uint32_t destip,srcip;string ipstr= "239.255.0.1"; string ipsrc= " 114.212.114.200 "; int status,i,num;destip=inet_addr (IPSTR.C_STR ());//Converts a string to a 32-bit network byte order. Srcip=inet_addr (Ipsrc.c_str ());d estip=nTohl (Destip);//convert 32-bit network byte-order to host byte-order Srcip=ntohl (SRCIP); Localip.push_back (SRCIP); Sessparams. Setowntimestampunit (1.0/8000); Sessparams. Setacceptownpackets (True); Transparams. Setportbase (portbase);//transparams. Setlocaliplist (Localip); Transparams. Setbindip (SRCIP); Transparams. Setmulticastinterfaceip (SRCIP); Rtpipv4address addr (destip,portbase); status = Sess. Create (Sessparams,&transparams); checkerror (status); status=sess. SupportsMulticasting (); CheckError (status); status=sess. Joinmulticastgroup (addr); CheckError (status);//sess. Setdefaultpayloadtype (//sess); Setdefaultmark (false);//sess. Setdefaulttimestampincrement, while (1) {Sess. Begindataaccess (); if (Sess.                Gotofirstsourcewithdata ()) {Do{rtppacket *pack;                uint32_t Packetlen;                uint8_t *packetdata; uint8_t j = 11; while (pack = Sess. Getnextpacket ()) = NULL) {Packetlen = Pack->get                                 Packetlength ()-1;       Packetdata = Pack->getpacketdata ();                                        printf ("RECV data:"); while (j + + < Packetlen) {printf ("                                        %c ", * (Packetdata + j));                                        } printf ("\ n"); Sess.                                Deletepacket (Pack); }}while (Sess. Gotonextsourcewithdata ());} Sess. Enddataaccess ();} return 0;}


The important source code

int Rtpudpv4transmitter::joinmulticastgroup (const rtpaddress &addr) {if (!init)                return err_rtp_udpv4trans_notinit;                                 mainmutex_lock int status; if (!created) {Mainmutex_unlock return err_                rtp_udpv4trans_notcreated; } if (addr.                                 Getaddresstype ()! = rtpaddress::ipv4address) {Mainmutex_unlock                return err_rtp_udpv4trans_invalidaddresstype;                } const Rtpipv4address &address = (const rtpipv4address &) addr; uint32_t Mcastip = address.                                 GetIP (); if (!                                 RTPUDPV4TRANS_IS_MCASTADDR (Mcastip)) {Mainmutex_unlock Returnerr_rtp_udpv4trans_notamulticastaddress; } status = Multicastgroups.                 AddElement (Mcastip); if (status >= 0) {rtpudpv4trans_mcastmembership (Rtpsock,ip_add_membershi                                 P,mcastip,status); if (Status! = 0) {multicastgroups.                                                Deleteelement (Mcastip);                                Mainmutex_unlock return err_rtp_udpv4trans_couldntjoinmulticastgroup; } rtpudpv4trans_mcastmembership (Rtcpsock,ip_add_membership,mcas                                 Tip,status); if (Status! = 0) {Rtpudpv4trans_mcastmember                                  Ship (rtpsock,ip_drop_membership,mcastip,status);              Multicastgroups.                                                Deleteelement (Mcastip);                                Mainmutex_unlock return err_rtp_udpv4trans_couldntjoinmulticastgroup; }} mainmutex_unlock return status;}                                                                                                                                                                #define Rtpudpv4trans_mcastmembership (Socket,type,mcastip,status) { struct Ip_mreq Mr                                                                                                                                                                                                                                                eq MREQ.IMR_MULTIADDR.S_ADDR = htonl (Mcastip)                                                        ;                                                                                                        Mreq.imr_interface                                                                                                                                                                . s_addr = htonl (MCASTIFACEIP); Status = SetSockOpt (Socket,ipproto_ip,type, (const CHA                                                                                                                                                R *) &mreq, sizeof (struct ip_mreq)); The Mcastifaceip inside is set by itself in the CREATE function Bindip = Params->getb                Indip ();                                Mcastifaceip = Params->getmulticastinterfaceip ();                memset (&addr,0, sizeof (struct sockaddr_in));                addr.sin_family = af_inet;                Addr.sin_port = htons (Params->getportbase ());                 ADDR.SIN_ADDR.S_ADDR = htonl (BINDIP); if (bind (rtpsock, struct sockaddr *) &addr,sizeof (struct SOCKADDR_IN))! = 0) {rtpclose (rtpsock);                                Rtpclose (Rtcpsock);                Mainmutex_unlock return err_rtp_udpv4trans_cantbindrtpsocket;                } memset (&addr,0, sizeof (struct sockaddr_in));                addr.sin_family = af_inet;                Addr.sin_port = htons (Params->getportbase () +1);                 ADDR.SIN_ADDR.S_ADDR = htonl (BINDIP);                                if (bind (rtcpsock, struct sockaddr *) &addr,sizeof (struct sockaddr_in))! = 0) {                                Rtpclose (Rtpsock);                                Rtpclose (Rtcpsock);                Mainmutex_unlock return err_rtp_udpv4trans_cantbindrtcpsocket; }

Specific implementation of good code, I hope to be useful to you, under the vs2012 run:

http://download.csdn.net/detail/u010289908/9515174

Click to open link


Implementation of multicast code sending and receiving end in Jrtplib

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.