rabbit5455 发表于 2007-5-3 21:05

[原创]JAVA实现tftp服务端 -- 我博客中的一篇文章

<P>这几天看了一下tftp协议,非常简单,这个学期选修的JAVA,<br>想趁这个机会练习一下,写一个tftp server的,刚刚把demo完成了,<br>用windows2003的tftp客户端测试过了,也用我的ADSL (嵌入式linux)里面的tftp测试过,demo版本还是挺好的,至少可以用 。。。<IMG src="http://blog.bc-cn.net/editor/images/emot/face1.gif"><br>=================================================</P>
<P><STRONG><FONT color=#cc3300>源代码可以在这里下载:</FONT></STRONG> <a href="http://blog.bc-cn.net/UploadFiles/2007-5/53552715.zip" target="_blank" >UploadFiles/2007-5/53552715.zip</A></P>
<P><FONT color=#e73408><STRONG>文章在博客中的地址:</STRONG></FONT> <a href="http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml" target="_blank" >http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml</A><br>=================================================</P>
<P>因为tftp协议本身就很简单,所以我的代码就用了三个类文件,<br><STRONG>Main.java</STRONG> --&gt; 这个是启动代码,这个demo版本中,<br>                     他只是建立一个ServerAgent的对象,然后启动这个对象<br><STRONG>tftpServerAgent.java</STRONG> --&gt; 这是Server,它一直监听UDP Port 69,<br>                                     来了新的连接,它就创建一个ClientAgent,<br>                                     自己继续等待。<br><STRONG>tftpClientAgent.java</STRONG> --&gt; 这是与ftp客户端通信的类,它处理RRQ和WRQ<br>                                    请求。<br>这两个Agent都继承了Thread,因为我想在以后的版本中加入Server的管理功能。</P>
<P>============================================<br><STRONG><FONT color=#cc0000>主要代码:<br><FONT color=#000000>=================================</FONT></FONT></STRONG></P>
<P><STRONG>Main.java<br></STRONG>===========================================<FONT color=#009933></P>
<DIV class=htmlcode twffan="done"><FONT color=#009933>public class Main {<br>    <br>    /** Creates a new instance of Main */<br>    public Main() {<br>    }<br>    <br>    public static void main(String[] args) throws Exception {<br>        // TODO code application logic here<br>        tftpServerAgent tftp_srv = new tftpServerAgent();<br>        tftp_srv.setDaemon(true);<br>        tftp_srv.start();<br>        <br>        tftp_srv.join();    //暂时先这么处理吧<br>    }<br>    <br>}</FONT><br></DIV>
<P></FONT>=======================================</P>
<P><STRONG>tftpServerAgent.java</STRONG><br>===========================================<br><FONT color=#009933></P>
<DIV class=htmlcode twffan="done">
<P><FONT color=#009933>public class tftpServerAgent extends Thread {<br>    <br></FONT><FONT color=#009933>    //over the parent's run methord<br>    public void run() {<br>        try {<br>            DatagramSocket tftpd = new DatagramSocket(69);  //tftp server socket<br>            byte[] buf = new byte[516]; //a buffer for UDP packet<br>            DatagramPacket dp = new DatagramPacket(buf, 516);   //a UDP packet</FONT></P>
<P><FONT color=#009933>            DataInputStream din = null;<br>            tftpClientAgent newClient = null;<br>            short tftp_opcode = 0;  //opcode: the 2 bytes in the front of tftp packet<br>            String tftp_filename = null;<br>            String tftp_mode = null;<br>            while (true) {<br>                tftpd.receive(dp);  //wait for a client<br>                buf = dp.getData(); //get the UDP packet data<br>                din = new DataInputStream(new ByteArrayInputStream(buf));<br>                tftp_opcode = din.readShort();  //get the opcode<br>                {   //get the filename<br>                    int fnoffset = 2;<br>                    int fnlen = 0;<br>                    while (din.readByte() != 0) {<br>                        fnlen++; //filename will end with a null char('\0')<br>                    }<br>                    tftp_filename = new String(buf, fnoffset, fnlen);</FONT></P>
<P><FONT color=#009933>                    //get the mode<br>                    int mdnoffset = fnoffset + fnlen + 2;<br>                    int mdnlen = 0;<br>                    while (din.readByte() != 0) {<br>                        mdnlen++; //filename will end with a null char('\0')<br>                    }<br>                    tftp_mode = new String(buf, mdnoffset, mdnlen);<br>                }</FONT></P>
<P><FONT color=#009933>                switch (tftp_opcode) {<br>                    case 1:<br>                         //RRQ<br>                   case 2:<br>                        //WRQ<br>                        newClient = new tftpClientAgent(dp.getAddress(), dp.getPort(), tftp_opcode, tftp_filename, tftp_mode);<br>                        newClient.setDaemon(true);<br>                        newClient.start();<br>                                                <br>                        this.m_hashClients.put(dp.getAddress().toString()+dp.getPort(), newClient);<br>                        //System.out.println("debug: Main.main() --&gt; a RRQ thread start ....");<br>                        break;<br>                }<br>            }<br>        } catch (Exception e) {<br>            <br>        }<br>    }</FONT></P>
<P><FONT color=#009933>}</FONT></P></DIV>
<P></FONT>=========================================<br><STRONG>tftpClientAgent.java<br></STRONG>=========================================<FONT color=#009933></P>
<DIV class=htmlcode twffan="done"><FONT color=#009933>public class tftpClientAgent extends Thread {<br>    public tftpClientAgent(InetAddress ip, int port, short opcode, String fname, String mode) {<br>        this.m_ClientAddress = ip;<br>        this.m_ClientPort = port;<br>        this.m_curopcode = opcode;<br>        this.m_filename = fname;<br>        this.m_mode = mode;<br>    }<br>    <br>    public void run() {<br>        int nfail = 100;<br>        while (nfail-- &gt; 0) {<br>            //try getFreePort 100 times<br>            try {<br>                this.m_so_tftp = new DatagramSocket(this.getFreePort());<br>                break;  //get a random port number<br>            } catch (SocketException e) {<br>            }<br>        }<br>        <br>        //ok, the UDP socket is ready, response to the client<br>        switch (this.m_curopcode) {<br>            case 1:<br>                //RRQ<br>                this.RRQ();<br>                break;<br>            case 2:<br>                //WRQ<br>                this.WRQ();<br>                //System.out.println("debug: tftpClientAgent.run() --&gt; a WRQ ended ...");<br>                break;<br>        }<br>        <br>        this.m_so_tftp.close();<br>        <br>    }<br>    <br>}<br><br>    private InetAddress m_ClientAddress;    //ip of the client<br>    private int m_ClientPort;   //port of the client<br>    private DatagramSocket m_so_tftp;   //the socket object send or get message<br>    private short m_curopcode; //the current opcode( wrq/rrq )<br>    private String m_filename;<br>    private String m_mode;<br>    private final int m_MAX_nTimeOut = 5<br></FONT></FONT><br></DIV>
[align=right][color=#000066][此贴子已经被作者于2007-5-3 21:08:20编辑过][/color][/align]

rabbit5455 发表于 2007-5-3 21:11

[原创]JAVA实现tftp服务端 -- RRQ

这里是RRQ请求的处理代码:<BR>=================================================
<P><STRONG><FONT color=#cc3300>源代码可以在这里下载:</FONT></STRONG> <a href="http://blog.bc-cn.net/UploadFiles/2007-5/53552715.zip" target="_blank" ><FONT color=#000000>UploadFiles/2007-5/53552715.zip</FONT></A></P>
<P><FONT color=#e73408><STRONG>文章在博客中的地址:</STRONG></FONT> <a href="http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml" target="_blank" ><a href="http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml" target="_blank" ><FONT color=#000000>http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml</FONT></A></A><BR>=================================================<BR><BR></P>
<P><STRONG>tftpClientAgent.java</STRONG><BR>=====================================<BR><FONT color=#006633></P>
<DIV class=htmlcode><FONT color=#006633>    public void RRQ() {<BR>        int ntimeout = this.m_MAX_nTimeOut;<BR>        try {<BR>            short nblock = 1;<BR>            //send the #0 block ACK to start the transfer<BR>            if (!this.SendFile(nblock, this.m_filename, this.m_mode)) {<BR>                this.SendERROR((short)0, "无法读取文件");<BR>                return ;<BR>            }<BR>            <BR>            //wait for the ACK packet<BR>            while (ntimeout &gt; 0) {<BR>                DatagramPacket dp;<BR>                dp = this.waitForData();<BR>                <BR>                if (dp == null) {<BR>                    //this.SendERROR((short)0, "超时了,连接被服务器断开 ...");<BR>                    //System.out.println("debug: tftpClientagent.RRQ() --&gt; timeout: " + ntimeout);<BR>                    ntimeout--;<BR>                } else {<BR>                    //ok, get a packet, check the ip and port<BR>                    if (!((dp.getAddress().equals(this.m_ClientAddress))<BR>                            &amp;&amp; (dp.getPort() == this.m_ClientPort))) {<BR>                        //ip or port has a mistake<BR>                        //this.SendERROR((short)0, "我不认识你");<BR>                        //System.out.println("debug: RRQ() --&gt; ip or port error ...");<BR>                        ntimeout--;<BR>                    } else {    //right ip and port<BR>                        //get the opcode<BR>                        DataInputStream din = new DataInputStream(new ByteArrayInputStream(dp.getData()));<BR>                        int opcode = din.readShort();<BR>                        //System.out.println("debug: the opcode is " + opcode);<BR>                        if (opcode != 4) {<BR>                            //不是04号ACK包<BR>                            if (opcode == 1 &amp;&amp; nblock == 1) {<BR>                                //是01号RRQ包,且正在等待#1号ACK包<BR>                                //此时收到RRQ,说明#1号data有可能失败了,重发<BR>                                if (!this.SendFile(nblock, this.m_filename, this.m_mode)) {<BR>                                    this.SendERROR((short)0, "无法读取文件");<BR>                                    return ;<BR>                                }<BR>                            } else {<BR>                                this.SendERROR((short)4, "非法的TFTP操作");<BR>                            }<BR>                            <BR>                            ntimeout--;<BR>                        } else {    //is an ACK packet<BR>                            //System.out.println("debug: get a ACK packet");<BR>                            int nblk = din.readShort();<BR>                            //System.out.println("debug: the waiting for #" + nblock + " -- the curBlock is #" + nblk);<BR>                            if (nblk != nblock) {<BR>                                //System.out.println("debug: re-SendFile(" + nblock + ")");<BR>                                //不是期待的块号,重发data包,超时计数减1<BR>                                if (!this.SendFile(nblock, this.m_filename, this.m_mode)) {<BR>                                    this.SendERROR((short)0, "无法读取文件");<BR>                                    return ;<BR>                                }<BR>                                ntimeout--;<BR>                            } else {    //get the right ACK packet<BR>                                //the rest of the special file<BR>                                long restSize = this.getRestOfFile(nblock, this.m_filename);<BR>                                //System.out.println("debug: the restSize = " + restSize);<BR>                                if (restSize &lt; 0) {<BR>                                    //ok, 上次已经把剩余的发送完毕了,结束<BR>                                    ntimeout = 0;<BR>                                } else {<BR>                                    //send the next block data<BR>                                    nblock++;<BR>                                    //System.out.println("debug: start sending ...");<BR>                                    if (!this.SendFile(nblock, this.m_filename, this.m_mode)) {<BR>                                        //System.out.println("debug: ERROR while sending the #" + nblock);<BR>                                        this.SendERROR((short)0, "无法读取文件");<BR>                                        ntimeout = 0;<BR>                                    } else {<BR>                                        //send succ<BR>                                        //System.out.println("debug: send the #" + nblock);<BR>                                        ntimeout = this.m_MAX_nTimeOut; //重置超时记数<BR>                                        //continue wait for ACK of the current block<BR>                                    }<BR>                                }<BR>                            }<BR>                        }<BR>                    }<BR>                }<BR>            }<BR>        } catch (Exception e) {<BR>            //System.out.println("Exception in tftpClientAgent.RRQ() --&gt; " + e.getMessage());<BR>        }<BR>    }</FONT></DIV>
<P></FONT>=============================================</P>

rabbit5455 发表于 2007-5-3 21:13

[原创]JAVA实现tftp服务端 -- WRQ

这里是WRQ请求的处理代码:<BR>=================================================
<P>
<P><STRONG><FONT color=#cc3300>源代码可以在这里下载:</FONT></STRONG> <a href="http://blog.bc-cn.net/UploadFiles/2007-5/53552715.zip" target="_blank" ><FONT color=#000000>UploadFiles/2007-5/53552715.zip</FONT></A></P>
<P><FONT color=#e73408><STRONG>文章在博客中的地址:</STRONG></FONT> <a href="http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml" target="_blank" ><a href="http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml" target="_blank" ><a href="http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml" target="_blank" ><FONT color=#000000>http://blog.bc-cn.net/user1/37/archives/2007/5202.shtml</FONT></A></A></A><BR>=================================================<BR></P>
<P><STRONG>tftpClientAgent.java<BR></STRONG>=====================================<FONT color=#009933></P>
<DIV class=htmlcode><FONT color=#009933>    public void WRQ() {<BR>        int ntimeout = this.m_MAX_nTimeOut;<BR>        try {<BR>            short nblock = 0;<BR>            //send the #0 block ACK to start the transfer<BR>            this.SendACK(nblock++);<BR>            <BR>            //wait for the data packet<BR>            while (ntimeout &gt; 0) {<BR>                DatagramPacket dp;<BR>                dp = this.waitForData();<BR>                //dp = new DatagramPacket(buf, 516);<BR>                if (dp == null) {<BR>                    //this.SendERROR((short)0, "超时了,连接被服务器断开 ...");<BR>                    //System.out.println("debug: tftpClientagent.WRQ() --&gt; timeout: " + ntimeout);<BR>                    ntimeout--;<BR>                } else {<BR>                    //ok, get a packet, check the ip and port<BR>                    if (!((dp.getAddress().equals(this.m_ClientAddress))<BR>                            &amp;&amp; (dp.getPort() == this.m_ClientPort))) {<BR>                        //ip or port has a mistake<BR>                        //this.SendERROR((short)0, "我不认识你");<BR>                        //System.out.println("debug: ip or port error ...");<BR>                        ntimeout--;<BR>                    } else {    //right ip and port<BR>                        //get the opcode<BR>                        DataInputStream din = new DataInputStream(new ByteArrayInputStream(dp.getData()));<BR>                        int opcode = din.readShort();<BR>                        //System.out.println("debug: the opcode is " + opcode);<BR>                        if (opcode != 3) {<BR>                            //不是03号data包<BR>                            if (opcode == 2 &amp;&amp; nblock == 1) {<BR>                                //是02号WRQ包,且正在等待#1号data包<BR>                                //此时收到WRQ,说明#0号ACK有可能失败了,重发<BR>                                this.SendACK((short)(nblock-1));<BR>                            } else {<BR>                                this.SendERROR((short)4, "非法的TFTP操作");<BR>                            }<BR>                            <BR>                            ntimeout--;<BR>                        } else {    //data packet<BR>                            //System.out.println("debug: get a data packet");<BR>                            int nblk = din.readShort();<BR>                            //System.out.println("debug: the waiting for #" + nblock + " -- the curBlock is #" + nblk);<BR>                            if (nblk != nblock) {<BR>                                //System.out.println("debug: re-SendACK(" + (nblock-1) + ")");<BR>                                //不是期待的块号,重发上一个包的ACK,超时计数减1<BR>                                this.SendACK((short)(nblock-1));<BR>                                ntimeout--;<BR>                            } else {    //the right packet waiting for<BR>                                //this.SaveFile(this.m_filename);<BR>                                if (!this.SaveFile(nblock, dp, this.m_filename, this.m_mode)) {<BR>                                    ntimeout = 0;<BR>                                    this.SendERROR((short)3, "磁盘满或超过分配的配额");<BR>                                } else {<BR>                                    //send the current block ACK<BR>                                    this.SendACK(nblock);<BR>                                    //System.out.println("debug: dp.length = " + (dp.getLength()-4));<BR>                                    if (dp.getLength()-4 &gt;= 512) {<BR>                                        //the transfer isn't ended<BR>                                        nblock++;   //wait for the next block<BR>                                        ntimeout = this.m_MAX_nTimeOut; //重置超时记数<BR>                                    } else {<BR>                                        //ok, there is no more data, the transfer succ<BR>                                        ntimeout = 1;<BR>                                        //continue wait for current block for 1 timeout,<BR>                                        //in case of the last ACK's failing<BR>                                    }<BR>                                }<BR>                            }<BR>                        }<BR>                    }<BR>                }<BR>            }<BR>        } catch (Exception e) {<BR>            //System.out.println("Exception in tftpClientAgent.WRQ() --&gt; " + e.getLocalizedMessage());<BR>        }<BR>    }<BR></FONT>    </DIV>
<P></FONT>======================================</P>
<P>好了,demo的代码大部分就在这里了。</P>
<P>以后继续努力,<IMG src="http://blog.bc-cn.net/editor/images/emot/face1.gif"></P>

yuyunliuhen 发表于 2007-5-3 22:07

哇噻,好强啊[em17]

rabbit5455 发表于 2007-5-5 14:52

这个是demo版本的 , 写的不好,我想逐步加入一些功能和组件,<br>如做一个连接池,这样就不用每次都new一个ClientAgent了,<br>减少线程创建和销毁的开销,还有服务器的管理员控制台功能。<br><br>但对于线程池的问题,我有些疑惑:<br>这是一个UDP的 协议,文件传输可能持续很长时间,那么这个<br>ClientAgent就没法接受新的任务,如果所有的ClientAgent都在<br>为自己的Client服务,那么新的Client来了,怎么办呢,再new一个<br>ClientAgent吗?有没有其他的方法。<br><br>希望与论坛里的各位高手交流一下看法。<br>

rabbit5455 发表于 2007-6-18 16:19

回复:(rabbit5455)这个是demo版本的 , 写的不好,...

1. 目的<br><br>TFTP是一个传输文件的简单协议,它其于UDP协议而实现,但是我们也不能确定有些TFTP协议是基于其它传输协议完成的。此协议设计的时候是进行小文件传输的。因此它不具备通常的FTP的许多功能,它只能从文件服务器上获得或写入文件,不能列出目录,不进行认证,它传输8位数据。传输中有三种模式:netascii,这是8位的ASCII码形式,另一种是octet,这是8位源数据类型;最后一种mail已经不再支持,它将返回的数据直接返回给用户而不是保存为文件。<br><br>2. 概况<br><br>任何传输起自一个读取或写入文件的请求,这个请求也是连接请求。如果服务器批准此请求,则服务器打开连接,数据以定长512字节传输。每个数据包包括一块数据,服务器发出下一个数据包以前必须得到客户对上一个数据包的确认。如果一个数据包的大小小于512字节,则表示传输结构。如果数据包在传输过程中丢失,发出方会在超时后重新传输最后一个未被确认的数据包。通信的双方都是数据的发出者与接收者,一方传输数据接收应答,另一方发出应答接收数据。大部分的错误会导致连接中断,错误由一个错误的数据包引起。这个包不会被确认,也不会被重新发送,因此另一方无法接收到。如果错误包丢失,则使用超时机制。错误主要是由下面三种情况引起的:不能满足请求,收到的数据包内容错误,而这种错误不能由延时或重发解释,对需要资源的访问丢失(如硬盘满)。TFTP只在一种情况下不中断连接,这种情况是源端口不正确,在这种情况下,指示错误的包会被发送到源机。这个协议限制很多,这是都是为了实现起来比较方便而进行的。<br><br>3. 与其它协议的联系<br><br>因为TFTP使用UDP,而UDP使用IP,IP可以还使用其它本地通信方法。因此一个TFTP包中会有以下几段:本地媒介头,IP头,数据报头,TFTP头,剩下的就是TFTP数据了。TFTP在IP头中不指定任何数据,但是它使用UDP中的源和目标端口以及包长度域。由TFTP使用的包标记(TID)在这里被用做端口,因此TID必须介于0到65,535之间。对它的初始化我们在后面讨论。TFTP头中包括两上字节的操作码,这个码指出了包的类型下面我们看看大体上的TFTP包格式,相关的内容我们在后面的章节中进行讨论。<br><br>---------------------------------------------------<br>| Local Medium | Internet | Datagram | TFTP |<br>---------------------------------------------------<br>图3-1: 包头次序<br><br>4. 初始连接<br><br>初始连接时候需要发出WRQ(请求写入远程系统)或RRQ(请求读取远程系统),收到一个确定应答,一个确定可以写出的包或应该读取的第一块数据。通常确认包包括要确认的包的包号,每个数据包都与一个块号相对应,块号从1开始而且是连续的。因此对于写入请求的确定是一个比较特殊的情况,因此它的包的包号是0。如果收到的包是一个错误的包,则这个请求被拒绝。创建连接时,通信双方随机选择一个TID,因此是随机选择的,因此两次选择同一个ID的可能性就很小了。每个包包括两个TID,发送者ID和接收者ID。这些ID用于在UDP通信时选择端口,请求主机选择ID的方法上面已经说过了,在第一次请求的时候它会将请求发到TID 69,也就是服务器的69端口上。应答时,服务器使用一个选择好的TID作为源TID,并用上一个包中的TID作为目的ID进行发送。这两个被选择的ID在随后的通信中会被一直使用。下例是一个写入的例子,其中WRQ,ACK和DATA代表写入请求,确认和数据。<br><br>1. 主机A向主机B发出WRQ,其中端口为69<br>2. B机向A机发出ACK,块号为0,包括B和A的TID<br><br>此时连接建立,第一个数据包以序列号1从主机开始发出。以后两台主机要保证以开始时确定的TID进行通信。如果源ID与原来确定的ID不一样,这个包会被认识为发送到了错误的地址而被抛弃。错误的包是被发送到正确端口的,但是包本身有错误。设想发送方发出一个请求,这个请求在网络的那个设备中被复制成两个包,接收方先后接收到两个包。接收方会认为为这是两个独立的请求,会返回两个应答。当这两个应答其中之一被接收到时,连接已经建立。第二个应答再到达时,这个包会被抛弃,而不会因为接收到第二个应答包而导致第一个建立的连接失败。<br><br>5. TFTP包<br><br>TFTP支持五种类型的包,我们在以上已经说明这五种类型的包:<br><br>opcode operation<br>1 Read request (RRQ)<br>2 Write request (WRQ)<br>3 Data (DATA)<br>4 Acknowledgment (ACK)<br>5 Error (ERROR)<br><br>包头中包括了这个包所指定的操作码。<br><br><br>2 bytes string 1 byte string 1 byte<br>------------------------------------------------<br>| Opcode | Filename | 0 | Mode | 0 |<br>------------------------------------------------<br><br>Figure 5-1: RRQ/WRQ包<br><br>RRQ和WRQ包(代码分别为1和2)的格式如上所示。文件名是NETASCII码字符,以0结束。 而MODE域包括了字符串"netascii","octet"或"mail",名称不分大小写。接收到NETASCII格式数据的主机必须将数据转换为本地格式。OCTET模式用于传输文件,这种文件在源机上以8位格式存储。假设每个机器都存在一个8位的格式,这样的假设是最一般的。比如DEC-20,这是一种36位机,我们可以假设它是4个8位外加另外4位而构成。如果机器收到OCTET格式文件,返回时必须与原来文件完全一样。在使用MAIL模式时,用户可以在FILE处使用接收人地址,这个地址可以是用户名或用户名@主机的形式,如果是后一种形式,允许主机使用电子邮件传输此文件。如果使用MAIL类型,包必须以WRQ开始,否则它与NETASCII完全一样。我们的讨论建立在发送方和接收方都在相同模式的情况下,但是双方可以以不同的模式进行传输。例如一个机器可以是一台存储服务器,这样一台服务器需要将NETASCII格式转换为自己的格式。另外,我们可以设想DEC-20这种机器,它使用36位字长,用户这边可以使用特殊的机制一次读取36位,而服务器却可以仍然使用8位格式。在这两种情况下,我们看到了两台机器使用不同格式的情况。可以在两台主机间定义其它的传输方式,但是定义要小心,因为这种传输方式不为人知,而且也没有权威机构为其指定名称或定义它的模式。<br><br>2 bytes 2 bytes n bytes<br>----------------------------------<br>| Opcode | Block # | Data |<br>----------------------------------<br>Figure 5-2: DATA包<br><br>数据在数据包中传输,其格式如上图所示。数据包的OP码为3,它还包括有一个数据块号和数据。数据块号域从1开始编码,每个数据块加1,这样接收方可以确定这个包是新数据还是已经接收过的数据。数据域从0字节到512字节。如果数据域是512字节则它不是最后一个包,如果小于512字节则表示这个包是最后一个包。除了ACK和用于中断的包外,其它的包均得到确认。发出新的数据包等于确认上次的包。WRQ和DATA包由ACK或ERROR数据包确认,而RRQ数据包由DATA或ERROR数据包确认。下图即是一个ACK包,操作码为4。其中的包号为要确认的数据包的包号。<br><br>2 bytes 2 bytes<br>---------------------<br>| Opcode | Block # |<br>---------------------<br>Figure 5-3: ACK包<br><br>WRQ数据包被ACK数据包确认,WRQ数据包的包号为0。<br><br>2 bytes 2 bytes string 1 byte<br>-----------------------------------------<br>| Opcode | ErrorCode | ErrMsg | 0 |<br>-----------------------------------------<br>Figure 5-4: ERROR包<br><br>一个ERROR包,它的操作码是5,它的格式如上所示。此包可以被其它任何类型的包确认。错误码指定错误的类型。错误的值和错误的意义在附录中。错误信息是供程序员使用的。<br><br>6. 正常终止<br><br>传输的结束由DATA数据标记,其包括0-511个字符。这个包可以被其它数据包确认。接收方在发出对最后数据包的确认后可以断开连接,当然,适当的等待是比较好的,如果最后的确定包丢失可以再次传输。如果发出确认后仍然收到最后数据包,可以确定最后的确认丢失。发送最后一个DATA包的主机必须等待对此包的确认或超时。如果响应是ACK,传输完成。如果发送方超时并不准备重新发送并且接收方有问题或网络有问题时,发送也正常结束。当然实现时也可以是非正常结束,但无论如何连接都将被关闭。<br><br>7. 早终结<br><br>如果请求不能被满足,或者在传输中发生错误,需要发送ERROR包。这仅是一种传输友好的方式,这种包不会被确认也不会被重新传输,因此这种包可能永远不会被接收到。因此需要用超时来侦测错误。<br><br>I. 附录<br><br>包头的次序<br><br>2 bytes<br>----------------------------------------------------------<br>| Local Medium | Internet | Datagram | TFTP Opcode |<br>----------------------------------------------------------<br><br>TFTP格式<br><br>Type Op # 没有包头的格式<br><br>2 bytes string 1 byte string 1 byte<br>-----------------------------------------------<br>RRQ/ | 01/02 | Filename | 0 | Mode | 0 |<br>WRQ -----------------------------------------------<br><br>2 bytes 2 bytes n bytes<br>---------------------------------<br>DATA | 03 | Block # | Data |<br>---------------------------------<br><br>2 bytes 2 bytes<br>-------------------<br>ACK | 04 | Block # |<br>--------------------<br><br>2 bytes 2 bytes string 1 byte<br>----------------------------------------<br>ERROR | 05 | ErrorCode | ErrMsg | 0 |<br>----------------------------------------<br><br>读文件的初始连接<br><br>1. 主机A发RRQ到A,包括源=A的ID和目的=69<br>2. 主机B发送DATA,其中包号=1,这个包被传送到A,源=B的ID,目的=A的ID<br><br>错误码<br><br>Value Meaning<br><br>0 未定义,请参阅错误信息(如果提示这种信息的话)<br>1 文件未找到<br>2 访问非法<br>3 磁盘满或超过分配的配额<br>4 非法的TFTP操作<br>5 未知的传输ID<br>6 文件已经存在<br>7 没有类似的用户<br><br>Internet用户数据报头<br><br>(TFTP不一定非要在UDP上实现。)<br><br>Format<br><br>0 1 2 3<br>0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1<br>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br>| Source Port | Destination Port |<br>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br>| Length | Checksum |<br>+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+<br><br>域的值<br><br>Source Port 由传输发起方选择<br>Dest. Port 由目的地选择(如果是RRQ或WRQ,其值为69)<br>Length 包括UDP包头的包长度<br>Checksum 校验码,如果是0,则未使用校验<br><br>注意:TFTP将传输标记TID传送给UDP作为源和目的端口<br><br>安全问题<br><br>因为TFTP没有安全控制机制,因此安全问题应该多加考虑。通常TFTP允许下载数据而不允许上传数据。<br><br>

rabbit5455 发表于 2007-6-18 16:21

将tftp协议的中文版放在这里吧,是在找不到地方发这个  。。。<br><br>怎么说和这个帖子也有点儿关系。。。<br>

linminna1985 发表于 2008-4-9 22:13

LZ,为什么源代码没法下载呢?

页: [1]

编程论坛