3.2.2 接收请求模块 接收请求模块的主要功能是监听网络上的广播请求,验证请求包的正确性,将请求信息写入请求队列。 内容来自论文无忧网 www.paper51.com
模块流程图如下: copyright paper51.com
图3 接受请求模块流程图 内容来自论文无忧网 www.paper51.com
主要实现代码: 内容来自www.paper51.com
if((nbSize=recvfrom(sockListener,buf,sizeof(buf),0,(SOCKADDRFAR *)&saClient,&nSize))==SOCKET_ERROR) //没有数据到达,线程将在此处阻塞 http://www.paper51.com { copyright paper51.com MessageBox(NULL,"receivepackage fail!","Error!",MB_OK); http://www.paper51.com Log.WriteLog("receive packagefail!"); 内容来自论文无忧网 www.paper51.com continue; copyright paper51.com
} http://www.paper51.com
内容来自论文无忧网 www.paper51.com chartemp[6];//用于提取包头 copyright paper51.com memcpy(temp,buf,5); http://www.paper51.com temp[5]=0; 内容来自www.paper51.com if(strcmp(temp,(char*)(LPCTSTR)PakHead)==0)//为第一次请求包 http://www.paper51.com
{ 内容来自www.paper51.com //请求信息写入结点 copyright paper51.com memcpy(&pak,buf,npak); paper51.com memcpy(addr.mac,pak.macaddr,12); http://www.paper51.com addr.mac[12]=0; 内容来自论文无忧网 www.paper51.com strcpy(addr.pcname,pak.body); 内容来自www.paper51.com strcpy(addr.ip,inet_ntoa(saClient.sin_addr)); copyright paper51.com
CString macaddr="receive a request, theMacaddress is "; 内容来自www.paper51.com CString s=addr.mac; 内容来自论文无忧网 www.paper51.com
macaddr=macaddr+s; http://www.paper51.com Log.WriteLog((char*)(LPCTSTR)macaddr);//写日志 内容来自www.paper51.com
if(Write()==false)//将请求信息存放在结点后写入请求队列 内容来自论文无忧网 www.paper51.com { 内容来自论文无忧网 www.paper51.com MessageBox(NULL,"write toqueue fail!","Error!",MB_OK); 内容来自论文无忧网 www.paper51.com Log.WriteLog("write to queuefail!"); paper51.com } http://www.paper51.com recptr->AddString(macaddr); //控件上显示请求信息 paper51.com } 内容来自www.paper51.com
if(strcmp(temp,"REQIP")==0)//二次请求包 paper51.com
{ 内容来自www.paper51.com
//请求信息写入结点 内容来自论文无忧网 www.paper51.com memcpy(&pak,buf,npak); copyright paper51.com strcpy(addr.mac,"0"); http://www.paper51.com strcpy(addr.pcname,pak.body); paper51.com
strcpy(addr.ip,inet_ntoa(saClient.sin_addr)); paper51.com CString macaddr="warning ! receive arequest, the Macaddress is "; 内容来自www.paper51.com CStrings=pak.macaddr; paper51.com macaddr=macaddr+s; paper51.com Log.WriteLog((char*)(LPCTSTR)macaddr); copyright paper51.com
if(Write()==false)//将请求信息写入队列 内容来自论文无忧网 www.paper51.com { paper51.com
MessageBox(NULL,"writeto queue fail!","Error!",MB_OK); paper51.com
Log.WriteLog("writeto queue fail!"); http://www.paper51.com
} http://www.paper51.com recptr->AddString(macaddr); //控件上显示请求信息 内容来自www.paper51.com 3.2.3 处理请求模块 内容来自论文无忧网 www.paper51.com 处理模块请求主要功能是为客户端分配对应的IP地址,通过查询数据库为客户端分配固定的IP地址。 copyright paper51.com 模块流程图如下: 内容来自论文无忧网 www.paper51.com 图4 处理请求模块流程图 copyright paper51.com |