typedef struct
{
//完成端口数据包结构
OVERLAPPED ol;
WSABUF data;
char buf[DATA_SIZE];
sockaddr_in addr;
OP_TYPE opType;
} PER_IO_DATA,*LPPER_IO_DATA;
(2)在服务器主窗口类CMY_UdpIocpSrvDlg中创建套接字及完成端口并进行绑定
//创建一个套接字并判断是否成功
int ret;
udpSocket = socket(AF_INET,SOCK_DGRAM,0);
if (udpSocket == SOCKET_ERROR)
{
TRACE("err code=%d\n",GetLastError());
return;
}
DWORD dwBytesReturned = 0;
BOOL bNewBehavior = FALSE;
WSAIoctl(udpSocket, SIO_UDP_CONNRESET,
&bNewBehavior, sizeof(bNewBehavior),
NULL, 0, &dwBytesReturned,
NULL, NULL);
//设置服务器地址
sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(m_port);
//设置服务器端口可重复利用
int opt = 1;本文来自优.文~论^文·网原文请找腾讯3249.114
ret=setsockopt(udpSocket,SOL_SOCKET,SO_REUSEADDR,(constchar*)&opt,sizeof(opt));
if (SOCKET_ERROR == ret)
{
TRACE("err code=%d\n",GetLastError());
return;
}论文网http://www.youerw.com/
//绑定套接字
ret = bind(udpSocket,(sockaddr*)&servaddr,sizeof(servaddr));
if (SOCKET_ERROR == ret)
{
TRACE("err code=%d\n",GetLastError());
return;
}
//创建完成端口
hIocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE,NULL,NULL,0);
if (hIocp == NULL)
{
TRACE("err code=%d\n",GetLastError());
return;
}
//将套接字与完成端口绑定
if (hIocp != CreateIoCompletionPort((HANDLE)udpSocket,hIocp,0,0))
{
TRACE("err code=%d\n",GetLastError());
<< 上一页 [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] ... 下一页 >>