版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、#define _VC#include #include #include #include #include #ifndef MAKEWORD#define MAKEWORD(l,h) (WORD)(BYTE)(l)|(WORD)(BYTE)(h)8)#endif#define WSA_MAJOR_VERSION 1#define WSA_MINOR_VERSION 1#define WSA_VERSION MAKEWORD(WSA_MAJOR_VERSION, WSA_MINOR_VERSION)/* read/write request packet format2 bytes stri
2、ng 1 byte string 1 byte-| Opcode | Filename | 0 | Mode | 0 |-*/#define TFTP_RRQ 1 /*Read request (RRQ)*/#define TFTP_WRQ 2 /*Write request (WRQ) */* DATA packet format2 bytes 2 bytes n bytes-| Opcode | block # | Data |-*/#define TFTP_DATA 3 /*Data (DA TA)*/* ACK packet format2 bytes 2 bytes-| Opcode
3、 | block # |-*/#define TFTP_ACK 4 /*Acknowledgment (ACK)*/*ERROR packet format2 bytes 2 bytes string 1 byte-| Opcode | ErrorCode | ErrMsg | 0 |-*/#define TFTP_ERROR 5 /*Error (ERROR)*/#define TFTP_NETASCII 0#define TFTP_OCTET 1#define TFTP_WSTA T_FIRSTACKE 0#define TFTP_WSTA T_NEXTACK 1#define TFTP_
4、WSTA T_LASTACK 2#define MAX_RETRY 3#define TFTP_NOTEND_DATALEN 512+2+2#ifdef _VC#pragma comment(lib,Wsock32.lib)#endif/*typedef void (* CMDFUNC)(char 256,int pcout);typedef struct _cmdnumchar *cmd;int num;int paramcount;CMDFUNC callback;CMDNUM,*PCMDNUM;*/typedef struct DATAint socknum;char *filename
5、;unsigned short remoteport;char *remoteaddr;DA TA passdata6;ofstream logfile(log.txt,ios:app);int makeack(unsigned short num,char *buffer,int size);void showsysinfo();void getfile(DATA *needdata);void putfile(DATA *needdata);int makeerror(int errcode,char errstring256,char *buffer,int bufsize);SOCKE
6、Tsock6=INV ALID_SOCKET,INV ALID_SOCKET,INV ALID_SOCKET,INV ALID_SOCKET,IN V ALID_SOCKET,INV ALID_SOCKET;int filemode =TFTP_OCTET;#define MYPORT 69void main(int argc,char *argv)WSADATA stWSAData;int ret=0;sockaddr_in addr;char recvbuf1024=0;char sendbuf1024=0;sockaddr_in from;sockaddr_in newaddr;int
7、fromlen=0;char filename256;int i,len;DWORD ThreadID6;showsysinfo();if(WSAStartup(WSA_VERSION,&stWSAData)!=0)printf(cant start socket rn);exit(0);if (sock0=socket(AF_INET,SOCK_DGRAM,0)0)perror(socket);exit(1);addr.sin_family=AF_INET;addr.sin_port=htons(MYPORT);addr.sin_addr.s_addr=INADDR_ANY;memset(&
8、(addr.sin_zero),0,8);if (bind(sock0,(struct sockaddr *)&addr,sizeof(struct sockaddr)0) perror(bind);exit(1);for (;)fromlen=sizeof(from);recvfrom(sock0,recvbuf,sizeof(recvbuf),0,(sockaddr*)&from,&fromlen);if (recvbuf0=0 & recvbuf10)for (i=0;i+)filenamei=recvbufi+2;if (filenamei=0) break;for (i=1;i=5;
9、i+) if (socki=INV ALID_SOCKET) passdatai.socknum=i;if (socki=socket(AF_INET,SOCK_DGRAM,0)0)perror(socket);exit(1);newaddr.sin_family=AF_INET;newaddr.sin_port=0;newaddr.sin_addr.s_addr=INADDR_ANY;memset(&(newaddr.sin_zero),0,8);if (bind(socki,(struct sockaddr *)&newaddr,sizeof(struct sockaddr)8);pos+
10、;bufferpos=(char)num;pos+;return pos;int makedata(int num,char *data,int datasize,char *buffer,int bufsize) int pos=0;bufferpos=0;pos+;bufferpos=TFTP_DA TA;pos+;bufferpos=(char)(num8);pos+;bufferpos=(char)num;pos+;memcpy(&bufferpos,data,datasize);pos=pos+datasize;return pos;int makeerror(int errcode
11、,char *errstring,char *buffer,int bufsize)int pos=0;int i;bufferpos=0;pos+;bufferpos=TFTP_ERROR;pos+;bufferpos=(char)(errcode8);pos+;bufferpos=(char)errcode;pos+;for(i=0;i+)bufferpos=errstringi;pos+;if (errstringi=0) break;return pos;void getfile(DATA *needdata)char sendbuf1024=0;char recvbuf1024=0;
12、sockaddr_in addr;sockaddr_in from;int fromlen=0;int ret=0;int len=0;/ fd_set fdr; int retry=0;/ struct timeval timeout=5,0; int stat=0;int lastdata=0; long flen=0; FILE *file;addr.sin_family=AF_INET; from.sin_family=AF_INET; addr.sin_port=htons(needdata-remoteport); addr.sin_addr.s_addr=inet_addr(ne
13、eddata-remoteaddr);/ printf(received WRQ form %sn,needdata-remoteaddr); if(file=fopen(needdata-filename,rb)!=NULL) fclose(file);printf(file %s already existn,needdata-filename); len=makeerror(6,file already exist0,sendbuf,sizeof(sendbuf);sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,si
14、zeof(addr);closesocket(sockneeddata-socknum);sockneeddata-socknum=INV ALID_SOCKET;return;if (file=fopen(needdata-filename,w+b)=NULL)printf(cant create file rn);closesocket(sockneeddata-socknum);sockneeddata-socknum=INV ALID_SOCKET;return;len=makeack(lastdata,sendbuf,sizeof(sendbuf);ret=sendto(sockne
15、eddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);/ printf(send ack 0# to %sn,needdata-remoteaddr); while(true)/* FD_ZERO(&fdr);FD_SET(sockneeddata-socknum,&fdr); ret=select(sockneeddata-socknum,&fdr,NULL,NULL,NULL); if(SOCKET_ERROR=ret)printf(socket error rn); fclose(file);return; else if
16、 (0=ret)if (MAX_RETRY=retry)printf(Time outrn);fclose(file);return;sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);retry+; else if (FD_ISSET(sockneeddata-socknum,&fdr)retry=0;*/fromlen=sizeof(sockaddr);ret=recvfrom(sockneeddata-socknum,recvbuf,sizeof(recvbuf),0,(sockaddr *)&
17、from,&fromlen);if(0=stat)addr.sin_port=from.sin_port;stat=1;if(TFTP_DATA=recvbuf1)lastdata=(recvbuf2+256)%256)*256+(recvbuf3+256)%256;/ printf(received data block %d# from %sn,lastdata,needdata-remoteaddr);len=makeack(lastdata,sendbuf,sizeof(sendbuf);sendto(sockneeddata-socknum,sendbuf,len,0,(sockad
18、dr*)&addr,sizeof(addr);/ printf(send ack %d# to %sn,lastdata,needdata-remoteaddr); if(retremoteaddr);logfile received filename from remoteaddrrnsocknum);sockneeddata-socknum=INVALID_SOCKET;return; else fwrite(&recvbuf4,1,512,file); flen=flen+512;/ printf(%d byte receivedr,flen);/ / void putfile(DATA
19、 *needdata)char sendbuf1024=0;char recvbuf1024=0;char databuf1024=0;sockaddr_in addr;sockaddr_in from;int fromlen=0;int ret=0;int len=0;fd_set fdr;int retry=0;struct timeval timeout=5,0;int stat=TFTP_WSTAT_NEXTACK;int lastack=0;FILE *file;int flen=0;int blocknum=0;size_t rlen=0;addr.sin_family=AF_IN
20、ET;from.sin_family=AF_INET;addr.sin_port=htons(needdata-remoteport);addr.sin_addr.s_addr=inet_addr(needdata-remoteaddr);/ printf(received RRQ form %sn,needdata-remoteaddr); if(file=fopen(needdata-filename,rb)=NULL) printf(file %s not foundrn,needdata-filename); len=makeerror(1,file not found0,sendbu
21、f,sizeof(sendbuf);sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);closesocket(sockneeddata-socknum);sockneeddata-socknum=INV ALID_SOCKET;return;rlen=fread(databuf,1,512,file);if (rlensocknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);/ printf(send data block %d# to %sn,blo
22、cknum,needdata-remoteaddr);for(;)FD_ZERO(&fdr);FD_SET(sockneeddata-socknum,&fdr);ret=select(sockneeddata-socknum,&fdr,NULL,NULL,&timeout);if(SOCKET_ERROR=ret)printf(socket errorrn);fclose(file);return; else if (0=ret)if (MAX_RETRY=retry)printf(time outrn);fclose(file);closesocket(sockneeddata-socknu
23、m);sockneeddata-socknum=INV ALID_SOCKET;return;sendto(sockneeddata-socknum,sendbuf,len,0,(sockaddr *)&addr,sizeof(addr);/ printf(send data block %d# to %sn,blocknum,needdata-remoteaddr);retry+; else retry=0; fromlen=sizeof(sockaddr);ret=recvfrom(sockneeddata-socknum,recvbuf,sizeof(recvbuf),0,(sockaddr *)&from,&fromlen);if(TFTP_ACK=recvbuf1)lastack=(recvbuf2+256)%256)*256+(recvbuf3+256)%256;/ printf(received ack %d# from %sn,lastack,needdata-remoteaddr);switch(stat)case TFTP_WSTAT_NEXTACK:if (lastack=blocknum)/ print
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 現(xiàn)澆樓蓋 課程設(shè)計
- 2025年度地下空間開發(fā)個人工程勞務(wù)分包合同范本4篇
- 2024年心理咨詢師題庫附答案(典型題)
- 二零二五版門衛(wèi)服務(wù)外包與社區(qū)安全防范系統(tǒng)承包4篇
- 2025年度草坪圍欄施工與智慧城市建設(shè)合同3篇
- 植筋膠施工方案
- 二零二五年度棉花品牌建設(shè)與推廣合同4篇
- 2024酒店宴會廳租賃與客戶服務(wù)協(xié)議版B版
- 地板磚拆除施工方案
- 工地跳板擋墻施工方案
- 北京工業(yè)大學(xué)教學(xué)科研儀器設(shè)備政府采購(一)
- 2023年版《安寧療護(hù)實踐指南(試行)》解讀課件
- AQ6111-2023個體防護(hù)裝備安全管理規(guī)范
- 2024年高考語文備考之??甲骷易髌罚ㄏ拢褐袊F(xiàn)當(dāng)代、外國
- T-CSTM 01124-2024 油氣管道工程用工廠預(yù)制袖管三通
- 新譯林版高中英語必修二全冊短語匯總
- 基于自適應(yīng)神經(jīng)網(wǎng)絡(luò)模糊推理系統(tǒng)的游客規(guī)模預(yù)測研究
- 河道保潔服務(wù)投標(biāo)方案(完整技術(shù)標(biāo))
- 品管圈(QCC)案例-縮短接臺手術(shù)送手術(shù)時間
- 精神科病程記錄
- 閱讀理解特訓(xùn)卷-英語四年級上冊譯林版三起含答案
評論
0/150
提交評論