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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Thu, 16 Oct 2008 11:39:01 -0700, Kumar <Kumar@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

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
.



Relevant Pages

  • works on console but not when writing to a string or file
    ... I am using granados telnet client for connecting to the telnet and get the ... System.Console.Write(Encoding.ASCII.GetString(data, offset, ... in the outputdata string the last packets gets chopped off. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic programming in C
    ... int main ... char buf, ... proper string and the assignment might even be off the end of the array. ... strcpy (offset, (offset + findlen)); ...
    (comp.lang.c)
  • Re: client-server work locally but client crashes if launched elsewhere.
    ... first fatal assumption is that a recv will give you all the bytes you asked for. ... You should not use the string datatype in MFC ... have to loop receiving some small number of bytes each time until you receive your ... and the size of packets sent ...
    (microsoft.public.vc.mfc)
  • Microsoft Word Autocorrects
    ... read MS Word autocorrects file. ... private static final String AMERICAN_AUTOCORRECTS = ... int offset = 0; ... // bypass possibly multiple spacer 0s. ...
    (comp.lang.java.programmer)
  • Re: assert "foo"[3] != "foo"[3,1]...revisited
    ... string, ... cases, if an offset is negative, it is counted from the end of str. ... However, as element referencing is currently implemented, the ...
    (comp.lang.ruby)