组播

来源:互联网 发布:java编程思想第5版txt 编辑:程序博客网 时间:2024/04/29 08:37

组播播出程序中主要的代码: 
        if   ((hSendSocket   =   socket(AF_INET,   SOCK_DGRAM,   0))   ==   INVALID_SOCKET) 
        { 
                sprintf(szPromoteMsg, "socket   failed   with:   %d/n ",   WSAGetLastError()); 
                AfxMessageBox(szPromoteMsg); 

                return   ; 
        } 
        //   Bind   the   socket   to   the   local   interface.   This   is   done   so   
        //   that   we   can   receive   data. 
        local.sin_family   =   AF_INET; 
        local.sin_port       =   htons(m_iPort); 
        local.sin_addr.s_addr   =   m_dwInterface; 

        if   (bind(hSendSocket,   (struct   sockaddr   *)&local,   
                sizeof(local))   ==   SOCKET_ERROR) 
        { 
                sprintf(szPromoteMsg, "bind   failed   with:   %d/n ",   WSAGetLastError()); 
                AfxMessageBox(szPromoteMsg); 

        return   ; 
        } 
        //   Setup   the   im_req   structure   to   indicate   what   group   we   want 
        //   to   join   as   well   as   the   interface 
        // 
        remote.sin_family             =   AF_INET; 
        remote.sin_port                 =   htons(m_iPort); 
        remote.sin_addr.s_addr   =   (m_dwTargetIPAddress); 
        
        mcast.imr_multiaddr.s_addr   =   (m_dwTargetIPAddress); 
        mcast.imr_interface.s_addr   =   m_dwInterface;//±¾µØÒÔÌ«Íø   MAC   µØÖ· 

        if   (setsockopt(hSendSocket,   IPPROTO_IP,   IP_ADD_MEMBERSHIP, 
                (char   *)&mcast,   sizeof(mcast))   ==   SOCKET_ERROR) 
        { 
                sprintf(szPromoteMsg, "setsockopt(IP_ADD_MEMBERSHIP)   failed:   %d/n ",           
                                                            WSAGetLastError()); 
                AfxMessageBox(szPromoteMsg); 

                return   ; 
        } 
        //   Set   the   TTL   to   something   else.   The   default   TTL   is   1. 
        optval   =   8; 
        if   (setsockopt(hSendSocket,   IPPROTO_IP,   IP_MULTICAST_TTL,   
                (char   *)&optval,   sizeof(int))   ==   SOCKET_ERROR) 
        { 
                sprintf(szPromoteMsg, "setsockopt(IP_MULTICAST_TTL)   failed:   %d/n ",   
                WSAGetLastError()); 
                AfxMessageBox(szPromoteMsg); 

                return   ; 
        } 
        //   Disable   the   loopback   if   selected.   Note   that   on   NT4   and   Win95 
        //   you   cannot   disable   it. 
        if   (bLoopBack) 
        { 
                optval   =   0; 
                if   (setsockopt(hSendSocket,   IPPROTO_IP,   IP_MULTICAST_LOOP, 
                        (char   *)&optval,   sizeof(optval))   ==   SOCKET_ERROR) 
                { 
                        sprintf(szPromoteMsg, "setsockopt(IP_MULTICAST_LOOP)   failed:   %d/n ",     
                        WSAGetLastError()); 
                        AfxMessageBox(szPromoteMsg); 

                        return   ; 
                } 
        } 
        bAllowedSending=TRUE; 
/* 
        BROADCASTPARAM   *pBroadcastParam=new   BROADCASTPARAM   ; 
        pBroadcastParam-> remote=remote; 
pBroadcastParam-> nRate=m_iRate; 
pBroadcastParam-> hBroadcastSocket=hSendSocket; 
pBroadcastParam-> bAllowedSending=bAllowedSending; 

AfxBeginThread(BroadcastThreadProc, 
                                                  (LPVOID)pBroadcastParam, 
  THREAD_PRIORITY_NORMAL, 
  0, 
  0, 
  NULL); 
*/

原创粉丝点击