473,379 Members | 1,283 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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
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
2 2143
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chucker | last post by:
Hi Folks, I got an Auto-Updating Application that is supposed to download all the assemblies needed from a deployment machine when started. Here ist the Code for the Launcher that has to be...
4
by: Bill Cohagan | last post by:
I'm writing a console app (in C#) and I want to be able to redirect the standard input/output streams when it's run at a command prompt. IOW I want to support the "<" and ">" redirection syntax....
4
by: Kerwin Cabrera | last post by:
I am writing a console application and I would like to put a "Press any key..." message once the program is done executing. Currently, when it executes it returns to the prompt. I have tried...
10
by: David B | last post by:
I am writing a console App with VB 2005 where I accept a text file as input, parse it and insert the text data into an SQL table. I have written the code correctly as well as I can tell because it...
4
by: eduardorp1 | last post by:
I have a GUI application that needs to be invoked from the command line as well. It should work both as a GUI and as a console application. When I invoke it from the command line, it reads all...
0
by: GT | last post by:
Hello I'm trying to embed .resource files into a Windows application in VS2005, and then compile and run the program from VS2005. This does not work because the debugger seems not find the...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
1
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I am experimenting with a concept the engineers want. I want to create an app that runs in the console. When an event happens, I want to display an image. Can I do this without having a form?...
1
by: Kurt Jakobsen | last post by:
Hi, I have problem opening an MySql connection from an win console application. I am using SharpDevelop as IDE. Previously I've been working with MySql through C# aspx and am new to writing exe...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.