Multicast.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:3k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // Multicast.cpp: implementation of the CMulticast class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "..stdafx.h"
- #include "Multicast.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- #pragma comment( lib , "ws2_32.lib")
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- int CMulticast::InitWSAStatup = 0;
- CMulticast::CMulticast()
- {
- if( ! CMulticast::InitWSAStatup )
- {
- WSADATA data;
- ::WSAStartup( MAKEWORD( 2 , 2 ) , &data );
- }
- CMulticast::InitWSAStatup ++;
- this->m_sock = INVALID_SOCKET;
- memset( &this->addr , 0 , sizeof( this->addr ) );
- memset( &this->command , 0 , sizeof( this->command ) );
- this->user_data = 0;
- memset( &this->recvBuffer , 0 , sizeof( this->recvBuffer ) );
- }
- CMulticast::~CMulticast()
- {
- CMulticast::InitWSAStatup -- ;
- if( ! CMulticast::InitWSAStatup )
- ::WSACleanup( );
- }
- //加入组播
- bool CMulticast::AddMemberShip( const char * ip , const int port )
- {
- this->DropMemberShip( );
- if( ( this->m_sock = ::socket( AF_INET , SOCK_DGRAM , 0 ) ) == INVALID_SOCKET )
- return false;
- this->addr.sin_family = AF_INET;
- this->addr.sin_addr.s_addr = INADDR_ANY;
- this->addr.sin_port = ::htons( port );
- int on = 1;
- //设定端口可重用
- if( ::setsockopt( this->m_sock , SOL_SOCKET , SO_REUSEADDR , ( const char * )&on , sizeof( on ) ) < 0 )
- {
- this->DropMemberShip( );
- return false;
- }//绑定端口
- if( ::bind( this->m_sock , ( struct sockaddr * )&this->addr , sizeof( this->addr ) ) == SOCKET_ERROR )
- {
- this->DropMemberShip( );
- return false;
- }
- this->command.imr_multiaddr.s_addr = inet_addr( ip );
- this->command.imr_interface.s_addr = INADDR_ANY;
- if( this->command.imr_multiaddr.s_addr == -1 )
- {
- this->DropMemberShip( );
- return false;
- }//加入组播
- if( ::setsockopt( this->m_sock , IPPROTO_IP , IP_ADD_MEMBERSHIP , ( const char * )&this->command , sizeof( this->command ) ) < 0 )
- {
- this->DropMemberShip( );
- return false;
- }//保存组播地址
- this->addr.sin_addr.s_addr = ::inet_addr( ip );
- return true;
- }
- //从组播中移出
- void CMulticast::DropMemberShip( void )
- {
- if( this->IsConnected( ) )
- { //移出租播
- ::setsockopt( this->m_sock , IPPROTO_IP , IP_DROP_MEMBERSHIP , ( const char * )&this->command , sizeof( this->command ) );
- ::closesocket( this->m_sock );
- this->m_sock = INVALID_SOCKET;
- }
- memset( &this->command , 0 , sizeof( this->command ) );
- }
- //发送组播数据
- bool CMulticast::SendTo( CBuffer & buffer )
- {
- if( this->IsConnected( ) )
- return ::sendto( this->m_sock , buffer.GetBuffer( ) , *( int * )buffer.GetBuffer( ) , 0 , ( struct sockaddr * )&this->addr , sizeof( this->addr ) );
- return false;
- }
- //接收组播数据
- bool CMulticast::ReceiveFrom( CBuffer & buffer )
- {
- if( ! this->IsConnected( ) )
- return false;
- struct sockaddr_in addr;
- int len = sizeof( addr );
- if( ::recvfrom( this->m_sock , this->recvBuffer , MAX_BUFFER_SIZE , 0 , ( struct sockaddr * )&addr , &len ) >= *( int * )this->recvBuffer )
- {
- if( buffer.GetSize( ) < *( int * )this->recvBuffer )
- buffer.Resize( *( int * )this->recvBuffer );
- memcpy( buffer.GetBuffer( ) , this->recvBuffer , *( int * )this->recvBuffer );
- return true;
- }
- return false;
- }