《一个简单的UDPServer实现 编程知识点梳理汇总.docx》由会员分享,可在线阅读,更多相关《一个简单的UDPServer实现 编程知识点梳理汇总.docx(9页珍藏版)》请在第一文库网上搜索。
1、一个简单的UDPServer实现一、思路1、建立接收SOCket,将SOCket和指定的端口绑定2、创建接收线程,在线程中调用ioct1socket()判断是否接收到数据,接收到数据时调用OnReCeiVe()(类似CSocket中的OnReceiveO)3、在OnReceive中申请空间调用recvfrom接收数据4、建立发送SoCket,和INADDR_ANY绑定5、调用Sendto发送数据二、本代码直接使用SocketAPI实现UDP通讯相关的函数有:WSAStartup(WORDwVersionRequested,1PWSADATAIPWSAData);初始化socket库WSAC1e
2、anUP();释放socket库socket(intaf,inttype,intProtoCo1);建立socketsetsockopt(SOCKETs,intIeve1jntoptname,constcharFAR*optva1,intOPUen);设置SOCket或者协议的属性bind(SOCKETs,conststructsockaddrFAR*name,intname1en);地址和socket绑定sendto(SOCKETs,constcharFAR*buf,intIenJntf1ags,conststructsockaddrFAR*to,intto1en);发送消息ioct1soc
3、ket(SOCKETs,1ongcmd,ujongFAR*argp);获取socket状态,可以获取socket缓冲中数据长度,利用这个函数可以实现类似CSOCket中的OnReCeiVe()函数(在接收数据之前调用)recvfrom(SOCKETs,charFAR*buf,int1en,intf1ags,structsockaddrFAR*from,intFAR*from1en);/接收数据三、代码说明1、UDPServenh和UDPServencpp是实现类CUDPServer的头文件和实现文件,udpdemo.cpp是演示程序2、编译环境在Win2000vc6.0下编译通过3、编译时需要
4、在vc的project的setting中连接WS2_32.1ib库/UDPServenh:interfacefortheCUDPServerc1ass./I1H11HHini11111111H1111111iui111111111111111111111IIHIIHHini#if!defined(UDPSERVER_HJNC1UDEDJ#defineUDPSERVER_HNC1UDED_#if_MSC_VER1000#pragmaonce#endif/MSCVER1000#inc1ude/WS2_32JibC1OSE.SOCKET(X)#defineif(X!=INVA1!D_SOCKET)c
5、1osesocket(X);X=INVA11D_SOCKET;)c1assCUDPServer(private:charm-sz1oca1IP20;charm_szHostName30J;intm_RecvPort;charm-szRemoteIP20;intm_RemotePort;HAND1Em_RecvThread;DWORDm_ThreadID;private:voidInitMemberVariab1eO;pub1ic:SOCKETm_SckRecive;SOCKETm_SckSend;boo1m_bReciveF1ag;CUDPServer();virtua1-CUDPServer
6、();voidInitia1izeO;voidDestory();初始化socket1ibboo1InitSocket1ib(WORDwVersion=0x0202);voidC1eanSocket1ibO;创建发送socketboo1CreateSendSocketO;创建接收socketboo1CreateRecvSocket(intRecvPort=9527);/发送intSendMsg(char*szBuf,int1ength,char*Szremote1P=NU11Jntport=0);启动接收线程boo1StartRecv();/停止接收线程boo1StopRecv();接收线程函
7、数,staticDWORDW1NAPIReceiveThread(1PVOIDIpParameter);/线程函数当有数据时,调用OnReCeiVeJength表示数据长度voidOnReceive(1ong1ength);staticDWORDW1NAPIOnReceiveThread(1PVOIDIpParameter);/线程函数#endif/!defined(UDPSERVER_H_INC1UDED_)/UDPServerxpp:imp1ementationoftheCUDPServerc1ass./Iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
8、iiiiiiiiiiiiiiiiiiiiiiiiiii#inc1ude,UDPServer.h#inc1udeIiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii/ConstructionZDestructionIiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiCUDPServer:CUDPServer()Initia1ize();CUDPSerVer:CUDPServer()Destory();voidCUDPSer
9、VerInitia1ize。(InitSocket1ib(0x0202);InitMemberVariab1eO;voidCUDPSerVer:Des1ory()(if(m_bReciveF1ag)(StopRecv();)C1eanSocket1ibO;)voidCUDPSerVer:InitMemberVariab1e。(memset(m_sz1oca11P,0,20);strcpy(m_sz1oca1IP,127.0.0.,);memset(m_szHostName,0,30);m_RecvPort=9527;m_SckRecive=INVA1ID_SOCKET;m.SckSend=IN
10、VA1ID.SOCKET;memset(m_szRemoteIP,0,20);strcpy(m_szRemote1P,127.0.0.1,);m_RemotePort=9527;m_RecvThread=NU11;m_Thread!D=0;m_bReciveF1ag=fa1se;boo1CUDPSerVer=InitSocket1ib(WC)RDwVersion)(WSADATAwsd;intret=WSAStartUP(WVerSiOn.&wsd);if(ret!=O)(WSAC1eanupO;returnfa1se;returntrue;voidCUDPServer:C1eanSocket
11、1ib()WSAC1eanupO;创建发送socketboo1CUDPServer:CreateSendSocket()intret;boo1f1ag;Sockaddrjnaddr;C1OSE_SOCKET(m_SckSend);m.SckSend=SoCket(AFNET,SOCK_DGRAMJPPROTOP);if(m_SckSend=INVA11D_SOCKET)(returnfa1se;)f1ag=true;设置允许地址复用ret=setsockopt(m_SckSend,SO1_SOCKET,SO_REUSEADDR,(char*)fef1ag,sizeof(f1ag);if(ret
12、!=O)C1OSE_SOCKET(m_SckSend);returnfa1se;)绑定ZeroMemory(&addr,SiZeof(addr);addr.sin_fami1y=AFNET;addr.sin_addr.s_addr=INADDR_ANY;Z/addr.sin_port=htons(m_RecvPort);/addr.sin_port=m_RecvPort;ret=bind(m_SckSend,(structsockaddr*)&addr,SiZeof(addr);if(ret!=O)(C1OSE_SOCKET(m_SckSend);returnfa1se;)returntrue
13、;)创建接收socketboo1CUDPServer:CreateRecvSocket(intRecvPort)(intret;boo1f1ag;Sockaddrjnaddr;C1OSE_SOCKET(m_SckRecive);m_RecvPort=RecvPort;/创建socketm_SckRecive=socket(AF_INET,SOCK_DGRAM,IPPROTO_IP);if(m_SckRecive=INVA1ID_SOCKET)(returnfa1se;)f1ag=true;设置允许地址更用ret=SetSoCkoPt(m_SckRecive,So1_SOCKET,Se1REUS
14、EADDR,(Char*)&f1ag,SiZeOf(f1ag);if(ret!=O)(C1OSE_SOCKET(m_SckRecive);returnfa1se;)*constintroutenum=10;ret=setsockopt(m_SckMu1tiCastSend,IPPROTO_IP,1P_MU1TICAST_TT1,(char*)&routenum,sizeof(routenum);if(ret!=O)(C1OSE_SOCKET(m_SckMu1tiCastSend);returnfa1se;)constint1oopback=1;禁止回馈ret=setsockopt(m_SckM
15、u1tiCastSend,IPPROTO_1P,1P_MU1TICAST_1OOR(char*)&1oopback,sizeof(1oopback);if(ret!=O)(C1OSE_SOCKET(m_SckMu1tiCastSend);returnfa1se;)f1ag=true;设置该套接字为广播类型,setsockopt(m_SckMu1tiCastSend,SO1_SOCKET,SO-BROADCASTXcharFAR*)&f1ag,sizeof(f1ag);*/绑定ZerOMemOry(&addr,SiZeof(addr);addr.sin_fami1y=AF_INET;addr.sin_addr.s_addr=INADDR_ANY;addr.sin-port=htons(m_RecvPor