By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Oct 16 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Thu, 16 Oct 2008 11:39:01 -0700, Kumar
<Ku***@discussions.microsoft.comwrote:
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
Oct 16 '08 #2

P: n/a
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" wrote:
On Thu, 16 Oct 2008 11:39:01 -0700, Kumar
<Ku***@discussions.microsoft.comwrote:
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
Oct 16 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.