我下载了一个C#开发的tftp的客户端,然后用它上传文件,时常文件传不全,就是说1M的文件只传了500K,但不是所有的文件,而且同一个文件每次传的情况不一样,有时候能传全有时候不行,不知道为什么,请各位大虾帮忙!
 public bool Put(string LocalFile, string RemoteFile, string Host,
            Modes Mode, int BlockSize, int Timeout)
        {
            int[] block = new int[2];
            int bufferSize = BlockSize;
            long fileSize, bytesSent = 0;            BinaryReader BReader = new BinaryReader(File.Open(LocalFile, FileMode.Open));
            FileInfo sendFile = new FileInfo(LocalFile);            TFTPSession.OpCodes opCode = new TFTPSession.OpCodes();            IPHostEntry hostInfo = Dns.GetHostEntry(Host);
            IPAddress address = hostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(address, 69);
            EndPoint localEP = (remoteEP);
            Socket UDPSock = new Socket
                (remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp);            // Retrieve filesize for tsize option
            fileSize = sendFile.Length;            // Create initial request and buffer for response
            byte[] sendData = _packetBuilder.Request (TFTPSession.OpCodes.WRQ,
                RemoteFile, Mode, BlockSize, fileSize, Timeout);
            byte[] recvData = new byte[bufferSize];            UDPSock.ReceiveTimeout = Timeout * 1000;            // Send request and wait for response
            UDPSock.SendTo(sendData, remoteEP);
            UDPSock.ReceiveFrom(recvData, ref localEP);            //Get TID
            remoteEP.Port = ((IPEndPoint)localEP).Port;            // Invoke Connected Event
            Connected.Invoke();            while (true)
            {
                // Read opcode
                opCode = _packetReader.ReadOpCode(recvData);                // ACK packet
                if (opCode == TFTPSession.OpCodes.ACK)
                {
                    block = _packetBuilder.IncrementBock(recvData, block);                    sendData = BReader.ReadBytes(bufferSize);
                    bytesSent += sendData.Length;                    // Invoke Transferring Event
                    Transferring.Invoke(bytesSent, fileSize);                    sendData = _packetBuilder.Data(sendData, block[0], block[1]);                    // Check if this packet is the last
                    if (sendData.Length < bufferSize + 4)
                    {
                        // Send final data packet and wait for ack
                        while (true)
                        {
                            UDPSock.SendTo(sendData, remoteEP);
                            UDPSock.ReceiveFrom(recvData, ref localEP);
                            remoteEP.Port = ((IPEndPoint)localEP).Port;                            // Check the blocks and break free if equal
                            if (_packetReader.CompareBlocks(sendData, recvData))
                                break;
                        }                        // Invoke TransferFinished Event
                        TransferFinished.Invoke();
                        break;
                    }
                }                // OACK packet
                else if (opCode == TFTPSession.OpCodes.OACK)
                {
                    sendData = BReader.ReadBytes(bufferSize);
                    sendData = _packetBuilder.Data(sendData, 0, 1);
                    bytesSent += sendData.Length - 4;                    // Invoke Transferring Event
                    Transferring.Invoke(bytesSent, fileSize);                    if (fileSize == 0)
                    {
                        // Invoke TransferFinished Event
                        TransferFinished.Invoke();
                        break;
                    }
                    else
                    {                        // Check if this packet is the last
                        if (sendData.Length < bufferSize + 4)
                        {
                            // Send final data packet and wait for ack
                            while (true)
                            {
                                UDPSock.SendTo(sendData, remoteEP);
                                UDPSock.ReceiveFrom(recvData, ref localEP);
                                remoteEP.Port = ((IPEndPoint)localEP).Port;                                // Check the blocks and break free if equal
                                if (_packetReader.CompareBlocks(sendData, recvData))
                                    break;
                            }
                            // Invoke TransferFinished Event
                            TransferFinished.Invoke();
                            break;
                        }
                    }
                }
                else if (opCode == TFTPSession.OpCodes.ERROR)
                {
                    ErrorPacket transferError = _packetReader.ReadError(recvData);
                    TransferFailed.Invoke(transferError.Code, transferError.Message);
                    break;
                }                // Send next packet
                UDPSock.SendTo(sendData, remoteEP);
                UDPSock.ReceiveFrom(recvData, ref localEP);
                remoteEP.Port = ((IPEndPoint)localEP).Port;
            }
            BReader.Close();
            UDPSock.Close();            // Invoke Disconnected Event
            Disconnected.Invoke();            return true;
        }

解决方案 »

  1.   

    老师说过,即使不懂也要把空格写满。也许会给分
    public bool Put(string LocalFile, string RemoteFile, string Host, 
                Modes Mode, int BlockSize, int Timeout) 
            { 
                int[] block = new int[2]; 
                int bufferSize = BlockSize; 
                long fileSize, bytesSent = 0;             BinaryReader BReader = new BinaryReader(File.Open(LocalFile, FileMode.Open)); 
                FileInfo sendFile = new FileInfo(LocalFile);             TFTPSession.OpCodes opCode = new TFTPSession.OpCodes();             IPHostEntry hostInfo = Dns.GetHostEntry(Host); 
                IPAddress address = hostInfo.AddressList[0]; 
                IPEndPoint remoteEP = new IPEndPoint(address, 69); 
                EndPoint localEP = (remoteEP); 
                Socket UDPSock = new Socket 
                    (remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp);             // Retrieve filesize for tsize option 
                fileSize = sendFile.Length;             // Create initial request and buffer for response 
                byte[] sendData = _packetBuilder.Request (TFTPSession.OpCodes.WRQ, 
                    RemoteFile, Mode, BlockSize, fileSize, Timeout); 
                byte[] recvData = new byte[bufferSize];             UDPSock.ReceiveTimeout = Timeout * 1000;             // Send request and wait for response 
                UDPSock.SendTo(sendData, remoteEP); 
                UDPSock.ReceiveFrom(recvData, ref localEP);             //Get TID 
                remoteEP.Port = ((IPEndPoint)localEP).Port;             // Invoke Connected Event 
                Connected.Invoke();             while (true) 
                { 
                    // Read opcode 
                    opCode = _packetReader.ReadOpCode(recvData);                 // ACK packet 
                    if (opCode == TFTPSession.OpCodes.ACK) 
                    { 
                        block = _packetBuilder.IncrementBock(recvData, block);                     sendData = BReader.ReadBytes(bufferSize); 
                        bytesSent += sendData.Length;                     // Invoke Transferring Event 
                        Transferring.Invoke(bytesSent, fileSize);                     sendData = _packetBuilder.Data(sendData, block[0], block[1]);                     // Check if this packet is the last 
                        if (sendData.Length < bufferSize + 4) 
                        { 
                            // Send final data packet and wait for ack 
                            while (true) 
                            { 
                                UDPSock.SendTo(sendData, remoteEP); 
                                UDPSock.ReceiveFrom(recvData, ref localEP); 
                                remoteEP.Port = ((IPEndPoint)localEP).Port;                             // Check the blocks and break free if equal 
                                if (_packetReader.CompareBlocks(sendData, recvData)) 
                                    break; 
                            }                         // Invoke TransferFinished Event 
                            TransferFinished.Invoke(); 
                            break; 
                        } 
                    }                 // OACK packet 
                    else if (opCode == TFTPSession.OpCodes.OACK) 
                    { 
                        sendData = BReader.ReadBytes(bufferSize); 
                        sendData = _packetBuilder.Data(sendData, 0, 1); 
                        bytesSent += sendData.Length - 4;                     // Invoke Transferring Event 
                        Transferring.Invoke(bytesSent, fileSize);                     if (fileSize == 0) 
                        { 
                            // Invoke TransferFinished Event 
                            TransferFinished.Invoke(); 
                            break; 
                        } 
                        else 
                        {                         // Check if this packet is the last 
                            if (sendData.Length < bufferSize + 4) 
                            { 
                                // Send final data packet and wait for ack 
                                while (true) 
                                { 
                                    UDPSock.SendTo(sendData, remoteEP); 
                                    UDPSock.ReceiveFrom(recvData, ref localEP); 
                                    remoteEP.Port = ((IPEndPoint)localEP).Port;                                 // Check the blocks and break free if equal 
                                    if (_packetReader.CompareBlocks(sendData, recvData)) 
                                        break; 
                                } 
                                // Invoke TransferFinished Event 
                                TransferFinished.Invoke(); 
                                break; 
                            } 
                        } 
                    } 
                    else if (opCode == TFTPSession.OpCodes.ERROR) 
                    { 
                        ErrorPacket transferError = _packetReader.ReadError(recvData); 
                        TransferFailed.Invoke(transferError.Code, transferError.Message); 
                        break; 
                    }                 // Send next packet 
                    UDPSock.SendTo(sendData, remoteEP); 
                    UDPSock.ReceiveFrom(recvData, ref localEP); 
                    remoteEP.Port = ((IPEndPoint)localEP).Port; 
                } 
                BReader.Close(); 
                UDPSock.Close();             // Invoke Disconnected Event 
                Disconnected.Invoke();             return true; 
            }