欢迎来到天天文库
浏览记录
ID:38711469
大小:57.50 KB
页数:7页
时间:2019-06-18
《基于UDP协议的WinSock编程》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、WinSocket编程(基于UDP)对于在一个无连接套接字上接收数据的进程来说,步骤并不复杂。先用WSAStartup来加载WinscockDLL相应版本,然后用socket或WSASocket建立套接字。再把这个套接字和准备接收数据的接口绑定在一起。这是通过bind函数来完成的。和面向连接会话不同的是,我们不必调用listen和accept。1、Winsock的初始化每个Winsock应用都必须加载WinsockDLL的相应版本。如果调用Winsock之前,没有加载Winsock库,这个函数就会返回一个SOCKET_ERROR,错误信息是WSANOTINITIALISED。加载Winsoc
2、k库是通过调用WSAStartup函数实现的。这个函数的定义如下:intWSAStartup(WORDwVersionRequested,LPWSADATAlpWSAData);wVersionRequested[in]HighestversionofWindowsSocketssupportthatthecallercanuse.Thehigh-orderbytespecifiestheminorversion(revision)number;thelow-orderbytespecifiesthemajorversionnumber.lpWSAData[out]PointertotheW
3、SADATAdatastructurethatistoreceivedetailsoftheWindowsSocketsimplementation.TheWSAStartupfunctionreturnszeroifsuccessful.Otherwise,itreturnsoneoftheerrorcodeslistedinthefollowing.AnapplicationcannotcallWSAGetLastErrortodeterminetheerrorcodeasisnormallydoneinWindowsSocketsifWSAStartupfails.TheWs2_32.
4、dllwillnothavebeenloadedinthecaseofafailuresotheclientdataareawherethelasterrorinformationisstoredcouldnotbeestablished.ErrorcodeMeaningWSASYSNOTREADYIndicatesthattheunderlyingnetworksubsystemisnotreadyfornetworkcommunication.WSAVERNOTSUPPORTEDTheversionofWindowsSocketssupportrequestedisnotprovided
5、bythisparticularWindowsSocketsimplementation.WSAEINPROGRESSAblockingWindowsSockets1.1operationisinprogress.WSAEPROCLIMLimitonthenumberoftaskssupportedbytheWindowsSocketsimplementationhasbeenreached.WSAEFAULTThelpWSADataisnotavalidpointer.注释:TheWSAStartupfunctionmustbethefirstWindowsSocketsfunctionc
6、alledbyanapplicationorDLL.ItallowsanapplicationorDLLtospecifytheversionofWindowsSocketsrequiredandretrievedetailsofthespecificWindowsSocketsimplementation.TheapplicationorDLLcanonlyissuefurtherWindowsSocketsfunctionsaftersuccessfullycallingWSAStartup.(在使用WindowsSockets之前必须调用WSAStartup加载Winsocket库成功
7、) OnceanapplicationorDLLhasmadeasuccessfulWSAStartupcall,itcanproceedtomakeotherWindowsSocketscallsasneeded.WhenithasfinishedusingtheservicesoftheWs2_32.dll,theapplicationorDLLmustcallWSACleanuptoallowtheW
此文档下载收益归作者所有