473,699 Members | 2,364 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(output data);
//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 2164
On Thu, 16 Oct 2008 11:39:01 -0700, Kumar
<Ku***@discussi ons.microsoft.c omwrote:
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(output data);
//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.Cl ose() 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***@discussi ons.microsoft.c omwrote:
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(output data);
//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.Cl ose() 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
1502
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 deployed to the Machine actually running the Code. All other Stuff should be downloaded upon execution time. using System;
4
8739
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. The obvious way to do this is by using the static Console type properties, In and Out. When trying to debug the app in the IDE however, this doesn't appear to work. I've edited the project properties and added the necessary text to the "command...
4
30363
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 console.read and console.readline but they both require an <Enter> key to be hit. Any suggestions Thanks -KC
10
2233
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 works running it within the IDE, given that you have to go into the Debug Project Properties to specify the argument. If I build the file and go run it from the command line, specifying the argument, I get the following error: ...
4
8627
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 the command line options perfectly. However, I can't use Console.WriteLine to write messages to the console (the lines don't appear at the console window). Is it possible? What should I do to "connect" the console? My Main() is:
0
1492
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 resource files, and I keep getting the following error: "MissingManifestResourceException: Could not find any resources appropriate for the specified culture (or the neutral culture) on disk. baseName: String locationInfo: <null fileName:
1
2038
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 error: The CLR has been unable to transition from COM context 0x1a2008 to COM context 0x1a2178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long...
1
1472
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? Right now, my app compiles, but crashes with an unhandled exception at this part: //
1
4793
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 programs in C#, so I assume that I might be missing something in the IDE / project configuration. I am successfully opening a connection and reading data from the table using adapter (window panel application binding to a grid), but when trying the...
0
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8884
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.