📄 clientsocket.cpp
字号:
// ClientSocket.cpp : implementation file
//
#include "stdafx.h"
#include "ClientSocket.h"
#include "head.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CClientSocket
//构造函数,初始化
CClientSocket::CClientSocket(CInterface *pInterFace,CMyWaveIn *pIn,CUdpSocket *pUdp):
m_bConnect(FALSE),
m_pInterface (NULL),
m_pIn(NULL),
m_pUdp (NULL)
{
m_pBuffer = new char[1024];
m_pInterface = pInterFace;
m_pIn = pIn;
m_pUdp = pUdp;
}
//析构函数
CClientSocket::~CClientSocket()
{
delete [1024]m_pBuffer;
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
//{{AFX_MSG_MAP(CClientSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions
void CClientSocket::OnClose(int nErrorCode)
{
//设置关闭标志
m_bConnect = FALSE;
//关闭
m_pInterface->BeClose ();
//调用基类OnClose()函数
CSocket::OnClose(nErrorCode);
}
void CClientSocket::OnReceive(int nErrorCode)
{
struct TalkFrame *frame;
frame = (struct TalkFrame *)m_pBuffer;
//接收缓存中的所有TalkFrame结构
int iLen = sizeof(struct TalkFrame);
while(iLen > 0)
{
//接收
int i = Receive (m_pBuffer +
sizeof(struct TalkFrame) - iLen,iLen);
//出错
if (i == SOCKET_ERROR )
return ;
iLen -= i;
}
//如果是非法数据就返回
if (strcmp(frame->cFlag ,"TalkFrame") != 0)
{
//返回
return;
}
//接收缓存中的所有音频数据
iLen = frame->iLen;
while (iLen > 0)
{
//接收
int i = Receive (m_pBuffer + sizeof(struct TalkFrame)
+ (frame->iLen - iLen),iLen);
//出错
if (i == SOCKET_ERROR )
return ;
//修改循环标志
iLen -= i;
}
//地址
CString add;
//端口
UINT port;
switch (frame->iCom )
{
//正常通信帧
case TC_NORMAL_TALK:
//初始化
memset(frame,0,sizeof (struct TalkFrame));
//设置数据帧标志
sprintf(frame->cFlag,"TalkFrame");
//获得与此套接字相连的地址和端口
GetPeerName (add,port);
//处于连接状态
if (m_pInterface ->IsConnect (add))
{
//设置通信帧标志及其长度
frame->iCom = TC_AGREE_TALK;
frame->iLen = 0;
//出错
if (SOCKET_ERROR == Send (m_pBuffer,sizeof(struct TalkFrame)))
{
break ;
};
//提示开始通信
m_pInterface->TalkStart (add);
//设置IP地址
m_pUdp->SetIp (add);
//允许发送
m_pIn->EnableSend (TRUE);
//允许工作
m_pInterface->m_bWork = TRUE;
break;
};
//设置通信帧标志
frame->iCom = TC_DISAGREE_TALK;
//设置通信帧长度
frame->iLen = 0;
//发送
Send (m_pBuffer,sizeof(struct TalkFrame));
//关闭
Close ();
break;
default:
break;
}
//调用基类OnReceive()函数
CSocket::OnReceive(nErrorCode);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -