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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:42:37+00:00 2026-06-07T02:42:37+00:00

This is not a question about how to do this, but a question about

  • 0

This is not a question about how to do this, but a question about whether it’s wrong what I’m doing. I’ve read that it’s not possible to detect if a socket is closed unexpectedly (like killing the server/client process, pulling the network cable) while waiting for data (BeginReceive), without use of timers or regular sent messages, etc. But for quite a while I’ve been using the following setup to do this, and so far it has always worked perfectly.

public void OnReceive(IAsyncResult result)
{
    try
    {
        var bytesReceived = this.Socket.EndReceive(result);

        if (bytesReceived <= 0)
        {
            // normal disconnect
            return;
        }

        // ...

        this.Socket.BeginReceive...;
    }
    catch // SocketException
    {
        // abnormal disconnect
    }
}

Now, since I’ve read it’s not easily possible, I’m wondering if there’s something wrong with my method. Is there? Or is there a difference between killing processes and pulling cables and similar?

  • 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-07T02:42:38+00:00Added an answer on June 7, 2026 at 2:42 am

    It’s perfectly possible and OK to do this. The general idea is:

    If EndReceive returns anything other than zero, you have incoming data to process.

    If EndReceive returns zero, the remote host has closed its end of the connection. That means it can still receive data you send if it’s programmed to do so, but cannot send any more of its own under any circumstances. Usually when this happens you will also close your end the connection thus completing an orderly shutdown, but that’s not mandatory.

    If EndReceive throws, there has been an abnormal termination of the connection (process killed, network cable cut, power lost, etc).

    A couple of points you have to pay attention to:

    1. EndReceive can never return less than zero (the test in your code is misleading).
    2. If it throws it can throw other types of exception in addition to SocketException.
    3. If it returns zero you must be careful to stop calling BeginReceive; otherwise you will begin an infinite and meaningless ping-pong game between BeginReceive and EndReceive (it will show in your CPU usage). Your code already does this, so no need to change anything.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not a question about jQuery, but about how jQuery implements such a
This question is not technical, but is still about programming. What is the funniest,
My question is not about how to solve this error(I already solved it) but
This question is not about, whether spurious wakeups actually happen, because this was already
Disclaimer This is not a question about whether we should be escaping for database
This question pertains not only to how to accomplish this task, but to whether
I read a similar question about this that suggested using git log --graph --all
This is not a programming question per se but a question that may be
i find a lot of questions about this problem but i did not solve...
This is not a question about a specific framework. I am using plain php

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.