works on console but not when writing to a string or file

K

Kumar

I am using granados telnet client for connecting to the telnet and get the
data from it. Every thing appears to be going smooth. But for some reason
when I try to write the byte data to a string or streamwriter, it looses the
final packet. Strangely, If I output the datapackets to a console from the
telnet server, it perfectly gets all the output packets.

the code snippet is below:

StreamWriter sw = new StreamWriter("c:\\output.txt", true);

public void OnData(byte[] data, int offset, int length)
{
System.Console.Write(Encoding.ASCII.GetString(data, offset,
length));
outputdata = (Encoding.ASCII.GetString(data, offset, length));
sw.Write(outputdata);
//sw.Close();
}

in the above,
System.Console.Write(Encoding.ASCII.GetString(data, offset, length));

outputs the data correctly, however,
in the outputdata string the last packets gets chopped off.

Can someone help me understand what's happening behind. Or is it something I
am missing while writing to a string or file.
 
K

Kumar

Yes you are right. I didn't close it and so the final packet is chopped off
when writing to the file. It was kind of a listener and so I had declared it
above the procedure and let it open. however, once I moved the streawriter
declaration in to the procedure and making it appendable plus closing at the
end did solve the problem.

Thanks anyway your suggestion helped.

Peter Duniho said:
I am using granados telnet client for connecting to the telnet and get
the
data from it. Every thing appears to be going smooth. But for some reason
when I try to write the byte data to a string or streamwriter, it looses
the
final packet. Strangely, If I output the datapackets to a console from
the
telnet server, it perfectly gets all the output packets.

Why is that strange? I would think that what's strange is that your code
doesn't work, not that some other code does. :)
the code snippet is below:

StreamWriter sw = new StreamWriter("c:\\output.txt", true);

public void OnData(byte[] data, int offset, int length)
{
System.Console.Write(Encoding.ASCII.GetString(data, offset,
length));
outputdata = (Encoding.ASCII.GetString(data, offset,
length));
sw.Write(outputdata);
//sw.Close();
}

in the above,
System.Console.Write(Encoding.ASCII.GetString(data, offset, length));

outputs the data correctly, however,
in the outputdata string the last packets gets chopped off.

Are you _sure_ that "in the outputdata string the last packets gets
chopped off"? That is, have you stepped through the debugger and
confirmed that you are missing data in the string itself?
Can someone help me understand what's happening behind. Or is it
something I
am missing while writing to a string or file.

Well, commenting out the call to StreamWriter.Close() can't possibly be
helping matters. Failing to close a StreamWriter when you're done with it
is certainly one way that files wind up shorted of data.

But you've specifically said the data is lost before you even get as far
as writing to the StreamWriter, which implies a problem somewhere else.

Unfortunately, without a concise-but-complete code sample that reliably
demonstrates the problem, it's nearly impossible to provide any specific
advice. At the very least, you need to be more precise about the problem
description.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top