資源描述:
《基于UDP協(xié)議的WinSock編程》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、WinSocket編程(基于UDP)對于在一個無連接套接字上接收數(shù)據(jù)的進程來說,步驟并不復雜。先用WSAStartup來加載WinscockDLL相應版本,然后用socket或WSASocket建立套接字。再把這個套接字和準備接收數(shù)據(jù)的接口綁定在一起。這是通過bind函數(shù)來完成的。和面向連接會話不同的是,我們不必調(diào)用listen和accept。1、Winsock的初始化每個Winsock應用都必須加載WinsockDLL的相應版本。如果調(diào)用Winsock之前,沒有加載Winsock庫,這個函數(shù)就會返回一個SOCKET_ERROR,錯誤信息是WSANOTINITIALISED。加載Winsoc
2、k庫是通過調(diào)用WSAStartup函數(shù)實現(xiàn)的。這個函數(shù)的定義如下: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之前必須調(diào)用WSAStartup加載Winsocket庫成功
7、) OnceanapplicationorDLLhasmadeasuccessfulWSAStartupcall,itcanproceedtomakeotherWindowsSocketscallsasneeded.WhenithasfinishedusingtheservicesoftheWs2_32.dll,theapplicationorDLLmustcallWSACleanuptoallowtheW