📄 connpooltest.cpp
字号:
// connPoolTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "connPoolTest.h"
#include <afxsock.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
DWORD DoTest(LPVOID pD);
#define MY_SERVER_NAME "my_svr"
class CMyDisplay : public CFileOutputDisplay
{//显示类
public:
CMyDisplay(LPCSTR pszServerName,LPCSTR pszFileName):CFileOutputDisplay(pszServerName,pszFileName)
{};
~CMyDisplay(){};
public:
BOOL PutLine(ErrorLevel eLevel,LPCSTR pszOutput)
{
char szDesc[20];
GetErrorLevelString(eLevel,szDesc);
printf("%d %s %s\n",(int)eLevel,szDesc,pszOutput);
return CFileOutputDisplay::PutLine(eLevel,pszOutput);
};
};
CMyDisplay glo_display(MY_SERVER_NAME,"log\\log_");
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
ASSERT(AfxSocketInit());
DoTest(NULL);
}
return nRetCode;
}
DWORD DoTest(LPVOID pD)
{//测试功能
CSocketPoolAdmin pa(MY_SERVER_NAME,&glo_display);
CSocketConnPara cp[3];
cp[0].m_szRemoteAddr="127.0.0.1";
cp[0].m_iRemotePort=9988;
cp[1]=cp[0];
cp[2]=cp[0];
pa.InitAllConnectionPara(3,cp);
pa.GetSleepInterval()=1;
pa.CreateDaemonThread();
for(int i=0;i<60000;i++)
{
int iSocketID;
CSocketImplement* pS = pa.GetFreeSocket(iSocketID);
if(pS)
{
printf("get free SocketID = %d\n",iSocketID);
CTCPSocket* pSocket = pS->GetSocket();
int iRet = pSocket->TestRead(0);
if(iRet != SP_ERR_NODATA)
{
pa.SetErrorToSocket(iSocketID);
printf("\tsocket %d closed\n",iSocketID);
}
else
{//连接正常
BYTE bSend[10]="123456789";
int iWrote=0,iRead=0;
iRet = pSocket->Send(10,bSend,iWrote);
if(iWrote != 10)
{//发送失败,说明连接出现问题
pa.SetErrorToSocket(iSocketID);
printf("\t\tsocket %d sendError\n",iSocketID);
}
else
{//发送成功,准备接收
BYTE bRecv[10];
iRet = pSocket->RecvWithTimeOut(5,10,bRecv,iRead);
Sleep(1000*1);
if(iRead != 10)
{//接收失败,说明连接出现问题
pa.SetErrorToSocket(iSocketID);
printf("\t\t\tsocket %d recvError\n",iSocketID);
}
else
{//完成
pa.FreeSocket(iSocketID);
printf("socket %d send&recv OK\n",iSocketID);
}
}
}
}
else
{
printf("no free Socket\n");
}
Sleep(1000*2);
}
pa.StopDaemonThread();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -