C# Web Server Connection Problem

troubleshooting asp.net web applications
contacting web server to start debugging
unable to start debugging on the web server. the operation has timed out
server error in '/' application.
production troubleshooting .net applications
unable to start debugging on the web server. a debugger is already attached
http error 500.19 - internal server error
unable to start debugging on the web server. the web server is not configured correctly

I'm trying to make a simple C# web server that, at this stage, you can access via your browser and will just do a "Hello World".

The problem I'm having is that the server can receive data fine - I get the browser's header information - but the browser doesn't receive anything I send. Furthermore, I can only connect to the server by going to localhost (or I can't get to it by going to my IP and it's not a network setting because Apache works fine if I run that instead. Also, I'm using a port monitoring program and after I attempt a connection from a browser, the process's port gets stuck in a TIME_WAIT state even though I told the connection to close and it should be back to LISTEN.

Here's the relevant code. A couple calls might not make sense but this is a piece of a larger program.

class ConnectionHandler
    private Server server;

    private TcpListener tcp;

    private ArrayList connections;

    private bool listening;

    private Thread listeningThread;

    public Server getServer()
        return server;

    private void log(String s, bool e)
        server.log("Connection Manager: " + s, e);

    private void threadedListen()
        while (listening)
                TcpClient t = tcp.AcceptTcpClient();
                Connection conn = new Connection(this, t);
            catch (NullReferenceException)
                log("unable to accept connections!", true);
        log("Stopped listening", false);

    public void listen()
        log("Listening for new connections", false);
        listening = true;
        if (listeningThread != null && listeningThread.IsAlive)
        listeningThread = new Thread(new ThreadStart(

    public void stop()
        listening = false;
        if (listeningThread != null)
            log("Forced stop", false);
        log("Stopped listening", false);

    public ConnectionHandler(Server server)
        this.server = server;
        tcp = new TcpListener(new IPEndPoint(
            IPAddress.Parse(""), 80));
        connections = new ArrayList();

class Connection
    private Socket socket;
    private TcpClient tcp;

    private ConnectionHandler ch;

    public Connection(ConnectionHandler ch, TcpClient t)
            this.ch = ch;
            this.tcp = t;
            ch.getServer().log("new tcp connection to " 
                + this.tcp.Client.RemoteEndPoint.ToString(), false);
            NetworkStream ns = t.GetStream();

            String responseString;
            Byte[] response;
            Int32 bytes;
            responseString = String.Empty;
            response = new Byte[512];
            bytes = ns.Read(response, 0, response.Length);
            responseString = 
                System.Text.Encoding.ASCII.GetString(response, 0, bytes);
            ch.getServer().log("Received: " + responseString);

            String msg = "<html>Hello World</html>";
            String fullMsg = "HTTP/1.x 200 OK\r\n"
                    + "Server: Test Server\r\n"
                    + "Content-Type: text/html; "
                        + "charset=UTF-8\r\n"
                    + "Content-Length: " + msg.Length + "\r\n"
                    + "Date: Sun, 10 Aug 2008 22:59:59 GMT"
                        + "\r\n";
            nsSend(fullMsg, ns);
            nsSend(msg, ns);

        catch (ArgumentNullException e)
            ch.getServer().log("connection error: argument null exception: " + e);
        catch (SocketException e)
            ch.getServer().log("connection error: socket exception: " + e);

    private void nsSend(String s, NetworkStream ns)
        Byte[] data = System.Text.Encoding.ASCII.GetBytes(s);
        ns.Write(data, 0, data.Length);
        ch.getServer().log("Sent: " + s);

Does anyone have any ideas? It feels like it's gotta be something stupid on my part but I just don't know what. I'd really appreciate any insight

You might like to know that you can use HttpListener to write a basic web-server very easily - this deals with most of the painful bits, letting you concentrate on writing the actual code. The MSDN page gives an example.

Note that this uses HTTP.SYS, which is good - but means that non-admins need to be explicitly given access to open ports; on xp you can do this with httpcfg; on vista you can use netsh. See here for more.

How to Troubleshoot ASP.NET Web Application Problems, If your IIS Application Pool is stopped, you will get only 503 errors on every This makes it hard to troubleshoot ASP.NET problems. This sample application (shown below) occasionally has problems connecting to Redis. Right click on the project and select properties Go to the Debug section Under Web Server Settings change App URL port [just increase by one]

One thing which isn't a problem at the moment but might be later on is that your content length is based on the Unicode length of the message string, not the binary length.

There's also rather a lot going on in the Connection constructor - stuff that really doesn't belong in a constructor at all, IMO. It also doesn't close things if exceptions occur.

Have you looked at what's going on in a network tracer like WireShark? That would be the easiest way of seeing whether any data is getting sent back to the client.

Alternatively, post a short but complete version of your code which we can compile and run ourselves.

Error: Unable to Start Debugging on the Web Server, The remote server returned an error; Could not start ASP.NET debugging; The debugger cannot connect to the remote computer; See help for  You disable connection pooling for a database connection, then each open of the database will be treated as an individual connection instead of the connection being shared pooled, which would result in the database server being choked=out on connections to the database.

Maybe I'm just missing something but the reason you can only connect on localhost is because the IP you are listening on is, this makes the TCPListener only listen on that IP address. And I don't see anywhere you are calling any client disconnect, the TCPListener is a blocking call, so it sits there forever until a connection is made. In my past experience with TCP/IP and the TCPListener, TCPClient classes there is not much of a way to force the listener to drop it's connection until you drop the client connection. Calling TCPListener.Abort() doesn't drop the client connection which keeps the port blocked up.

Troubleshooting common permissions and security-related issues in , Do other ASP.NET applications on the server fail with the same error? in IIS and see if you are able to connect to the remote server that is  In the example, I've added Connection Timeout=60 to allow the SQL connection 60 seconds to connect to SQL Server (the default value is 15 seconds). Notice that this setting doesn't control command timeout, which is the time allowed for querying one or more tables.

For anyone in similar situation where you want to access you C# server from the local network ip address then you will need to listen to address meaning listen on all ip addresses not a specific one like etc.

Error - The web server is not configured correctly, Error: The web server is not configured correctly Mismatched versions of ASP.​NET on IIS and in your Visual Studio project may cause this issue. have too many connections, you may need to increase the connection limit. Though it can be developed in any .net supported language, I chose C# for this example. The code is compiled using beta2. Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]. It can be used with Beta1 with some minor modification. This application can co-exists with IIS or any web server, the key is to choose any free port.

Troubleshoot Server & Database Connection Problems, When connecting to SQL Server, this failure may be caused by the fact Advanced Services on a server named DEVSRV01, the web portal  The first step is to create variables, which will be used to create the connection string and the connection to the SQL Server database. The next step is to create the connection string. The connecting string needs to be specified correctly for C# to understand the connection string. The connection string consists of the following parts

Unable to connect to web server 'IIS Express', This problem keeps returning. I seem to be able to get past it, then it starts again. I set my .NET Core 2.1 WebApi project as the startup project. In the text box that appears, enter regedt32. In the Registry Editor, select File, then select Connect Network Registry. In the Select Computer dialog box, enter the name of the remote computer, select Check Names, and then select OK. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server.

Why Your Web Application Can't Connect to SQL Server, One of the trickiest problems I encountered when I was just starting ASP.NET web development was debugging issues with my web application  Launch PortqryUI tool on your client machine (the machine where you are having connection issues, for web apps, this could be the IIS server.) Specify server name of the SQL Server instance or the SQL vitutal server name in the Destination IP or FQDN to query. Select Query predefined service and select SQL Service in the dropdown list.

  • Well if you're going to take all the fun out of it... ;)
  • I'm still kind of curious what was wrong with my original code by HttpListener works great. I think rather than waste any more time, I'm just going to go with that for now. Thanks!