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

  • SEARCH
  • Home
  • 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 6611473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:58:26+00:00 2026-05-25T19:58:26+00:00

This is a continuation of the this question. I am new to network programming,

  • 0

This is a continuation of the this question. I am new to network programming, so I am just writing small sample stuff to gain understanding, but somewhat struggling with explaining results.

It seems setting NetworkStream.ReceiveTimeout is not working correctly when client that was supposed to be sending data simply closes before sending all the expected data.

Here is the sample code:

public static void Main(string[] args)
{
    TcpListener listener = new TcpListener(IPAddress.Any, 10001);
    listener.Start();

    ThreadPool.QueueUserWorkItem(WriterThread);

    using (TcpClient client = listener.AcceptTcpClient())
    using (NetworkStream stream = client.GetStream())
    {
        client.ReceiveTimeout = (int)new TimeSpan(0, 0, 2).TotalMilliseconds;
        stream.ReadTimeout = (int)new TimeSpan(0, 0, 2).TotalMilliseconds;
        ReceiveMessage(stream, 1024);
    }

    listener.Stop();

    Console.WriteLine("Done.");
    Console.ReadKey(true);
}


private static void WriterThread(object state)
{
    using (TcpClient client = new TcpClient())
    {
        client.Connect(new IPEndPoint(IPAddress.Loopback, 10001));
        using (NetworkStream stream = client.GetStream())
        {
            byte[] bytes = Encoding.ASCII.GetBytes("obviously less than 1024 bytes");
            stream.Write(bytes, 0, bytes.Length);

            Thread.Sleep(10000); // comment out 
        }
    }
}


private static byte[] ReceiveMessage(Stream stream, int length)
{
    byte[] buffer = new byte[length];
    int bufferFill = 0;

    while (true)
    {
        bufferFill += stream.Read(buffer, bufferFill, buffer.Length - bufferFill);
        if (buffer.Length == bufferFill)
            return buffer;

        Thread.Sleep(100);
    }
}

This version works correctly triggering exception on the stream.Read() call. However If I comment out Thread.Sleep(10000), the client closes connection, but listener fails to recognize it. Main thread gets stuck inside the while(true) loop. The stream.Read() keeps returning zero, but no exception thrown.

Is this normal? If so how am I expected to handle abnormal client disconnections?

  • 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-05-25T19:58:27+00:00Added an answer on May 25, 2026 at 7:58 pm

    Yes, this sounds normal. There is no receive- or read timeout because the client has disconnected. This means that no more data is available for reading and the stream will return 0 immediately just as documented.

    I would modify your ReceiveMessage method to something like the following:

    private static byte[] ReceiveMessage(Stream stream, int length)
    {
       byte[] buffer = new byte[length];
       int bufferFill = 0;
    
       while (true)
       {
          int bytesRead = stream.Read(buffer, bufferFill, buffer.Length - bufferFill);
          if (bytesRead == 0)
             throw new Exception("No more data available.");
          bufferFill += bytesRead;
          if (buffer.Length == bufferFill)
             return buffer;
    
          Thread.Sleep(100);
       }
    }
    

    Clearly if the stream.Read() call returns 0 before we have received all the expected bytes there must have been some form of disconnection or similar. Either way we will never get any more data from the stream.

    Edit: The Stream class has no notion of a “message”. The Read method blocks until more data becomes available if none is already in the buffer. It will however return 0 when no more data can be received, which in this case means the connection is closed.

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

Sidebar

Related Questions

If anyone read my last question, this is somewhat of a continuation of it.
This is a continuation from the previous stackoverflow jQuery question I asked, but I
This is a continuation question from a previous question I have asked I now
This is a continuation of my question about reading the superblock . Let's say
This is in continuation with the question posted here: Finding the center of mass
This is a continuation of this question: Original Question (SO) The answer to this
This is a continuation of the question posted in: How to load a jar
This is a continuation of the question here: JBoss - does app have to
And my confusion with JSF continues. This is a continuation of a question asked
Ok - this is in continuation from my earlier question about sending an email

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.