本文整理汇总了C++中CBaseSocket::SetBlockMode方法的典型用法代码示例。如果您正苦于以下问题:C++ CBaseSocket::SetBlockMode方法的具体用法?C++ CBaseSocket::SetBlockMode怎么用?C++ CBaseSocket::SetBlockMode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CBaseSocket
的用法示例。
在下文中一共展示了CBaseSocket::SetBlockMode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: AddClientChannel
/********************************************************************************
Function: AddClientChannel
Description: 增加一个客户端
Input: pszhost 主机地址 unDesPort 目的端口 pszBindIP 本地IP地址 可以是0 表示本机的任意IP localport 本地端口 可以为0 系统自动分配
Output: pSocketChnl 成功返回通道指针
Return:
Note:
Author: CHC
Date: 2010/08/31
********************************************************************************/
INT CNetService::AddClientChannel( const char *pszhost, UINT16 unDesPort,const char *pszBindIP,
UINT16 localport,enumNetProtocolType eProtocolType, ISocketChannel** pSocketChnl)
{
if ( m_bIsExit )
{
return ERROR_NET_UNKNOWN;
}
if ( !m_bIsInit )
{
return ERROR_NET_UNKNOWN;
}
CGSAutoMutex GSAutoMutex(&m_GSMutex);
// 为了区分协议栈和gsp,得这么写
/*if (!g_clsLogPtr)
{
m_clsThreadDealNetEvent.SetLogPath( GetApplicationPath().c_str() );
m_clsThreadDealNetEvent.m_clsThreadPool.SetLogInstancePtr( m_clsThreadDealNetEvent.GetLogInstancePtr());
}*/
if (true == m_clsThreadDealNetEvent.IfMaxChannelCount())
{
return ERROR_NET_MAX_CHANNEL_COUNT;//已达到最大连接数
}
if (pszhost == NULL)
{
return ERROR_NET_PARAM_WRONG;
}
//建立socketchannel
CSocketChannel *pclsSocketChnl = new CSocketChannel();//此指针保存到队列中,直到通道需要删除时才释放,比如程序退出,或者上层请求关闭通道
if ( NULL == pclsSocketChnl )
{
// 没申请到内存
return ERROR_NET_UNKNOWN;
}
pclsSocketChnl->SetLocalIPPort(pszBindIP, localport);
pclsSocketChnl->SetReMoteIPPort(pszhost, unDesPort);
pclsSocketChnl->SetNetProtocolType( eProtocolType );
pclsSocketChnl->SetServerType( CLIENT );
pclsSocketChnl->SetChannelType( COMM_CHANNEL );//客户端建立的都是通讯通道
//建立basesocket
CBaseSocket* pBaseSocket = NULL;//生命周期和pclsSocketChnl一样,
#if _WIN32
if (eProtocolType == NET_PROTOCOL_TCP)
{
pBaseSocket = new CWinTcpSocket();
}
else
{
pBaseSocket = new CWinUdpSocket();
}
#endif
#if _LINUX
if (eProtocolType == NET_PROTOCOL_TCP)
{
pBaseSocket = new CLinuxTcpSocket();
}
else
{
pBaseSocket = new CLinuxUdpSocket();
}
#endif
if ( NULL == pBaseSocket )
{
// 没申请到内存
return ERROR_NET_UNKNOWN;
}
pBaseSocket->SetBlockMode( m_clsThreadDealNetEvent.GetBlockMode() );
m_clsClientChannel.SetLocalIPPort(pszBindIP, localport);
m_clsClientChannel.SetReMoteIPPort(pszhost,unDesPort);
//创建通道
if (m_clsClientChannel.CreateChannel(pBaseSocket) != ERROR_BASE_SUCCESS)
{
//创建 通道失败
pclsSocketChnl->CloseChannel();
delete pclsSocketChnl;
pclsSocketChnl = NULL;
//.........这里部分代码省略.........
示例2: AddServerChannel
/*************************************************
Function: AddServerChannel
Description: 建立通讯通道,
Input:
Output:
Return:
Note: 对于
*************************************************/
INT CNetService::AddServerChannel(const char* pszBindIP, UINT16 unPort,
enumNetProtocolType eProtocolType, ISocketChannel** pSocketChnl)
{
CGSAutoMutex GSAutoMutex(&m_GSMutex);
if ( !m_bIsInit )
{
return ERROR_NET_UNKNOWN;
}
// 为了区分协议栈和gsp,得这么写
/*if (!g_clsLogPtr)
{
m_clsThreadDealNetEvent.SetLogPath( GetApplicationPath().c_str() );
m_clsThreadDealNetEvent.m_clsThreadPool.SetLogInstancePtr( m_clsThreadDealNetEvent.GetLogInstancePtr());
}*/
if (true == m_clsThreadDealNetEvent.IfMaxChannelCount())
{
return ERROR_NET_MAX_CHANNEL_COUNT;//已达到最大连接数
}
if (unPort < 0) //绑定的IP为null时 表示绑定本机任意IP
{
return ERROR_NET_PARAM_WRONG;
}
//建立socketchannel
CSocketChannel *pclsSocketChnl = new CSocketChannel();//此指针保存到队列中,直到通道需要删除时才释放.在~CNetInterfaceCommData(void)里释放
if ( NULL == pclsSocketChnl )
{
return ERROR_NET_UNKNOWN;
}
//设置通道属性
pclsSocketChnl->SetLocalIPPort(pszBindIP, unPort);
pclsSocketChnl->SetServerType( SERVER );
pclsSocketChnl->SetNetProtocolType( eProtocolType );
pclsSocketChnl->SetChannelType( LISTEN_CHANNEL );//不管是UDP还是tcp 凡是AddServerChannel add的通道都认为是监听通道
//建立basesocket
CBaseSocket* pBaseSocket = NULL;//生命周期和pclsSocketChnl一样,
#if _WIN32
if (eProtocolType == NET_PROTOCOL_TCP)
{
pBaseSocket = new CWinTcpSocket();
}
else
{
pBaseSocket = new CWinUdpSocket();
}
#endif
#if _LINUX
if (eProtocolType == NET_PROTOCOL_TCP)
{
pBaseSocket = new CLinuxTcpSocket();
}
else
{
pBaseSocket = new CLinuxUdpSocket();
}
#endif
if ( NULL == pBaseSocket )
{
if ( pclsSocketChnl )
{
pclsSocketChnl->CloseChannel();
delete pclsSocketChnl;
pclsSocketChnl = NULL;
}
return ERROR_NET_UNKNOWN;
}
pBaseSocket->SetBlockMode( m_clsThreadDealNetEvent.GetBlockMode() );
m_clsServerChannel.SetLocalIPPort(pszBindIP, unPort);
//创建通道
if (m_clsServerChannel.CreateChannel(pBaseSocket) != ERROR_BASE_SUCCESS)
{
//创建 通道失败
pclsSocketChnl->CloseChannel();
delete pclsSocketChnl;
pclsSocketChnl = NULL;
delete pBaseSocket;
pBaseSocket = NULL;
//.........这里部分代码省略.........