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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:43:40+00:00 2026-05-14T06:43:40+00:00

How can I know when to close the socket of an HTTP client (I

  • 0

How can I know when to close the socket of an HTTP client (I mean a browser is connecting to my TCP socket server). Everything works perfect but in other for the browser to show what the server has sent, i have to shutdown the server (or call socket.Close()).

It works fine if I call (socket.Close()) but I just don’t know when.? I don’t want to call close in a middle of a request otherwise the browser would have to reload to connect to the server again.

  • 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-14T06:43:40+00:00Added an answer on May 14, 2026 at 6:43 am

    You need to shutdown your end of the socket so the client knows there will be no more data. You also need to read any pending data because if you call Close() right away the client will get a reset (TCP RST).

    With the code above, once the client closes its end or the timeout you define is up, the connection will finally be closed on our end.

    What I did a long time ago was this (for Mono’s ASP.NET server):

        void LingeringClose ()
        {
            int waited = 0;
    
            if (!Connected)
                return;
    
            try {
                Socket.Shutdown (SocketShutdown.Send);
                DateTime start = DateTime.UtcNow;
                while (waited < max_useconds_to_linger) {
                    int nread = 0;
                    try {
                        if (!Socket.Poll (useconds_to_linger, SelectMode.SelectRead))
                            break;
    
                        if (buffer == null)
                            buffer = new byte [512];
    
                        nread = Socket.Receive (buffer, 0, buffer.Length, 0);
                    } catch { }
    
                    if (nread == 0)
                        break;
    
                    waited += (int) (DateTime.UtcNow - start).TotalMilliseconds * 1000;
                }
            } catch {
                // ignore - we don't care, we're closing anyway
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know how I can close all modal dialogs created by Dojo ?
Is it possible to close an InfoPath form programmatically? I know that it can
I know you can look at the row.count or tables.count, but are there other
How can I know in a C#-Application, in which direction the screen of the
How can I know which machines are alive on the same LAN as my
In other words, can fn() know that it is being used as $var =
Me stupid. How can I know which assembly to reference if I just know
When I get a reference to a System.Diagnostics.Process , how can I know if
I have a git repository with multiple branches. How can I know which branches
I know I can do most of this by hacking Trac and using Git

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.