Click here to Skip to main content
15,890,512 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: What exactly Difference between Application and Session ? PinPopular
Abhijit Jana6-Dec-09 16:48
professionalAbhijit Jana6-Dec-09 16:48 
AnswerRe: What exactly Difference between Application and Session ? Pin
Dinesh Mani6-Dec-09 20:30
Dinesh Mani6-Dec-09 20:30 
GeneralRe: What exactly Difference between Application and Session ? Pin
Abhijit Jana6-Dec-09 20:42
professionalAbhijit Jana6-Dec-09 20:42 
GeneralRe: What exactly Difference between Application and Session ? Pin
Dinesh Mani6-Dec-09 20:49
Dinesh Mani6-Dec-09 20:49 
Question[Message Deleted] Pin
Dhisni De Costa6-Dec-09 14:02
Dhisni De Costa6-Dec-09 14:02 
AnswerRe: “Publisher could not be verified” Error message Pin
Abhijit Jana6-Dec-09 18:20
professionalAbhijit Jana6-Dec-09 18:20 
GeneralRe: “Publisher could not be verified” Error message Pin
Dhisni De Costa6-Dec-09 18:32
Dhisni De Costa6-Dec-09 18:32 
QuestionDistinguishing dialup users from broadband users without javascript Pin
Dennis Dykstra6-Dec-09 8:38
Dennis Dykstra6-Dec-09 8:38 
I'm hoping to cash in on the well-known ASP.Net expertise of CodeProject members. Currently I'm working on an ASP.Net website for a nonprofit organization with users in many developing countries, including locations where only dialup is available. The organization has determined that many of its users have disabled javascript for security reasons. The organization wants the new website to be graphically appealing, but also wants to be able to turn off most of the graphics for dialup users. This means we have to have some way of determining whether a request comes from a user on dialup or on broadband. The organization wants its home page to be filled with graphics for broadband users, which means there has to be a way to detect the connection speed in the background before sending a visible response to the browser.

At first I thought this wouldn't be possible from the server side. Although the advice I've found by searching many forums (including this one) suggested largely that this is the case, a few people thought it should be possible. Most of the suggestions involved timing downloads through WebClient or WebRequest/WebResponse. By running a number of tests on both dialup and broadband connections I've determined that the download method doesn't work because what's being timed is the length of time needed to send the download; without javascript there's no way to tell when it has actually been received by the browser.

A different approach was suggested via a PHP script posted by Emanuele Feronato on her blog[^]. This approach involves sending some text to the browser, one KB at a time, and flushing the response after each KB. She suggested sending 512 KB to get a good estimate of the connection speed, but that's far too much for dialup users to endure. I tested her approach with 16 KB and it seemed to give reasonable results. Since PHP is a server-side technology, I thought this might work in ASP.Net as well.

My C# code for the approach is below. The text I'm sending is just a collection of dots (ASCII 0x2e). To hide the text being pushed to the browser I've used a <span style="display:none;"></span> around it. Emmanuele used comment tags around the text to accomplish the same thing.

Here are a few things I've noticed:

1. The method doesn't work at all in ASP.Net unless you set Response.BufferOutput = true.

2. You have to flush the response buffer after each 1 KB or it doesn't work either.

3. Caching (or disabling caching) seems to have no effect on the results at all. I assume this is because the response is being buffered and flushed after each 1 KB.

4. For broadband connections (DSL at home and T1 at work, the latter shared with about 50 other users), I get estimated connection speeds of between 500 and 900 Kbps. This is well below the actual connection speeds for the two lines, but seems realistic in that it accounts for other things that are happening on the networks, plus transmission delays of one sort or another. Since I just want to distinguish broadband from dialup, the actual speed is not my primary interest.

5. For dialup connections the results are puzzling. On a series of tests with connections between 45.2 and 50.6 Kbps I've gotten some responses that seem reasonable (20-40 Kbps), but others that are far above what the connection should be able to support, sometimes going up to as much as 140 Kbps and in general averaging around 100 Kbps. This result is the same with Emmanuele's PHP script and with my translation of it into C#. I've determined that the results are not being served from the browser cache; everytime you hit F5 the results are different, sometimes with higher reported speeds and other times with lower speeds.

This procedure seems to permit me to distinguish between broadband and dialup users, but I'm uncomfortable with the results because of the discrepancies mentioned in point 5. I'd be grateful for any observations about what might be causing this. If I'm not timing the actual download, what am I timing instead?

Thanks for any observations or suggestions. Confused | :confused:

<<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    void Page_Load(object sender, System.EventArgs e)
        {
        Response.BufferOutput = true;
        Response.ContentType = "text/HTML";
        Response.Charset = "utf-8";

        System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

        // -------------------------------------------------------
        // Default number of KBytes to download if no QueryString.
        // -------------------------------------------------------
        int nKB = 16;
        // ---------------------------------------------------------------------------
        // If a QueryString ("?k=") was entered, get the number of KBytes to download.
        // If the value is <= 0 or > 25600, use the default instead.
        // ---------------------------------------------------------------------------
        string strK = "";
        if (!String.IsNullOrEmpty(Request.QueryString["k"]))
            {
            strK = Request.QueryString["k"];
            int nK;
            if (Int32.TryParse(strK, out nK))
                {
                if ((nK > 0) && nK <= 25600) nKB = nK;
                }
            }

        // --------------------------------------------------------
        // Create a 1-KB array of bytes filled with dot characters.
        // --------------------------------------------------------
        char[] charBuf = new char[1024];
        for (int k = 0; k < 1024; ++k) { charBuf[k] = '.'; }
        
        Response.Write("<span style=\"display:none;\">");

        // ------------------------
        // Start the clock running.
        // ------------------------
        stopWatch.Start();
        // ------------------------------------------------
        // This loop tells how many K bytes (nKB) to write.
        // ------------------------------------------------
        for (int i = 0; i < nKB; ++i)
            {
            // ----------------------------------------
            // Write 1024 bytes to the Response stream.
            // ----------------------------------------
            Response.Write(charBuf, 0, 1024);
            Response.Flush();    // Flush the output after every KB.
            }
        // ---------------
        // Stop the clock.
        // ---------------
        stopWatch.Stop();
    
        Response.Write("</span>");    // Terminate the span of hidden characters.
        Response.Flush();

        // ---------------------------------------------------------
        // Calculate the elapsed time and apparent connection speed.
        // ---------------------------------------------------------
        double dSpeed = (nKB * 8d) / stopWatch.Elapsed.TotalSeconds;
        int nKbps = (int)dSpeed;
        if (double.IsInfinity(dSpeed)) nKbps = -1;
        string dOn = "<span style=\"color:Navy; font-weight:bold;\">";
        string dOff = "</span>";
        lblSpeed.Text  = String.Format("Estimated connection speed using StopWatch = {0}{1:N0} Kbps{2},", dOn, nKbps, dOff);
        lblKBytes.Text = String.Format("based on a download of {0}{1:N0} KBytes{2}.", dOn, nKB, dOff);
        lblTime.Text   = String.Format("Elapsed time using StopWatch = {0}{1} seconds{2}.", dOn,
            stopWatch.Elapsed.TotalSeconds, dOff);
        dOn = "<span style=\"color:Red; font-weight:bold;\">";
        lblFreq.Text   = String.Format("Stopwatch frequency on server = {0}{1:N0} ticks per second{2}.", dOn,
            System.Diagnostics.Stopwatch.Frequency, dOff);
        lblIsHR.Text   = String.Format("Stopwatch using high-resolution timer on server? {0}{1}{2}.", dOn,
            System.Diagnostics.Stopwatch.IsHighResolution, dOff);
        if ((double)System.Diagnostics.Stopwatch.Frequency >= 1e9)
            lblNano.Text= String.Format("Stopwatch on server is accurate to {0}less than one nanosecond{1}.", dOn, dOff);
        else
            lblNano.Text   = String.Format("Stopwatch on server is accurate within {0}{1} nanoseconds{2}.", dOn,
                ((1000L * 1000L * 1000L) / System.Diagnostics.Stopwatch.Frequency), dOff);
        }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Check connection speed in C#</title>
</head>
<body>
    <form id="formPage" runat="server" style="font:normal 10pt Verdana;">

    <div>
        <asp:Label runat="server" ID="lblSpeed"></asp:Label><br />
        <asp:Label runat="server" ID="lblKBytes"></asp:Label><br /><br />
        <asp:Label runat="server" ID="lblTime"></asp:Label><br /><br />
        <asp:Label runat="server" ID="lblFreq"></asp:Label><br />
        <asp:Label runat="server" ID="lblIsHR"></asp:Label><br />
        <asp:Label runat="server" ID="lblNano"></asp:Label><br />
    </div>

    </form>
</body>
</html>

AnswerRe: Distinguishing dialup users from broadband users without javascript Pin
Abhishek Sur6-Dec-09 10:27
professionalAbhishek Sur6-Dec-09 10:27 
GeneralRe: Distinguishing dialup users from broadband users without javascript Pin
Dennis Dykstra6-Dec-09 14:00
Dennis Dykstra6-Dec-09 14:00 
GeneralRe: Distinguishing dialup users from broadband users without javascript Pin
Abhishek Sur6-Dec-09 20:58
professionalAbhishek Sur6-Dec-09 20:58 
QuestionServing multiple ads on single ad using AdRotator Pin
Krishhhhhhhhhhhhhh6-Dec-09 8:02
Krishhhhhhhhhhhhhh6-Dec-09 8:02 
AnswerRe: Serving multiple ads on single ad using AdRotator Pin
Abhishek Sur6-Dec-09 9:16
professionalAbhishek Sur6-Dec-09 9:16 
GeneralRe: Serving multiple ads on single ad using AdRotator Pin
Krishhhhhhhhhhhhhh6-Dec-09 19:16
Krishhhhhhhhhhhhhh6-Dec-09 19:16 
GeneralRe: Serving multiple ads on single ad using AdRotator Pin
Abhishek Sur6-Dec-09 20:56
professionalAbhishek Sur6-Dec-09 20:56 
GeneralRe: Serving multiple ads on single ad using AdRotator Pin
Krishhhhhhhhhhhhhh6-Dec-09 21:58
Krishhhhhhhhhhhhhh6-Dec-09 21:58 
QuestionHelp me update product [modified] Pin
trinhitc6-Dec-09 6:08
trinhitc6-Dec-09 6:08 
AnswerRe: Help me update product Pin
Abhishek Sur6-Dec-09 8:09
professionalAbhishek Sur6-Dec-09 8:09 
AnswerRe: Help me update product Pin
Krishhhhhhhhhhhhhh6-Dec-09 8:11
Krishhhhhhhhhhhhhh6-Dec-09 8:11 
QuestionNeed help with converting Chart().DataFormula outputs into an array/arraylist Pin
James Shao6-Dec-09 5:14
James Shao6-Dec-09 5:14 
QuestionThe problem of usng IIS for publishing ASP.net website Pin
Seraph_summer6-Dec-09 3:35
Seraph_summer6-Dec-09 3:35 
AnswerRe: The problem of usng IIS for publishing ASP.net website Pin
Abhijit Jana6-Dec-09 7:16
professionalAbhijit Jana6-Dec-09 7:16 
GeneralRe: The problem of usng IIS for publishing ASP.net website Pin
Seraph_summer7-Dec-09 9:31
Seraph_summer7-Dec-09 9:31 
GeneralRe: The problem of usng IIS for publishing ASP.net website Pin
Seraph_summer7-Dec-09 9:49
Seraph_summer7-Dec-09 9:49 
QuestionImport data from Xml to sql in vs2003 Pin
<<Tash18>>6-Dec-09 1:28
<<Tash18>>6-Dec-09 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.