Comments (6)
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.
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.
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.
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.
Maybe type UninstalItems.log && echo "EOF"
would work?
Substitute "EOF" with something you know cannot be in the file.
from telnet.
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)
- Weird response from server HOT 1
- LiteGuard dependency conflict HOT 7
- [Question] Added Telnet Server to main project HOT 2
- Console.write in TryLoginAsync intentional? HOT 3
- Particular Example Request HOT 1
- CancellationToken functionality at creating Client Instance HOT 1
- TryLoginAsync timeout HOT 1
- TryLoginAsync attempt 2 HOT 3
- Sending UTF-8 characters in XML file HOT 3
- Remove Console.Write() from TerminatedReadAsync HOT 2
- TelnetServer? HOT 2
- Confusing use of Timeout HOT 1
- No stated dependency on Microsoft.VisualStudio.Threading, Version=17.3.0.0 HOT 6
- Update function TryLoginAsync with Regex terminator string HOT 1
- Client keeps disconnecting HOT 2
- Telnet Login HOT 1
- How to read UTF8 strings? HOT 2
- How to filter out remote echo of my commands HOT 1
- DONT/WONT case should read byte to the end of IAC
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from telnet.