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 8318929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:08:46+00:00 2026-06-08T22:08:46+00:00

I was looking how to detect a ‘client disconnect’ when using a TcpListener. All

  • 0

I was looking how to detect a ‘client disconnect’ when using a TcpListener.

All the answers seem to be similar to this one:
TcpListener: How can I detect a client disconnect?

Basically, read from the stream and if Read() returns 0 the client had disconnected.

But that’s assuming that a client disconnects after every single stream of data it sent.
We operate in environments where the TCP connect/disconnect overhead is both slow and expensive.

We establish a connection and then we send a number of requests.

Pseudocode:

client.Connect();
client.GetStatus();
client.DoSomething();
client.DoSomethingElse();
client.AndSoOn();
client.Disconnect();

Each call between Connect and Disconnect() sends a stream of data to the server.
The server knows how to analyze and process the streams.

If let the TcpListener read in a loop without ever disconnecting it reads and handles all the messages, but after the client disconnects, the server has no way of knowing that and
it will never release the client and accept new ones.

var read = client.GetStream().Read(buffer, 0, buffer.Length);

if (read > 0) 
{
   //Process
}

If I let the TcpListener drop the client when read == 0 it only accepts
the first stream of data only to drop the client immediately after.

Of course this means new clients can connect.

There is no artificial delay between the calls,
but in terms of computer time the time between two calls is ‘huge’ of course,
so there will always be a time when read == 0 even though that does not mean
the client has or should be disconnected.

var read = client.GetStream().Read(buffer, 0, buffer.Length);

if (read > 0) 
{
   //Process
}
else
{
   break;   //Always executed as soon as the first stream of data has been received
}

So I’m wondering… is there a better way to detect if the client has disconnected?

  • 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-08T22:08:48+00:00Added an answer on June 8, 2026 at 10:08 pm

    You could get the underlying socket using the NetworkStream.Socket property and use it’s Receive method for reading.

    Unlike NetworkStream.Read, the linked overload of Socket.Receive will block until the specified number of bytes have been read, and will only return zero if the remote host shuts down the TCP connection.


    UPDATE: @jrh’s comment is correct that NetworkStream.Socket is a protected property and cannot be accessed in this context. In order to get the client Socket, you could use the TcpListener.AcceptSocket method which returns the Socket object corresponding to the newly established connection.

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

Sidebar

Related Questions

How can I detect onKeyUp in AngularJS? I'm looking for an 'ngOnkeyup' directive, similar
I'm looking for a way to detect file corruption using C#. Maybe this is
I'm looking for a method, or a way to detect clients using any type
Im looking for a way to detect if the user is using Opera Mini
We are looking at using a library to help us detect SQL injections. We
I'm looking for .NET static code analysis tools that can detect things like unit
I'm looking for the best way to detect more than one finger on the
How can I detect when a user resizes the browser window? I'm looking for
I have been looking into training/using OpenCV to attempt to detect human figures. I
Just wondering if anyone has come across this? Basically, Im looking to detect for

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.