Git Product home page Git Product logo

Comments (6)

9swampy avatar 9swampy commented on June 9, 2024

Can you magically predict the future? No.

Could a header inform you how much to expect? Yes.

Could you be told "ok you've got it all now, stop listening"? Yes.

The idea's you keep reading until you receive an EOF marker you can recognise or the quantity that you were told to expect. The timeouts are just there to let the code escape when something goes wrong/comms fail.

do
{
Keep listening for more with a little sanity baked in to how long you wait
} while (!EOF received)

from telnet.

sokolej79 avatar sokolej79 commented on June 9, 2024

Thank you very much for your reply and recommendation.
There is no marker at the end of file.
Could the marker be simulated with echo command and response of echo would be used as termination marker?
Based on internal working of Telnet client is this safe to relay on second echo command as marker?
Something like this sample code:


            if (!_client.IsConnected)
            {
                throw new TelnetDisconnectedException($"Error {actionMessage}. Telnet command @{command}. Telnet disconnected.");
            }

            timeoutMillis = 5000;
            terminator = "Crg_110099_Crg";// some unique string that canot be found in response text

            await _client.WriteLine("type " + filePath"); // first command read long file text 
            await _client.WriteLine($"echo {terminator}"); // !!! second command with echo unique string that will be used as terminator
 
            var response = await _client.TerminatedReadAsync(terminator, TimeSpan.FromMilliseconds(timeoutMillis));

            if (!response.Contains(terminator)) {
                bool retry = true;
                var repeats = maxRepeats;
                do
                {
                    if (repeats == 0) {
                        throw new Exception($"Read file error. Max repeats read reached. Repeated reads {maxRepeats}.");
                    }

                    if (!_client.IsConnected)
                    {
                        throw new TelnetDisconnectedException($"Error {actionMessage}. Telnet command @{command}. Telnet disconnected.");
                    }

                    var res = await _client.TerminatedReadAsync(terminator, TimeSpan.FromMilliseconds(timeoutMillis));
                    if (!string.IsNullOrWhiteSpace(res))
                    {
                        response += res;
                        if (res.Contains(terminator))
                        {
                            retry = false;
                        }
                    }
                    repeats--;
                } while (retry);
            }

            if (string.IsNullOrWhiteSpace(response))
            {
                throw new Exception($"Error {actionMessage}. Telnet command @{command}. Telnet command response is empty.");
            }

from telnet.

9swampy avatar 9swampy commented on June 9, 2024

If there is no EOF marker then there's either a header informing how much to expect, or the reader has prior knowledge. That's just how these things work.

Telnet is a very simple and "safe" protocol, it would be hard to do any damage... without malicious intent. However, I suggest you do a bit more research as the solution I proposed would be easier. At a stretch, if you own the code for both client and server negotiate before retrieving the file to find out how much to expect, then retrieve that much.

from telnet.

sokolej79 avatar sokolej79 commented on June 9, 2024

Thank you very much for your reply and recommendation.
I must say that otherwise this Telnet package is very good, thank you for this great package.
I dont own the server only the client.
Server is an old Windows Ce device, which generates csv files and Telnet client reads directory for files, parse file names and read file one by one with telnet "type "+filepath" command and move it to server archive dir and to client Pc directory, everything with Telnet commands.
Server (Windows Ce) does not have ftp support and I can only communicate with server with Telnet.
With ordinary HttpClient (Post,Put, get) I can't, it is not working.
I cannot read headers, because Telnet client has only Read methods than return only string, no headers.
Reader has no prior knowledge about how much data to expect for file.
So I dont quite understand how to get information about the file from headers or how would reader have knowledge about how much to expect?

from telnet.

9swampy avatar 9swampy commented on June 9, 2024

Maybe type UninstalItems.log && echo "EOF" would work?

Substitute "EOF" with something you know cannot be in the file.

from telnet.

sokolej79 avatar sokolej79 commented on June 9, 2024

Thank you very much for help.
I will try as you said.
As I already said great library.
I'm closing.
Thanks.

from telnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.