




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
時間:20150324時間:201503243-4姓名:林小燕班級:工程8WindowsSocketAPI,C/S2、客戶機向服務器發送信息(如字符串“hello”)ModuleName:tcpclient.cppThissampleillustrateshowtodevelopasimpleTCP//thatcansendasimple"hello"messagetoaTCPserverlisteningonport5150.//Thissampleisimplementedasaconsole-styleapplicationandsimplyprints//statusmessagesaconnectionismadeandwhendataissenttothecl-otcpclienttcpclient.cppws2_32.libCommandLineOptions:tcpclient.exe<serverIP#include<stdio.h>#pragmacharvoid{Port=#pragmacharvoid{Port=5150;(argc{printf("USAGE:tcpclient<ServerIPaddress>.\n");}//InitializeWinsockversion{((Ret=WSAStartup(MAKEWORD(2,2),&wsaData))!=//NOTE:SinceWinsockfailedtoloadwecannotuse//todeterminetheerrorcodeasisnormallydonewhena//APIfails.Wehavetoreportthereturnstatusoftheprintf("WSAStartupfailedwitherror%d\n",Ret);}Createanewsockettomakeaclientconnection.((s=socket(AF_INET,SOCK_STREAM,=={printf("socketfailedwitherror%d\n",}SetupaSOCKADDR_INstructurethatwillbeusedtoconnecttoalisteningserveronport5150.Fordemonstrationpurposes,werequiredtheusertosupplyanIPaddressonthecommandlineandwefilledthisfieldinwiththedatafromtheuser.ServerAddr.sin_family=AF_INET;ServerAddr.sin_port=htons(Port);ServerAddr.sin_addr.s_addr=inet_addr(argv[1]);//Makeaconnectiontotheserverwithsockets.printf("Wearetryingtoconnectto%s:%d...\n",if(connect(s,(SOCKADDR*)&ServerAddr,=={printf("connectfailedwitherrorif(connect(s,(SOCKADDR*)&ServerAddr,=={printf("connectfailedwitherror%d\n",WSAGetLastError());}printf("Ourconnection//Atthispointyoucanstartsendingorreceivingdata//thesockets.Wewilljustsendahellomessagetotheserver.printf("Wewillnowtrytosendahellomessage.\n");if((Ret=send(s,"hello",6,0))=={printf("sendfailedwitherror%d\n",WSAGetLastError());}printf("Wesuccessfullysent%dbyte(s).\n",//Whenyouarefinishedsendingandreceivingdataon//youshouldclosetheprintf("Weareclosingtheconnection.\n");//Whenyourapplicationisfinishedhandlingthe//}ModuleName:tcpserver.cppThissampleillustrateshowtodevelopasimpleTCP//thatlistensforaTCPconnectiononport5150andreceivesdata.isimplementedasaconsole-styleapplicationandmessageswhenaconnectionisacceptedandwhendatabytheserver.cl-otcpservertcpserver.cppCommandLineNOTE:Therearenocommand#include<stdio.h>voidmain(void){NOTE:Therearenocommand#include<stdio.h>voidmain(void){//Port=5150;Winsockversionif((Ret=WSAStartup(MAKEWORD(2,2),{!=//NOTE:SinceWinsockfailedtoloadwecannotuse//todeterminetheerrorcodeasisnormallydonewhena//APIfails.Wehavetoreportthereturnstatusoftheprintf("WSAStartupfailedwitherror%d\n",Ret);}Createanewsockettolisteningforclientconnections.((ListeningSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))=={printf("socketfailedwitherror%d\n",WSAGetLastError());}SetupaSOCKADDR_INstructurethatwilltellbindthatwewanttolistenforconnectionsonallinterfacesusingport5150.NoticehowweconvertthePortvariablefromhostbyteordertonetworkbyteorder.ServerAddr.sin_family=AF_INET;ServerAddr.sin_port=htons(Port);ServerAddr.sin_addr.s_addr=htonl(INADDR_ANY);//Associatetheaddressinformationwiththesocketusingif(bind(ListeningSocket,(SOCKADDR*)&ServerAddr,=={%d\n",printf("bindfailedwithif(bind(ListeningSocket,(SOCKADDR*)&ServerAddr,=={%d\n",printf("bindfailedwitherror}Weusedabacklogof5Listenforclientconnections.normalformanyapplications.{(listen(ListeningSocket,5)==printf("listenfailedwitherror%d\n",WSAGetLastError());}printf("WeareawaitingaconnectiononportClientAddrLen=sizeof(SOCKADDR);//Acceptanewconnectionwhenonearrives.if((NewConnection={%d.\n",(SOCKADDR==printf("acceptfailedwitherror%d\n",WSAGetLastError());}printf("Wesuccessfullygotaconnectionfrom%s:%d.\n",Atthispointyoucandotwothingswiththesesockets.AwaitformoreconnectionsbycallingacceptagainonListeningSocketandstartsendingorreceivingdataonNewConnection.ForsimplicityWewillstoplisteningformoreconnectionsby//ListeningSocket.Wewillstartsendingandreceivingdata//NewConnection.//StartsendingandreceivingdataonNewConnection.For//wewilljustreceivesomedataandreporthowmanybytes//printf("Wearewaitingtoreceiveif((Ret=recv(NewConnection,DataBuffer,sizeof(DataBuffer),=={printf("recvfailedwitherror//wewilljustreceivesomedataandreporthowmanybytes//printf("Wearewaitingtoreceiveif((Ret=recv(NewConnection,DataBuffer,sizeof(DataBuffer),=={printf("recvfailedwitherror%d\n",WSAGetLastError());}printf("Wesuccessfullyreceived%dbyte(s).\n",Ret);printf("data:%s\n",DataBuffer);//Forthisapplicationwedonotplantodoanythingelse//connectionsowewilljustclosetheconnection.printf("Wearenowgoingtoclosetheclientconnection.\n");//Whenyourapplicationis//callWSACleanup.}#include<stdio.h>#pragmavoid{charPort=5150;(argc{IPprintf("USAGE:tcpclient<Server}//InitializeWinsockversionif((Ret=WSAStartup(MAKEWORD(2,2),&wsaData))!={//NOTE:SinceWinsockfailedtoloadwecannotuse//todeterminetheerror//todeterminetheerrorcodeasisnormallydonewhena//APIfails.Wehavetoreportthereturnstatusoftheprintf("WSAStartupfailedwitherror%d\n",Ret);}Createanew((s=socket(AF_INET,SOCK_DGRAM,=={printf("socketfailedwitherror%d\n",}SetupaSOCKADDR_INstructurethatwillbeusedtoconnecttoalisteningserveronport5150.Fordemonstrationpurposes,werequiredtheusertosupplyanIPaddressonthecommandlineandwefilledthisfieldinwiththedatafromtheuser.ServerAddr.sin_family=AF_INET;ServerAddr.sin_port=htons(Port);ServerAddr.sin_addr.s_addr=inet_addr(argv[1]);//Atthispointyoucanstartsendingorreceivingdata//thesockets.Wewilljustsendahellomessagetotheserverintlen;len=printf("Wewillnowtrytosendahelloif((Ret=sendto(s,"hello",6,0,(SOCKADDR*)&ServerAddr,len))==SOCKET_ERROR){printf("sendfailedwitherror%d\n",WSAGetLastError());}printf("Wesuccessfullysent%dbyte(s).\n",//Whenyouarefinishedsendingandreceivingdataonsocket//youshouldclosetheprintf("Weareclosingtheconnection.\n");//Whenyourapplicationisfinishedhandlingtheconnection,//}#include<stdio.h>voidmain(void){}#include<stdio.h>voidmain(void){//Port=5150;Winsockversionif((Ret=WSAStartup(MAKEWORD(2,2),{!=//NOTE:SinceWinsockfailedtoloadwecannotuse//todeterminetheerrorcodeasisnormallydonewhena//APIfails.Wehavetoreportthereturnstatusoftheprintf("WSAStartupfailedwitherror%d\n",Ret);}Createanew((s=socket(AF_INET,SOCK_DGRAM,=={printf("socketfailedwitherror%d\n",WSAGetLastError());}SetupaSOCKADDR_INstructurethatwilltellbindthatwewanttolistenforconnectionsonallinterfacesusingport5150.NoticehowweconvertthePortvariablefromhostbyteordertonetworkbyteorder.ServerAddr.sin_family=AF_INET;ServerAddr.sin_port=htons(Port);ServerAddr.sin_addr.s_a
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 設備運輸方案模板 (一)
- 國家開放大學《教育心理學》形考任務1-4答案
- 設計基礎教案
- 幼兒園大班《樹真好》活動教案
- 財務會計人員的崗位職責(25篇)
- 2025年Android小菜雞2 個月的面試親身經歷告訴大家如何進入 BAT 等大廠
- 2024-2025學年下學期高二生物滬科版期末必刷常考題之生態系統具有維持動態平衡的能力
- 森林報春的題目及答案
- 2023-2024學年山東省煙臺市高二下學期7月期末學業水平診斷數學試題(解析版)
- 2023-2024學年湖南省邵陽市高二下學期期末考試數學試卷(解析版)
- 人力資源薪酬專員個人月工作計劃
- 期末考試(試題)-2023-2024學年五年級下冊數學人教版
- 乳腺癌病人的心理護理課件
- 2024年茶葉加工工(高級)理論考試題庫大全(附答案)
- 大連公交集團管理制度
- ABAQUS在隧道及地下工程中的應用
- 林業和草原建設項目初步設計編制實施細則
- 醫療安全不良事件培訓
- 機器人結構優化設計
- 員工能力評價表(全套)
- 土石方工程挖掘機人員車輛信息登記表
評論
0/150
提交評論