Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:18:26+00:00 2026-06-15T21:18:26+00:00

I am using StreamWriter instantiated over a Tcpstream like this streamWriter = new StreamWriter(tcpClient.GetStream());

  • 0

I am using StreamWriter instantiated over a Tcpstream
like this

streamWriter = new StreamWriter(tcpClient.GetStream());    

I am confused about the behavior of following calls with regards to Exceptions. The following two functions are expected to raise IOException Surprisingly they do not raise the IOException when the server to which the tcpClient is connected is disconnected and therefore the underlying TCP client connection is broken.. These two lines execute without raising any Exception. Why ?

streamWriter.WriteLine(strBuffer);
streamWriter.Flush();

EDIT: This particular post also helped me understand the reason, besides the replies here from the experts.
http://social.msdn.microsoft.com/Forums/sk/netfxnetcom/thread/b177a935-7430-4812-9555-f747748df1af
In essence the NetworkStream.Write is called when WriteLine of StreamWriter is called, This writes to the out-going buffer and returns, without blocking, unless if the buffer was full. The internal buffer maintained by OS contains unsent data, which OS will keep trying to send until a given number of attempts fail.
WriteLine will throw IOException but not necessarily every time..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:18:27+00:00Added an answer on June 15, 2026 at 9:18 pm

    This issue is unrelated to StreamWriter, it’s only about the behavior of NetworkStream.

    NetworkStream.Flush() is a no-op. In particular is does not block until the recipient has acknowledged all bytes sent prior to the flush.

    The Flush method implements the Stream.Flush method; however, because NetworkStream is not buffered, it has no affect on network streams. Calling the Flush method does not throw an exception.

    (from NetworkStream.Flush Method (MSDN))

    You will get your exception at some point, but that can be much later. Essentially neither NetworkStream nor the OS know that the TCP connection is already broken, so they won’t raise an exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see this: using (StreamWriter sw = new StreamWriter(file.txt)) { // d0 w0rk s0n
Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter =
A few days ago I posted some code like this: StreamWriter writer = new
This works: using (StreamWriter stw = new StreamWriter(Server.MapPath(\\xml\\file.xml))) { stw.Write(xmlEncStr); } This creates an
Given this example code: 1: using (StreamWriter sw = new StreamWriter(@C:\file.txt)) 2: { 3:
I'm using StreamWriter to write log file. And this is the writing log method,
My root problem is that when using calls Dispose on a StreamWriter , it
I get the following message back when trying to retrieve a file using TCPClient
I'm using StreamWriter to output a text file, but I would like to end
Before writing to the file: using (StreamWriter outfile = new StreamWriter(filePath, false, System.Text.Encoding.UTF8)) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.