Reliable Multicast Protocol VC implementation in windows...

Source: Internet
Author: User
Tags htons

Reliable multicasting is said in many places on the Internet and in the book. After reading it, I understand it, but it is very vague...

I also checked Windows Network Programming and finally realized it ..

Some server code:

// Dlgreliablemulticast. CPP: implementation file // # include "stdafx. H "# include" testserver. H "# include" dlgreliablemulticast. H "// zookeeper (histogram, cdialog) cdlgreliablemulticast: cdlgreliablemulticast (cwnd * pparent/* = NULL */): cdialog (cdlgreliablemulticast: IDD, pparent) {m_hworkthread = NULL;} cdlgreliablemulticast ::~ Upload () {} void upload: dodataexchange (cdataexchange * PDX) {cdialog: dodataexchange (PDX);} begin_message_map (cdlgreliablemulticast, cdialog) end_message_map () // receive message handlersbool cdlgreliablemulticast: oninitdialog () {cdialog: oninitdialog (); initnetwork (); Return true;} void cdlgreliablemulticast: initnetwork () {// create threaddword dwthreadid = 0; m_hworkthread = createthread (null, 0, networkthreadproc, this, create_suincluded, & dwthreadid); If (null = m_hworkthread) {return ;} setthreadpriority (m_hworkthread, thread_priority_highest); resumethread (m_hworkthread);} DWORD winapi restart: networkthreadproc (lpvoid lpvparam) {limit * pthis = (Limit *) lpvparam; return pthis-> threadproc ();} DWORD cdlgreliablemulticast: threadproc () {m_dwlocalipaddr = ...; // native ipwsadata wsadata; wsastartup (makeword (2, 2), & wsadata); m_hsocket = socket (af_inet, sock_rdm, ipproto_rm); If (m_hsocket = invalid_socket) {int nerr = wsagetlasterror (); logprintf (_ T ("socket: errorcode: % d \ n"), nerr);} // bind to inaddr_anysockaddr_in localaddr; memset (& localaddr, 0, sizeof (localaddr); localaddr. sin_family = af_inet; localaddr. sin_addr.s_un.s_addr = htonl (inaddr_any); localaddr. sin_port = htons (0); int nret = BIND (m_hsocket, (sockaddr *) & localaddr, sizeof (localaddr); If (nret = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("BIND: errorcode: % d \ n"), nerr);} // set the outbound interface ulong ulsend = m_dwlocalipaddr; nret = setsockopt (m_hsocket, ipproto_rm, rm_set_send_if, (char *) & ulsend, sizeof (ulsend); If (nret = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("setsockopt: errorcode: % d \ n"), nerr);} // connect the socket to connect the multicast group address sockaddr_in addrmulti; memset (& addrmulti, 0, sizeof (addrmulti); addrmulti. sin_family = af_inet; addrmulti. sin_addr.s_un.s_addr = inet_addr ("234.5.6.7"); addrmulti. sin_port = htons (5150); nret = connect (m_hsocket, (sockaddr *) & addrmulti, sizeof (addrmulti); If (nret = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("setsockopt: errorcode: % d \ n"), nerr);} Char Buf [1024] = {0 }; buf [0] = 'a'; Buf [1] = 'V'; Buf [2] = 'D'; Buf [3] = 'R'; while (1) {nret = Send (m_hsocket, Buf, sizeof (BUF), 0); If (nret = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("Send: errorcode: % d \ n "), nerr) ;}} wsacleanup (); Return 0 ;}

Client:

// Dlgreliablemulticast. CPP: implementation file // # include "stdafx. H "# include" testclient. H "# include" dlgreliablemulticast. H "// zookeeper (histogram, cdialog) cdlgreliablemulticast: cdlgreliablemulticast (cwnd * pparent/* = NULL */): cdialog (cdlgreliablemulticast: IDD, pparent) {m_hworkthread = NULL;} cdlgreliablemulticast ::~ Upload () {} void upload: dodataexchange (cdataexchange * PDX) {cdialog: dodataexchange (PDX);} begin_message_map (cdlgreliablemulticast, cdialog) end_message_map () // receive message handlersbool cdlgreliablemulticast: oninitdialog () {cdialog: oninitdialog (); initnetwork (); Return true;} void cdlgreliablemulticast: initnetwork () {// create threaddword dwthreadid = 0; m_hworkthread = createthread (null, 0, networkthreadproc, this, create_suincluded, & dwthreadid); If (null = m_hworkthread) {return ;} setthreadpriority (m_hworkthread, thread_priority_highest); resumethread (m_hworkthread);} DWORD winapi restart: networkthreadproc (lpvoid lpvparam) {limit * pthis = (Limit *) lpvparam; return pthis-> threadproc ();} DWORD cdlgreliablemulticast: threadproc () {m_dwlocalipaddr = ...; // native ipwsadata wsadata; wsastartup (makeword (2, 2), & wsadata); m_hsocket = socket (af_inet, sock_rdm, ipproto_rm); If (m_hsocket = invalid_socket) {int nerr = wsagetlasterror (); logprintf (_ T ("socket: errorcode: % d \ n"), nerr);} sockaddr_in addrmulti; memset (& addrmulti, 0, sizeof (addrmulti); addrmulti. sin_family = af_inet; addrmulti. sin_addr.s_un.s_addr = inet_addr ("234.5.6.7"); addrmulti. sin_port = htons (5150); int nret = BIND (m_hsocket, (sockaddr *) & addrmulti, sizeof (addrmulti); If (nret = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("BIND: errorcode: % d \ n"), nerr);} nret = listen (m_hsocket, 10 ); if (nret = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("Listen: errorcode: % d \ n"), nerr );} ulong localipaddress = m_dwlocalipaddr; nret = setsockopt (m_hsocket, ipproto_rm, rm_add_receive_if, (char *) & localipaddress, sizeof (localipaddress); If (nret = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("Listen: errorcode: % d \ n"), nerr);} sockaddr_in addrfrom; int fromlen = sizeof (addrfrom ); socket NS = accept (m_hsocket, (sockaddr *) & addrfrom, & fromlen); If (NS = invalid_socket) {int nerr = wsagetlasterror (); logprintf (_ T ("accept: errorcode: % d \ n"), nerr);} closesocket (m_hsocket); // while (1) {char Buf [1024]; int rc = Recv (NS, Buf, sizeof (BUF), 0); If (rc = socket_error) {int nerr = wsagetlasterror (); logprintf (_ T ("Recv: errorcode: % d \ n "), nerr);} else {//} closesocket (NS); wsacleanup (); Return 0 ;}

At the same time, you must install the reliable multicast protocol on the local machine as follows:

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.