實驗報告tcp套接字編程_第1頁
實驗報告tcp套接字編程_第2頁
實驗報告tcp套接字編程_第3頁
實驗報告tcp套接字編程_第4頁
實驗報告tcp套接字編程_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

2:TCP時間:2015年03月31日星期二,第3/4 地點:理工樓學(xué)號 ;班級:計本7WindowsSocket使用WindowsSocketAPI,采用TCP協(xié)議實現(xiàn)一個簡單的文件傳輸服1、首先,運行服務(wù)端。服務(wù)端啟動后,將在指定端口上來自客戶2、接著,運行客戶端,由客戶端通過已知的IP地址和端,向服務(wù)3、雙方建立連接。連接建立后,由客戶端將所需文件的文件名(絕對4、服務(wù)端在本機上查找相關(guān)文件。若找到,則服務(wù)端將該文件傳5提示:TCP硬件條件:PC操作系統(tǒng):WindowsXP Windows7;開發(fā)環(huán)境:VC++6.0;socket()Bink()服務(wù)器端用客戶端發(fā)送的連接請求#include#pragmacomment(lib,"WS2_32.lib")#include<iostream>#includecharg_szNewFileName[MAXFILEDIRLENGTH];charg_szBuff[MAX_PACKET_SIZE+1];longg_lLength;char*g_pBuff=#define bool{WSADATAint //InitializeWinsockversionif((Ret=WSAStartup(MAKEWORD(2,2),&wsaData))!={//NOTE:SinceWinsockfailedtoloadwecannotuse//todeterminetheerrorcodeasisnormallydonewhena//APIfails.Wehavetoreportthereturnstatusoftheprintf("WSAStartupfailedwitherror%d\n",Ret);returnfalse;}return}bool{if(g_pBuffNULL){delete[]g_pBuff;g_pBuff=NULL;}return}//文件進緩沖boolReadRequestFile(SOCKETs{FILE*pFile=if((pFile=fopen(g_szNewFileName,printf("Cannotfindthefile,requesttheinputfilenameagain\n");TMSG_ERROR_MSGtMsgErrorMsg(MSG_OPENFILE_ERROR);send(s,(char*)(&tMsgErrorMsg),sizeof(TMSG_ERROR_MSG),0);returnfalse;}printf("文件長度=%d\n",g_lLength);TMSG_FILELENGTHtMsgFileLength(g_lLength);send(s,(char*)(&tMsgFileLength),sizeof(TMSG_FILELENGTH),0);//磁盤號 _splitpath(g_szNewFileName,szDrive,szDir,szFname,szExt); TMSG_FILENAMEtMsgFileName;strcpy(tMsgFileName.szFileName,szFname);printf("傳送文件名為:%s\n",tMsgFileName.szFileName);send(s,(char*)(&tMsgFileName),sizeof(TMSG_FILENAME),0);g_pBuffnewchar[g_lLength1];if(g_pBuff==NULL){printf("Allocmemoryforfileerror!\n");returnfalse;}fseek(pFile0,SEEK_SET);fread(g_pBuff,sizeof(char),g_lLength,pFile);g_pBuff[g_lLength]='\0';returntrue;}//打開并文boolOpenRequestFile(TMSG_HEADER*pMsgHeader,SOCKETs{TMSG_FILENAME*pRequstFileNameMsg=(TMSG_FILENAME*)char*p1,*p2; {{//除了回車以外的所有字}{ }}{{returnReadRequestFile(s}boolSendRequestFile(SOCKETs{if(NULL=={}intnPacketBufferSizeMAX_PACKET_SIZE2*sizeof(int);for(inti=0;i<g_lLength;i+={TMSG_FILEtMsgFile.tFile.nStarti;if(i+nPacketBufferSize+1>{tMsgFile.tFile.nSize=g_lLength-i;}{tMsgFile.tFile.nSize} //send(s,(char*)(&tMsgFile),sizeof(TMSG_FILE),0);intnBytes=sizeof(TMSG_FILE);intnLeft=nBytes,idx=0;while(nLeft>0)intret=send(s,&((char*)(&tMsgFile))[idx],nLeft,0);if(ret==SOCKET_ERROR){printf("SendFileNameError!\n");}nLeft-=ret;}}delete[]g_pBuff;g_pBuff=NULL;returntrue;}boolProcessMsg(SOCKETs){/*structtimevaltv_out;tv_out.tv_sec=1;tv_out.tv_usec= intnRecv=recv(sintnRecv=0;intnLeft=MAX_PACKET_SIZE+1,idx=while((nLeft>0)&&((nRecv=recv(s,&g_szBuff[idx],nLeft,{nLeft-=nRecv;if(nRecv==printf("RecvError%d!\n",GetLastError());}{}TMSG_HEADER*pMsgHeader=(TMSG_HEADER*){caseMSG_FILENAME://{}{}caseMSG_SENDFILESUCCESS:/{printfs!\n");returnfalse;}{printf("Thefilereadytosendalreadyexit!\n");returnfalse;}}return} bool {SOCKETsListen;SOCKETs//Createanewsockettolisteningforconnections.if((sListen=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))=={printf("socketfailedwitherror%d\n",WSAGetLastError());return}//SetupaSOCKADDR_INstructurethatwilllbindthat//wanttolistenforconnectionsonallinterfacesusingthespeicalsockaddr_inServerAddr;ServerAddr.sin_family=AF_INET;ServerAddr.sin_port=htons(PORT);ServerAddr.sin_addr.s_addr=//Associatetheaddressinformationwiththesocketusingbind.if(bind(sListen,(SOCKADDR*)&ServerAddr,sizeof(ServerAddr))=={printf("bindfailedwitherror%d\n",WSAGetLastError());return}//Listenforconnections.Weusedabacklogof5which//normalformanyif(listen(sListen,5)=={printf("listenfailedwitherror%d\n",WSAGetLastError());return}printf("d.\n",intAddrLen=sizeof(SOCKADDR);sockaddr_inAddr;//Acceptanewconnectionwhenoneif((s=accept(sListen,(SOCKADDR*)&&AddrLen))=={printf("acceptfailedwitherror%d\n",WSAGetLastError());return}printf("我們成功從該地址獲取了連接%s:%d.\n",inet_ntoa(Addr.sin_addr),ntohs(Addr.sin_port));{}returntrue;}int{ return0;}:#include#pragmacomment(lib,"WS2_32.lib")#include<iostream>#include<direct.h>#include"tmsg.h"#define #define longg_lLength=0;char*g_pBuff=NULL;charcharg_szBuff[MAX_PACKET_SIZE+1];IDSOCKETg_sbool{WSADATAint//InitializeWinsockversionif((Ret=WSAStartup(MAKEWORD(2,2),&wsaData))!={//NOTE:SinceWinsockfailedtoloadwecannotuse//todeterminetheerrorcodeasisnormallydonewhena//APIfails.Wehavetoreportthereturnstatusofthefunction.printf("WSAStartupfailedwitherror%d\n",Ret);return}return}bool{socketif(g_pBuffNULL){delete[]g_pBuff;g_pBuff=NULL;}return}servervoid{//charszFileName[MAXFILEDIRLENGTH]; :");fgets(g_szFileName,MAXFILEDIRLENGTH,stdin);//將文件名在serverTMSG_FILENAMEtMsgRequestFileName;strcpy(tMsgRequestFileName.szFileName,g_szFileName);=={printf("SendFileNameError!\n");}intnBytes=sizeof(TMSG_FILENAME);intnLeft=nBytes,idx=0;while(nLeft>{

intret=send(g_s,&if(ret=={printf("SendFileNameError!\n");}nLeft-=ret;}}boolOpenFileError(TMSG_HEADER{//assert(pMsgHeader!=NULL);if(g_pBuff!=NULL){returntrue;}printf("Cannotfind//重新輸入文件名稱returntrue;}boolAllocateMemoryForFile(TMSG_HEADER{//assert(pMsgHeader!=NULL);if(g_pBuff!=NULL){returntrue;}TMSG_FILENAME*pFilenameMsgTMSG_FILENAME*)pMsgHeader;printf("文件名為:%s\n",pFilenameMsg->szFileName);//把文件的路徑設(shè)置為當(dāng) if(NULL=={printf("GetCurrentWorkingDirectoryError%d!\n",WSAGetLastError());}//strcpy(g_szFileName,"D:\\");strcat(g_szFileName,"\\");strcat(g_szFileName,pFilenameMsg->szFileName);FILE*if((pFile=fopen(g_szFileName,"r"))!={TMSG_ERROR_MSGsend(g_s,(char*)(&tMsgErrorMsg),sizeof(TMSG_ERROR_MSG),returnfalse;}if{returnfalse;}g_pBuff=newchar[g_lLength+1];if(g_pBuff!=NULL){memset(g_pBuff,'\0',g_lLength+printf("%s!\n",pFilenameMsg-TMSG__READY if(send(g_s,(char*)(&tMsgReady),sizeof(TMSG_0)=={printf("SendError%d!\n",GetLastError());}}{printf("Allocmemoryforfileerror!\n");}return}boolWriteToFile(TMSG_HEADER{//assert(pMsgHeader!=TMSG_FILE*pMsgFile=(TMSG_FILE*)pMsgHeader;intnStart=pMsgFile->tFile.nStart;intnSize=pMsgFile-if(nStart=={}memcpy(g_pBuff+nStart,pMsgFile->tFile.szBuff,if(nStart+nSize>={printf("Writingto FILE*pFile=fopen(g_szFileName,"w+b");fwrite(g_pBuff,sizeof(char),g_lLength,pFile);delete[]g_pBuff;g_pBuff=NULL;TMSG_SENDFILESUCCESS sizeof(TMSG_SENDFILESUCCESS),0)==SOCKET_ERROR){printf("SendSuccessMsgtoServerError%d!\n",GetLastError());}printf("%s!\n",g_szFileName);returntrue;}{returnfalse;}}serverbool{structtimevaltv_out;tv_out.tv_sec=1;tv_out.tv_usec= intnRecv=recv(g_s,g_szBuff,MAX_PACKET_SIZE+1,/*intnRecv=intnLeft

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論