程序主要功能:
1,使用.net 3.5 SocketAsyncEventArgs类,创建异步Socket的完成端口模型
2,使用自定义PackTask 类处理TCP粘包问题
3,使用事件将处理过的原始封包投递给事件绑定的方法
程序类结构如下:
使用示例:
NetServer^ netserver=gcnew NetServer(nport);
netserver->ServerEvent+=gcnew TaskEventHandler(this,&Form1::NetServerProc); //创建监听
if (!netserver->Start())
{
MessageBox::Show("监听GPRS建立失败!","错误",MessageBoxButtons::OK,MessageBoxIcon::Error);
return;
}
/**********************************************/
void NetServerProc(PackTaskEventArgs^ e)
{
switch(e->Type)
{
case TaskType::Connectioned:
//... e->Iep
break;
case TaskType::DisConnectioned:
//... e->Iep
break;
case TaskType::Receive:
//... e->Buff
break;
case TaskType::Exception:
Console::WriteLine(L"error:{0}",e->Message);
break;
case TaskType::DebugRecv:
Console::WriteLine(L"debug");
break;
}
}
netserver->ServerEvent+=gcnew TaskEventHandler(this,&Form1::NetServerProc); //创建监听
if (!netserver->Start())
{
MessageBox::Show("监听GPRS建立失败!","错误",MessageBoxButtons::OK,MessageBoxIcon::Error);
return;
}
/**********************************************/
void NetServerProc(PackTaskEventArgs^ e)
{
switch(e->Type)
{
case TaskType::Connectioned:
//... e->Iep
break;
case TaskType::DisConnectioned:
//... e->Iep
break;
case TaskType::Receive:
//... e->Buff
break;
case TaskType::Exception:
Console::WriteLine(L"error:{0}",e->Message);
break;
case TaskType::DebugRecv:
Console::WriteLine(L"debug");
break;
}
}