This project has moved. For the latest updates, please go here.

bug in TFTP upload?

Feb 17, 2015 at 3:02 PM
Edited Feb 17, 2015 at 3:04 PM
Hi,

The Tftp.Net download example works OK for me but I cannot get a TFTP upload to work. I get a ObjectDisposed exception after receiving the ACK to the initial WRITE_REQJUEST.

If i debug using the full source package it appears that the Filestream (Context.InputOutputStream) I opened to start the transfer has got "closed" by the time the ACK handler goes to send the first DATA block of the upload. (stack trace at end)

Is my client code (below) correct?

Regards,
Fergal.

-------------start client code----------
    public static FileStream stream;
    private static String fileName = "x.bin";

    static void Main(string[] args)
    {
        //Setup a TftpClient instance
        var client = new TftpClient("10.0.0.2");

        //Prepare a simple transfer 
        var transfer = client.Upload("x.bin");
        transfer.TransferMode = TftpTransferMode.octet;

        //Capture the events that may happen during the transfer
        transfer.OnProgress += new TftpProgressHandler(transfer_OnProgress);
        transfer.OnFinished += new TftpEventHandler(transfer_OnFinshed);
        transfer.OnError += new TftpErrorHandler(transfer_OnError);

        //Start the transfer 
        using (stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            transfer.Start(stream);
        }

        //Wait for the transfer to finish
        TransferFinishedEvent.WaitOne();
        Console.ReadKey();
    }
-------------end client code----------

-----------------------start stack strace ----------------
Tftp.Net.dll!Tftp.Net.Transfer.States.Sending.SendNextPacket(ushort blockNumber) Line 62 C#
Tftp.Net.dll!Tftp.Net.Transfer.States.Sending.OnStateEnter() Line 21 + 0xd bytes    C#
Tftp.Net.dll!Tftp.Net.Transfer.TftpTransfer.SetState(Tftp.Net.Transfer.States.ITransferState newState) Line 68 + 0xc bytes  C#
Tftp.Net.dll!Tftp.Net.Transfer.States.SendWriteRequest.BeginSendingTo(System.Net.EndPoint endpoint) Line 56 + 0x2b bytes    C#
Tftp.Net.dll!Tftp.Net.Transfer.States.SendWriteRequest.OnCommand(Tftp.Net.ITftpCommand command, System.Net.EndPoint endpoint) Line 37 + 0xb bytes   C#
Tftp.Net.dll!Tftp.Net.Transfer.TftpTransfer.connection_OnCommandReceived(Tftp.Net.ITftpCommand command, System.Net.EndPoint endpoint) Line 52 + 0x18 bytes  C#
Tftp.Net.dll!Tftp.Net.Channel.UdpChannel.RaiseOnCommand(Tftp.Net.ITftpCommand command, System.Net.IPEndPoint endpoint) Line 80 + 0x1a bytes C#
Tftp.Net.dll!Tftp.Net.Channel.UdpChannel.UdpReceivedCallback(System.IAsyncResult result) Line 67 + 0xe bytes    C#
[External Code] 
-----------------------end stack strace ----------------
Coordinator
Feb 18, 2015 at 7:15 AM
Hi,

your code should work when you remove the "using" statement. The TftpTransfer will take care of closing your stream when the transfer is done.
If you wrap it into the using statement, the stream will be closed before the Transfer is done, which basically results in the error you are seeing.

The following example should work as expected:
    public static FileStream stream;
    private static String fileName = "x.bin";

    static void Main(string[] args)
    {
        //Setup a TftpClient instance
        var client = new TftpClient("10.0.0.2");

        //Prepare a simple transfer 
        var transfer = client.Upload("x.bin");
        transfer.TransferMode = TftpTransferMode.octet;

        //Capture the events that may happen during the transfer
        transfer.OnProgress += new TftpProgressHandler(transfer_OnProgress);
        transfer.OnFinished += new TftpEventHandler(transfer_OnFinshed);
        transfer.OnError += new TftpErrorHandler(transfer_OnError);

        //Start the transfer 
        transfer.Start(new FileStream(fileName, FileMode.Open, FileAccess.Read));

        //Wait for the transfer to finish
        TransferFinishedEvent.WaitOne();
        Console.ReadKey();
    }
Best regards,
Michael
Feb 18, 2015 at 8:01 AM
thanks - works now.
Fergal.