1.需要一个回调函数NotifyProc
void CALLBACK CPcRemoteDlg::NotifyProc(LPVOID lpParam, ClientContext *pContext, UINT nCode)
{
try
{
switch (nCode)
{
case NC_CLIENT_CONNECT:
break;
case NC_CLIENT_DISCONNECT:
//g_pConnectView->PostMessage(WM_REMOVEFROMLIST, 0, (LPARAM)pContext);
break;
case NC_TRANSMIT:
break;
case NC_RECEIVE:
//ProcessReceive(pContext); //这里是有数据到来 但没有完全接收 大家可能会奇怪他怎么知道没有完全接收呢?
//回到OnClientReading 继续向下分析
break;
case NC_RECEIVE_COMPLETE:
//ProcessReceiveComplete(pContext); //就是这里 数据解压 还原后在调用 ProcessReceiveComplete 转到ProcessReceiveComplete
break;
}
}
catch (...) {}
}
2.复制Activate 代码 并处理
void CPcRemoteDlg::Activate(UINT nPort, UINT nMaxConnections)
{
CString str;
if (m_iocpServer != NULL)
{
m_iocpServer->Shutdown();
delete m_iocpServer;
}
m_iocpServer = new CIOCPServer;
lang2.1_8
// 开启IPCP服务器 最大连接 端口 查看NotifyProc回调函数 函数定义
if (m_iocpServer->Initialize(NotifyProc, NULL, 100000, nPort))
{
char hostname[256];
gethostname(hostname, sizeof(hostname));
HOSTENT *host = gethostbyname(hostname);
if (host != NULL)
{
for (int i = 0; ; i++)
{
str += inet_ntoa(*(IN_ADDR*)host->h_addr_list[i]);
if (host->h_addr_list[i] + host->h_length >= host->h_name)
break;
str += "/";
}
}
//m_wndStatusBar.SetPaneText(0, str);
//str.Format("端口: %d", nPort);
//m_wndStatusBar.SetPaneText(2, str);
str.Format("监听端口: %d成功", nPort);
ShowMessage(true, str);
}
else
{
//str.Format("端口%d绑定失败", nPort);
//m_wndStatusBar.SetPaneText(0, str);
//m_wndStatusBar.SetPaneText(2, "端口: 0");
str.Format("监听端口: %d失败", nPort);
ShowMessage(false, str);
}
}
3.初始化函数添加:
Activate(8000,9999);
4.测试 netstat -ano