socket一个函数中
BOOL CMySocket::OnMessagePending()
{
// TODO: 在此添加专用代码和/或调用基类
MSG msg;
if(::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER,PM_REMOVE))
{
if (msg.wParam == (UINT)m_uTimerID)
{
CancelBlockingCall();
return FALSE;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return CSocket::OnMessagePending();
}
这样可以连接上服务器,但是最多能连接上两台
要是改为
BOOL CMySocket::OnMessagePending()
{
// TODO: 在此添加专用代码和/或调用基类
MSG msg;
if(::PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
{
if (msg.wParam == WM_TIMER)
{
CancelBlockingCall();
return FALSE;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return CSocket::OnMessagePending();
}
这样的话就直接连不上服务器
服务器:
socket对应的函数OnMessagePending用哪种结果都是一样
有知道怎么才能多连的啊,两台以上的,
是不是上面的用法有问题?
不能连两台以上的原因是什么?
硬件没问题,MAC不冲突,ping地址都很正常
求解
14 个解决方案
#1
帮顶
#2
没人懂吗?
#3
BOOL CTimeOutSock::OnMessagePending()
{
// TODO: Add your specialized code here and/or call the base class
/*
MSG msg;
if(::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_NOREMOVE))
{
if (msg.wParam == (UINT) m_nTimerID)
{
// Remove the message and call CancelBlockingCall.
::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE);
CancelBlockingCall();
return FALSE; // No need for idle time processing.
};
};
*/
if( m_llDtStart )
{
LARGE_INTEGER lldtEnd;
::QueryPerformanceCounter(&lldtEnd);
LARGE_INTEGER llFrq;
::QueryPerformanceFrequency(&llFrq);
double dbDealy=(double)(lldtEnd.QuadPart-m_llDtStart)*1000/llFrq.QuadPart;
if( dbDealy>m_uTimeOut )
{
CancelBlockingCall();
return FALSE; // No need for idle time processing.
}
}
return CSocket::OnMessagePending();
}
WInsock
CSocket同步操作阻塞时设置超时的解决方案
#4
网络堵塞造成的?
#5
1>.\MySocket.cpp(204) : error C2065: 'm_llDtStart' : undeclared identifier
1>.\MySocket.cpp(211) : error C2065: 'm_uTimeOut' : undeclared identifier
m_llDtStart是什么标识?自定义?
m_uTimeOut?是不是就是(UINT) m_nTimerID?
1>.\MySocket.cpp(211) : error C2065: 'm_uTimeOut' : undeclared identifier
m_llDtStart是什么标识?自定义?
m_uTimeOut?是不是就是(UINT) m_nTimerID?
#6
LONGLONG m_llDtStart;
UINT m_uTimeOut;
BOOL CTimeOutSock::SetTimeOut(UINT uTimeOut)
{
//get start cnt
LARGE_INTEGER llCnt;
::QueryPerformanceCounter(&llCnt);
m_llDtStart=llCnt.QuadPart;
m_uTimeOut=uTimeOut;
return TRUE;
}
#7
结果还是最多只能两台连上,
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
只执行了两次
// MySocket.cpp : 实现文件
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "MySocket.h"
// CMySocket
CMySocket::CMySocket()
{
m_pMainDlg = NULL;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::CMySocket(CLiuHeCaiServerDlg *pMainDlg)
{
m_pMainDlg = pMainDlg;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::~CMySocket()
{
m_pMainDlg = NULL;
if(m_pstBodyRecv)
{
delete m_pstBodyRecv;
m_pstBodyRecv = NULL;
}
if(m_pstBodySend)
{
delete m_pstBodySend;
m_pstBodySend = NULL;
}
if(m_pstHeadRecv)
{
delete m_pstHeadRecv;
m_pstHeadRecv = NULL;
}
if(m_pstHeadSend)
{
delete m_pstHeadSend;
m_pstHeadSend = NULL;
}
if(m_pcRecvBuf)
{
delete [] m_pcRecvBuf;
m_pcRecvBuf = NULL;
}
}
BOOL CMySocket::SetTimeOut(const UINT uTimeOut)
{
LARGE_INTEGER llCnt;
::QueryPerformanceCounter(&llCnt);
m_llDtStart=llCnt.QuadPart;
m_uTimeOut=uTimeOut;
m_uTimerID = SetTimer(NULL, 0, uTimeOut, NULL);
if(m_uTimerID == 0)
{
return FALSE;
}
return TRUE;
}
BOOL CMySocket::KillTimeOut()
{
return KillTimer(NULL, m_uTimerID);
}
// CMySocket 成员函数
BOOL CMySocket::MyCreateClient()
{
return Create(CLIENT_PORT);
}
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
void CMySocket::OnReceive(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
static int iOneRecvCnt = 0;
static int iCurRecvCnt = 0;
static int iCurRecvStart = 0;
static const int iRecvBufLen = sizeof(char)*TEMPRECV_BUF;
static const int istBodyRecvLen = sizeof(SBodyRecv);
iCurRecvStart = iCurRecvCnt;
ZeroMemory(m_pcRecvBuf+iCurRecvStart, iRecvBufLen-iCurRecvStart);
iOneRecvCnt = Receive(m_pcRecvBuf+iCurRecvStart, SOCKETDEF_BUF);
if((iOneRecvCnt > SOCKETDEF_BUF) || (iOneRecvCnt <= 0))
{
int iLastError = WSAGetLastError();
TRACE1("CMySocket::OnReceive: Error %d.\n", iLastError);
iCurRecvCnt = 0;
iCurRecvStart = 0;
ZeroMemory(m_pcRecvBuf, iRecvBufLen);
OnClose(iLastError);
return ;
}
iCurRecvCnt += iOneRecvCnt;
m_pcRecvBuf[iCurRecvCnt] = 0;
int iCpyStart = 0;
while(iCurRecvCnt >= istBodyRecvLen)
{
memcpy(m_pstBodyRecv, m_pcRecvBuf+iCpyStart, istBodyRecvLen);
iCurRecvCnt -= istBodyRecvLen;
iCpyStart += istBodyRecvLen;
m_pMainDlg->HealRecvMsg(this);
}
memcpy(m_pcRecvBuf, m_pcRecvBuf+iCpyStart, iCurRecvCnt);
CSocket::OnReceive(nErrorCode);
}
void CMySocket::OnClose(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
CSocket::OnClose(nErrorCode);
m_pMainDlg->CloseSocket(this);
}
BOOL CMySocket::OnMessagePending()
{
if( m_llDtStart )
{
LARGE_INTEGER lldtEnd;
::QueryPerformanceCounter(&lldtEnd);
LARGE_INTEGER llFrq;
::QueryPerformanceFrequency(&llFrq);
double dbDealy=(double)(lldtEnd.QuadPart-m_llDtStart)*1000/llFrq.QuadPart;
if( dbDealy>m_uTimeOut )
{
CancelBlockingCall();
return FALSE; // No need for idle time processing.
}
}
return CSocket::OnMessagePending();
}
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
只执行了两次
// MySocket.cpp : 实现文件
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "MySocket.h"
// CMySocket
CMySocket::CMySocket()
{
m_pMainDlg = NULL;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::CMySocket(CLiuHeCaiServerDlg *pMainDlg)
{
m_pMainDlg = pMainDlg;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::~CMySocket()
{
m_pMainDlg = NULL;
if(m_pstBodyRecv)
{
delete m_pstBodyRecv;
m_pstBodyRecv = NULL;
}
if(m_pstBodySend)
{
delete m_pstBodySend;
m_pstBodySend = NULL;
}
if(m_pstHeadRecv)
{
delete m_pstHeadRecv;
m_pstHeadRecv = NULL;
}
if(m_pstHeadSend)
{
delete m_pstHeadSend;
m_pstHeadSend = NULL;
}
if(m_pcRecvBuf)
{
delete [] m_pcRecvBuf;
m_pcRecvBuf = NULL;
}
}
BOOL CMySocket::SetTimeOut(const UINT uTimeOut)
{
LARGE_INTEGER llCnt;
::QueryPerformanceCounter(&llCnt);
m_llDtStart=llCnt.QuadPart;
m_uTimeOut=uTimeOut;
m_uTimerID = SetTimer(NULL, 0, uTimeOut, NULL);
if(m_uTimerID == 0)
{
return FALSE;
}
return TRUE;
}
BOOL CMySocket::KillTimeOut()
{
return KillTimer(NULL, m_uTimerID);
}
// CMySocket 成员函数
BOOL CMySocket::MyCreateClient()
{
return Create(CLIENT_PORT);
}
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
void CMySocket::OnReceive(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
static int iOneRecvCnt = 0;
static int iCurRecvCnt = 0;
static int iCurRecvStart = 0;
static const int iRecvBufLen = sizeof(char)*TEMPRECV_BUF;
static const int istBodyRecvLen = sizeof(SBodyRecv);
iCurRecvStart = iCurRecvCnt;
ZeroMemory(m_pcRecvBuf+iCurRecvStart, iRecvBufLen-iCurRecvStart);
iOneRecvCnt = Receive(m_pcRecvBuf+iCurRecvStart, SOCKETDEF_BUF);
if((iOneRecvCnt > SOCKETDEF_BUF) || (iOneRecvCnt <= 0))
{
int iLastError = WSAGetLastError();
TRACE1("CMySocket::OnReceive: Error %d.\n", iLastError);
iCurRecvCnt = 0;
iCurRecvStart = 0;
ZeroMemory(m_pcRecvBuf, iRecvBufLen);
OnClose(iLastError);
return ;
}
iCurRecvCnt += iOneRecvCnt;
m_pcRecvBuf[iCurRecvCnt] = 0;
int iCpyStart = 0;
while(iCurRecvCnt >= istBodyRecvLen)
{
memcpy(m_pstBodyRecv, m_pcRecvBuf+iCpyStart, istBodyRecvLen);
iCurRecvCnt -= istBodyRecvLen;
iCpyStart += istBodyRecvLen;
m_pMainDlg->HealRecvMsg(this);
}
memcpy(m_pcRecvBuf, m_pcRecvBuf+iCpyStart, iCurRecvCnt);
CSocket::OnReceive(nErrorCode);
}
void CMySocket::OnClose(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
CSocket::OnClose(nErrorCode);
m_pMainDlg->CloseSocket(this);
}
BOOL CMySocket::OnMessagePending()
{
if( m_llDtStart )
{
LARGE_INTEGER lldtEnd;
::QueryPerformanceCounter(&lldtEnd);
LARGE_INTEGER llFrq;
::QueryPerformanceFrequency(&llFrq);
double dbDealy=(double)(lldtEnd.QuadPart-m_llDtStart)*1000/llFrq.QuadPart;
if( dbDealy>m_uTimeOut )
{
CancelBlockingCall();
return FALSE; // No need for idle time processing.
}
}
return CSocket::OnMessagePending();
}
#8
求解 - -
#9
继续求解中
#10
帮顶下
呵呵~~~
呵呵~~~
#11
帮顶下
呵呵~~~
呵呵~~~
#12
自己顶一下
求解ing............
求解ing............
#13
求解.............\
没人知道吗?
几天过去了
没人知道吗?
几天过去了
#14
无法解决结贴
#1
帮顶
#2
没人懂吗?
#3
BOOL CTimeOutSock::OnMessagePending()
{
// TODO: Add your specialized code here and/or call the base class
/*
MSG msg;
if(::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_NOREMOVE))
{
if (msg.wParam == (UINT) m_nTimerID)
{
// Remove the message and call CancelBlockingCall.
::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE);
CancelBlockingCall();
return FALSE; // No need for idle time processing.
};
};
*/
if( m_llDtStart )
{
LARGE_INTEGER lldtEnd;
::QueryPerformanceCounter(&lldtEnd);
LARGE_INTEGER llFrq;
::QueryPerformanceFrequency(&llFrq);
double dbDealy=(double)(lldtEnd.QuadPart-m_llDtStart)*1000/llFrq.QuadPart;
if( dbDealy>m_uTimeOut )
{
CancelBlockingCall();
return FALSE; // No need for idle time processing.
}
}
return CSocket::OnMessagePending();
}
WInsock
CSocket同步操作阻塞时设置超时的解决方案
#4
网络堵塞造成的?
#5
1>.\MySocket.cpp(204) : error C2065: 'm_llDtStart' : undeclared identifier
1>.\MySocket.cpp(211) : error C2065: 'm_uTimeOut' : undeclared identifier
m_llDtStart是什么标识?自定义?
m_uTimeOut?是不是就是(UINT) m_nTimerID?
1>.\MySocket.cpp(211) : error C2065: 'm_uTimeOut' : undeclared identifier
m_llDtStart是什么标识?自定义?
m_uTimeOut?是不是就是(UINT) m_nTimerID?
#6
LONGLONG m_llDtStart;
UINT m_uTimeOut;
BOOL CTimeOutSock::SetTimeOut(UINT uTimeOut)
{
//get start cnt
LARGE_INTEGER llCnt;
::QueryPerformanceCounter(&llCnt);
m_llDtStart=llCnt.QuadPart;
m_uTimeOut=uTimeOut;
return TRUE;
}
#7
结果还是最多只能两台连上,
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
只执行了两次
// MySocket.cpp : 实现文件
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "MySocket.h"
// CMySocket
CMySocket::CMySocket()
{
m_pMainDlg = NULL;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::CMySocket(CLiuHeCaiServerDlg *pMainDlg)
{
m_pMainDlg = pMainDlg;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::~CMySocket()
{
m_pMainDlg = NULL;
if(m_pstBodyRecv)
{
delete m_pstBodyRecv;
m_pstBodyRecv = NULL;
}
if(m_pstBodySend)
{
delete m_pstBodySend;
m_pstBodySend = NULL;
}
if(m_pstHeadRecv)
{
delete m_pstHeadRecv;
m_pstHeadRecv = NULL;
}
if(m_pstHeadSend)
{
delete m_pstHeadSend;
m_pstHeadSend = NULL;
}
if(m_pcRecvBuf)
{
delete [] m_pcRecvBuf;
m_pcRecvBuf = NULL;
}
}
BOOL CMySocket::SetTimeOut(const UINT uTimeOut)
{
LARGE_INTEGER llCnt;
::QueryPerformanceCounter(&llCnt);
m_llDtStart=llCnt.QuadPart;
m_uTimeOut=uTimeOut;
m_uTimerID = SetTimer(NULL, 0, uTimeOut, NULL);
if(m_uTimerID == 0)
{
return FALSE;
}
return TRUE;
}
BOOL CMySocket::KillTimeOut()
{
return KillTimer(NULL, m_uTimerID);
}
// CMySocket 成员函数
BOOL CMySocket::MyCreateClient()
{
return Create(CLIENT_PORT);
}
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
void CMySocket::OnReceive(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
static int iOneRecvCnt = 0;
static int iCurRecvCnt = 0;
static int iCurRecvStart = 0;
static const int iRecvBufLen = sizeof(char)*TEMPRECV_BUF;
static const int istBodyRecvLen = sizeof(SBodyRecv);
iCurRecvStart = iCurRecvCnt;
ZeroMemory(m_pcRecvBuf+iCurRecvStart, iRecvBufLen-iCurRecvStart);
iOneRecvCnt = Receive(m_pcRecvBuf+iCurRecvStart, SOCKETDEF_BUF);
if((iOneRecvCnt > SOCKETDEF_BUF) || (iOneRecvCnt <= 0))
{
int iLastError = WSAGetLastError();
TRACE1("CMySocket::OnReceive: Error %d.\n", iLastError);
iCurRecvCnt = 0;
iCurRecvStart = 0;
ZeroMemory(m_pcRecvBuf, iRecvBufLen);
OnClose(iLastError);
return ;
}
iCurRecvCnt += iOneRecvCnt;
m_pcRecvBuf[iCurRecvCnt] = 0;
int iCpyStart = 0;
while(iCurRecvCnt >= istBodyRecvLen)
{
memcpy(m_pstBodyRecv, m_pcRecvBuf+iCpyStart, istBodyRecvLen);
iCurRecvCnt -= istBodyRecvLen;
iCpyStart += istBodyRecvLen;
m_pMainDlg->HealRecvMsg(this);
}
memcpy(m_pcRecvBuf, m_pcRecvBuf+iCpyStart, iCurRecvCnt);
CSocket::OnReceive(nErrorCode);
}
void CMySocket::OnClose(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
CSocket::OnClose(nErrorCode);
m_pMainDlg->CloseSocket(this);
}
BOOL CMySocket::OnMessagePending()
{
if( m_llDtStart )
{
LARGE_INTEGER lldtEnd;
::QueryPerformanceCounter(&lldtEnd);
LARGE_INTEGER llFrq;
::QueryPerformanceFrequency(&llFrq);
double dbDealy=(double)(lldtEnd.QuadPart-m_llDtStart)*1000/llFrq.QuadPart;
if( dbDealy>m_uTimeOut )
{
CancelBlockingCall();
return FALSE; // No need for idle time processing.
}
}
return CSocket::OnMessagePending();
}
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
只执行了两次
// MySocket.cpp : 实现文件
//
#include "stdafx.h"
#include "Server.h"
#include "ServerDlg.h"
#include "MySocket.h"
// CMySocket
CMySocket::CMySocket()
{
m_pMainDlg = NULL;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::CMySocket(CLiuHeCaiServerDlg *pMainDlg)
{
m_pMainDlg = pMainDlg;
m_pcRecvBuf = NULL;
m_pstHeadSend = NULL;
m_pstHeadRecv = NULL;
m_pstBodySend = NULL;
m_pstBodyRecv = NULL;
m_strConnIP = _T("127.0.0.1");
m_uTimerID = 0;
m_llDtStart=0;
m_uTimeOut=0;
NewMyData(&m_pcRecvBuf, TEMPRECV_BUF);
ASSERT(m_pcRecvBuf);
NewMyData(&m_pstHeadSend, 1);
ASSERT(m_pstHeadSend);
NewMyData(&m_pstHeadRecv, 1);
ASSERT(m_pstHeadRecv);
NewMyData(&m_pstBodySend, 1);
ASSERT(m_pstBodySend);
NewMyData(&m_pstBodyRecv, 1);
ASSERT(m_pstBodyRecv);
}
CMySocket::~CMySocket()
{
m_pMainDlg = NULL;
if(m_pstBodyRecv)
{
delete m_pstBodyRecv;
m_pstBodyRecv = NULL;
}
if(m_pstBodySend)
{
delete m_pstBodySend;
m_pstBodySend = NULL;
}
if(m_pstHeadRecv)
{
delete m_pstHeadRecv;
m_pstHeadRecv = NULL;
}
if(m_pstHeadSend)
{
delete m_pstHeadSend;
m_pstHeadSend = NULL;
}
if(m_pcRecvBuf)
{
delete [] m_pcRecvBuf;
m_pcRecvBuf = NULL;
}
}
BOOL CMySocket::SetTimeOut(const UINT uTimeOut)
{
LARGE_INTEGER llCnt;
::QueryPerformanceCounter(&llCnt);
m_llDtStart=llCnt.QuadPart;
m_uTimeOut=uTimeOut;
m_uTimerID = SetTimer(NULL, 0, uTimeOut, NULL);
if(m_uTimerID == 0)
{
return FALSE;
}
return TRUE;
}
BOOL CMySocket::KillTimeOut()
{
return KillTimer(NULL, m_uTimerID);
}
// CMySocket 成员函数
BOOL CMySocket::MyCreateClient()
{
return Create(CLIENT_PORT);
}
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
m_pMainDlg->AcceptSocket();
CSocket::OnAccept(nErrorCode);
}
void CMySocket::OnReceive(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
static int iOneRecvCnt = 0;
static int iCurRecvCnt = 0;
static int iCurRecvStart = 0;
static const int iRecvBufLen = sizeof(char)*TEMPRECV_BUF;
static const int istBodyRecvLen = sizeof(SBodyRecv);
iCurRecvStart = iCurRecvCnt;
ZeroMemory(m_pcRecvBuf+iCurRecvStart, iRecvBufLen-iCurRecvStart);
iOneRecvCnt = Receive(m_pcRecvBuf+iCurRecvStart, SOCKETDEF_BUF);
if((iOneRecvCnt > SOCKETDEF_BUF) || (iOneRecvCnt <= 0))
{
int iLastError = WSAGetLastError();
TRACE1("CMySocket::OnReceive: Error %d.\n", iLastError);
iCurRecvCnt = 0;
iCurRecvStart = 0;
ZeroMemory(m_pcRecvBuf, iRecvBufLen);
OnClose(iLastError);
return ;
}
iCurRecvCnt += iOneRecvCnt;
m_pcRecvBuf[iCurRecvCnt] = 0;
int iCpyStart = 0;
while(iCurRecvCnt >= istBodyRecvLen)
{
memcpy(m_pstBodyRecv, m_pcRecvBuf+iCpyStart, istBodyRecvLen);
iCurRecvCnt -= istBodyRecvLen;
iCpyStart += istBodyRecvLen;
m_pMainDlg->HealRecvMsg(this);
}
memcpy(m_pcRecvBuf, m_pcRecvBuf+iCpyStart, iCurRecvCnt);
CSocket::OnReceive(nErrorCode);
}
void CMySocket::OnClose(int nErrorCode)
{
// TODO: 在此添加专用代码和/或调用基类
CSocket::OnClose(nErrorCode);
m_pMainDlg->CloseSocket(this);
}
BOOL CMySocket::OnMessagePending()
{
if( m_llDtStart )
{
LARGE_INTEGER lldtEnd;
::QueryPerformanceCounter(&lldtEnd);
LARGE_INTEGER llFrq;
::QueryPerformanceFrequency(&llFrq);
double dbDealy=(double)(lldtEnd.QuadPart-m_llDtStart)*1000/llFrq.QuadPart;
if( dbDealy>m_uTimeOut )
{
CancelBlockingCall();
return FALSE; // No need for idle time processing.
}
}
return CSocket::OnMessagePending();
}
#8
求解 - -
#9
继续求解中
#10
帮顶下
呵呵~~~
呵呵~~~
#11
帮顶下
呵呵~~~
呵呵~~~
#12
自己顶一下
求解ing............
求解ing............
#13
求解.............\
没人知道吗?
几天过去了
没人知道吗?
几天过去了
#14
无法解决结贴