一个简单的UDPServer实现 编程知识点梳理汇总.docx

上传人:太** 文档编号:97871966 上传时间:2024-07-08 格式:DOCX 页数:10 大小:25.06KB
返回 下载 相关 举报
一个简单的UDPServer实现 编程知识点梳理汇总.docx_第1页
第1页 / 共10页
一个简单的UDPServer实现 编程知识点梳理汇总.docx_第2页
第2页 / 共10页
点击查看更多>>
资源描述

《一个简单的UDPServer实现 编程知识点梳理汇总.docx》由会员分享,可在线阅读,更多相关《一个简单的UDPServer实现 编程知识点梳理汇总.docx(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、一个简单的UDP Server实现一、思路1、建立接收socket,将socket和指定的端口绑定2、创建接收线程,在线程中调用ioctlsocket。判断是否接收到数据,接收到数据时调用 OnReceive()(类似 CSocket 中的 OnReceive()3、在OnReceive中申请空间调用recvfrom接收数据4、建立发送socket,和INADDR_ANY绑定5、调用sendto发送数据二、本代码直接使用socket API实现UDP通讯相关的函数有:WSAStartup(WORD wVersionRequested,LPWSADATA IpWSAData);初始化 socke

2、t 库WSACIeanup。;/释放 socket 库socket(int af,int type,int protocol); 建立 socketsetsockopt(SOCKET s,int level,int optname,const char FAR * optval,int optlen); 设置 socket 或者 协议的属性bind(SOCKET s,const struct sockaddr FAR * name,int namelen);地址和 socket 绑定sendto(SOCKET s,const char FAR * buf,int len,int flags,c

3、onst struct sockaddr FAR * to,int tolen); 发送消息ioctlsocket(SOCKET s,long cmd,u_long FAR * argp);/获取 socket 状态,可以获取 socket 缓冲 中数据长度,利用这个函数可以实现类似CSocket中的OnReceive。函数(在接收数据之前调 用)recvfrom(SOCKET s,char FAR * buf,int len,int flags,struct sockaddr FAR * from,int FAR * fromlen);/接收数据三、代码说明1 UDPServer.h 和 U

4、DPServer.cpp 是实现类 CUDPServer 的头文件和实现文件,udpdemo.cpp 是演示程序2、编译环境在win2000 vc6.0下编译通过3、编译时需要在vc的project的setting中连接 Ws2_32.1ib库/ UDPServer.h: interface for the CUDPServer class./IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIH#if !defined(UDPSERVER_H_INCLUDED_)#define UDPSERVER_H_INCLUDED_ #if_MS

5、C_VER 1000#pragma once#endif/ MSC VER 1000elseif(lLength0)(pUDPServer-OnReceive(lLength);)printf(nReceive Thread Ended.nH);return 0;)/udpdemo.cpp#include #include nUDPServer.hnusing namespace std;int main(int argc,char *argv) (char szBuf100;CUDPServer pUDPServer;coutnUDP Demo.Hendl;pUDPServer.Create

6、RecvSocket(9000);pUDPServer.CreateSendSocket();pUDPServer.StartRecv();while(l)(memset(szBuf,0,100);coutnInput:n;cinszBuf;if(strcmp(szB uf, nquitH )=0) (pUDPServer.StopRecv();break;)pUDPServer.SendMsg(szBuf,strlen(szBuf),nl 27.0.0. ln,9000);)return 0;四、结束语欢迎对代码不足之处提出宝贵意见。CLOSE_SOCKET(X)include / Ws2_

7、32.1ib #defineif(X!=INVALID_SOCKET)closesocket(X);X=INVALID_SOCKET; class CUDPServer ( private:char m_szLocalIP20;char m_szHostName30; int m_RecvPort;char m_szRemoteIP20;int m_RemotePort;HANDLE m_RecvThread;DWORD m_ThreadID;private:void InitMemberVariable(); public:SOCKET m_SckRecive;SOCKET m_SckSen

8、d; bool m_bReciveFlag; CUDPServer();virtual -CUDPServer();void Initialize(); void Destory(); 初始化 socket lib bool InitSocketLib(WORD wVersion=0x0202); void CleanSocketLib();创建发送socket bool CreateSendSocket(); 创建接收socketbool CreateRecvSocket(int RecvPort=9527); 发送int SendMsg(char * szBuf,int length,ch

9、ar * szremoteIP=NULL,int port=0);启动接收线程 bool StartRecv();停止接收线程bool StopRecv();接收线程函数,static DWORD WINAPI ReceiveThread(LPVOID IpParameter); / 线程函数/当有数据时,调用OnReceive,length表示数据长度void OnReceive(long length);static DWORD WINAPI OnReceiveThread(LPVOID IpParameter); / 线程函数;#endif /! defined(UDPSERVER_H_

10、INCLUDED_) / UDPServer.cpp: implementation of the CUDPServer class./include MUDPServer.hn#include / Construction/DestructionIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIH/ /kkJ*1* kJ* *!* kJkJkJ*1* *1* *!* kJ* kJkJkF*kJ k1 k|* k1*!/ / *4*7* *7*7* *T*7* *7* *T*7* *7*7* *T*7*T *7* *7* *7* *T

11、*7* *7*CUDPServer: :CUDPServer()InitializeQ;/ / *4 *.!*1*1* L*.L *1*1*.L* *!* *1*必KL 1* , , 1 , *1* !. * 1, -X* , 1, *1* *1* , ,*1* * 1. 1 7,7, * , , , :. *1* 1, , , ./ / *7、*7* *T* *7* *7* Tw *Tw *T* *7* rTw rr *Tw - *7、*7* *T* *T* *7* *T rTw rT* *Jw *Tw *T* rTw Tw *Tw *7*Tw *Tw *7、*7* *T* Tw Tw *7

12、* *T rTw rTw *Tw T rT riw erw *Tw void CUDPServer: Destory()if(m_bReciveFlag)StopRecv();)CleanSocketLib();/ / !* *!* kJ*L*L *!* *!*k|* *1* *!*1* *1* *!* *!*L *1*f *T* *T *T* *T*T*T* *T* *T* *T* *7* *T* *T* *T* *T* *Tw *T*T*7* *T*T* *T*T* *T* *? *T*T* *T* *T*T* *T*void CUDPServer:InitMemberVariable()

13、memset(m_szLocalIP,0,20);strcpy(m_szLocalIP,n 127.0.0. ln);memset(m_szHostName,0,30);m_RecvPort = 9527;m SckRecive = INVALID SOCKET;m.SckSend = INVALID.SOCKET; memset(m_szRemoteIP,0,20); strcpy(m_szRemoteIP,n 127.0.0.1,); m RemotePort = 9527;m_RecvThread = NULL;m_ThreadID = 0;m_bReciveFlag = false;/

14、 / %1* *1* * *1* *1* * * !* !* *1* / /卜卜,卜,卜,7、 卜,】 ,卜卜,1 ,卜,卜,卜 , ,卜.、卜 卜一:, 卜,】 , 一1,卜卜,卜卜,卜,卜.、 卜1 ”卜bool CUDPServer:InitSocketLib(WORD wVersion) (WSADATA wsd;int ret = WSAStartup(wVersion,&wsd);if(ret!=O)(WSACleanupO;return false;)return true;)/ /kJkk|k!kk1 kIk|1 k! k1 *1 k1 k kJx/ / 、4、,I、.】、*4

15、*、4、】、*4、4、4、*4* *2*Y、* i* 、.卜,卜 *1、.卜 V、*1* 、【、】、4*4、4、.卜 、V、1* void CUDPServer:CleanSocketLib()WSACleanupO;/ /kJ*si* k1 kIak*k| k1 k1 k1 *1* k*k1k kk1k J k1 *1* *1*k1 k1 k1 k1k! k1 k1k*k1* k1 k|/ /、4、1* *1* *T4* 、1、*1*、 1*4、4、4、 、1、 .卜 1、1*、卜卜、.、1、V、7*1* !* !* .!* .!*!* ,/ / 卜*. ; *i*,*7* ,1 ,卜* 卜

16、卜; *1* ,【 *i* ,卜,1*,1, *7*; ,*1*创建接收socketbool CUDPServer:CreateRecvSocket(int RecvPort) (int ret;bool flag;SOCKADDR IN addr;CLOSE_SOCKET(m_SckRecive);m_RecvPort = RecvPort;创建 socketm_SckRecive = socket(AFNET,SOCK_DGRAM,IPPROTOP);if(m_SckRecive=INVALID_SOCKET)( return false;)flag = true;设置允许地址复用ret=

17、setsockopt(m_SckRecive,SOL_SOCKET,SO_REUSEADDR,(char*)&flag,sizeof(flag);if(ret!=O)(CLOSE_SOCKET(m_SckRecive);return false;)/*const int routenum = 10;ret= setsockopt(m_SckMultiCastSend,IPPROTO_IP,IP_MULTICAST_TTL,(char*)&routenum,sizeof( routenum);if(ret!=O)CLOSE_SOCKET(m_SckMultiCastSend); return f

18、alse;)const int loopback = 1; 禁止回馈ret setsockopt(m_SckMultiCastSendJPPROTO_IPJP_MUUTICAST_LOOP,(char)&loopback,sizeof (loopback);if(ret!=O)CLOSE_SOCKET(m_SckMultiCastSend);return false;)flag=true;设置该套接字为广播类型,FARsetsockopt(m_SckMultiCastSend,SOL_SOCKET,SO_BROADCAST,(char*)&flag,sizeof(flag);*/绑定ZeroM

19、emory(&addr,sizeof(addr);addr.sin_family = AF_INET;addr.sin_addr.s_addr = INADDR_ANY;addr.sin_port = htons(m_RecvPort);/addr.sin_port = m_MultiCastPort;ret = bind(m_SckRecive,(struct sockaddr )&addr,sizeof(addr);if(ret!=0)CLOSE_SOCKET(m_SckRecive); return false;return true;/ /7,7,*1* *1* *1* 7,7,*1*

20、 *1* *1* kJ* *l* *1* *1* *1* *1* *1* *1* * *1* *1* *1* *L* 7,*.!*k|*1* *1* *1* *1* *L* kJ k! k!* *1 *i *1* *1* *1* 7,7,*1* *L* k17,1 *1* *.!* *1* *1* 7,7,*1*/ /乙、q.,j、,j、,j*/j. /j* j* *2*4* q. q.,j、*2*,j、*4*q. q.,j、j*,j*,j* *4*/j* *2* *4* q. q.-j、/j* q* *?* r、,l、-j*,j.,j、,j*q./发送int CUDPServer:SendM

21、sg(char * szBuf,int length,char * szremoteIP,int port)int ret;SOCKADDRJN addr;if(m_SckSend=INVALID_SOCKET)return -1;if(szremoteIP!二NULL)strcpy(m_szRemoteIP,szremoteIP);m_RemotePort = port;)ZeroMemory(&addr,sizeof(addr);addr.sin_family = AF_INET;addr.sin_addr.s_addr = inet_addr(m_szRemoteIP);addr.sin

22、_port = htons(m_RemotePort);ret = sendto(m_SckSend,szBuf,length,0,(sockaddr *)&addr,sizeof(addr); if(ret =二 SOCKET_ERROR)(return -1;)return ret;)/ /*1*kl*1* *1* *1* !* !* .!* .!*!* ,/ / 卜*. ; *i*,*7* ,卜* 卜卜; *1* ,【 *i* ,卜,1*,1, *7*; ,*1*启动接收线程bool CUDPServer:StartRecv()(if(m_SckRecive=INVALID_SOCKET

23、) (if(! CreateRec vSocket()return false;)if(m_bReciveFlag)return true;)/m_RecvThread 二 CreateThread(NULL,O,ReceiveThread,this,O,&m_ThreadID);m_RecvThread = CreateThread(NULL,0,OnReceiveThread,this,0,&m_ThreadID);if(m_RecvThread=NULL)return false; )return true;/ /*1* *XkJkJ*kJ必*1*1* kJ*f *7* *T*T*停止接

24、收线程if(! m_bReciveFlag)return true;)CLOSE_SOCKET(m_SckRecive); if(m_Rec vThread! =NULL)WaitForSingleObject(m_RecvThread,INFINITE);m_RecvThread = NULL;m_ThreadID = 0;)return true;)/ /si*/ /*Jw rj*rj* rj*rj*rjw/Jw rj* rjwrj*rj* rj* *Jwrj*rj* r|*接收线程函数,DWORD WIN API CUDPServer: :ReceiveThread(LPV OID Ip

25、Parameter)int addrjen;struct sockaddr_in addr;char szRecBuf 10000;int iRecLen;CUDPServer * pUDPServer;pUDPServer = (CUDPServer *)lpParameter;pUDPServer-m_bReciveFlag = true;printf(MReceive Thread Started.nn);/addr.sin_family = AF_INET;addr. sin_addr. s_addr = INADDR_ANY;addr.sin_port = htons(pNetMul

26、tiCast-m_MultiCastPort);*/while(pUDPServer-m_bReciveFlag)addr_len = sizeof(addr);iRecLen = recvfrom(pUDPServer-m_SckRecive,szRecBuf,l 0000,0,(struct sockaddr *)&addr,&addr_len);if(iRecLen=SOCKET_ERROR|iRecLenm_bReciveFlag = false;)elseszRecBufiRecLen = 0;printf(HReceive from %s :%snn,inet_ntoa(addr.

27、sin_addr),szRecBuf);printf(Receive Thread Ended.nH); return 0;void CUDPServer:OnReceive(long length)(/length是socket列队中的第一个报文长度/在本函数中可以进行业务的处理char *pbuf;int addr_len,iRecLen;struct sockaddr_in addr;pbuf 二 new charlength+l;memset(pbuf,O,length+1);addr_len = sizeof(addr);iRecLen = recvfrom(m_SckRecive,pbuf,length,Obstruct sockaddr *)&addr,&addr_len);if(iRecLen=SOCKET_ERROR| |iRecLenm_bReciveFlag = true;printf(nReceive Thread Started.nn);while(pUDPServer-m_bReciveFlag)(ret = ioctlsocket(pUDPServer-m_SckRecive,FIONREAD,&lLength); if(ret!=O)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁