HTTP Webserver - tcp thread issues

  • Thread starter Thread starter Microsoft News
  • Start date Start date
M

Microsoft News

Hi,

I have been using several http server code examples from the web, include
one from msdn, and I can't seem to get a simple http server thread working.
I can connect the server successful using IE6 and following url:
http://127.0.0.1:5050
But when I attempt a second connect the windows symbol in the upper right
corner of ie starts in motion and nothing happens it just sits there waiting
for a response. The code also behaves very strangely when I try to debug
it, it will jump out of the serverThread function in the middle of the
stream.read loop and not return any exceptions, I'm really lost on this one.
The code also really seems to stumble when I use IE's refresh button rather
than typing in the URL if you can believe it.

I have included the class in question below stripped to the bone. Any
feedback would be appreciated.

-----

using System;

using System.Net;

using System.Net.Sockets;

using System.Text;

using System.Threading;

namespace httpserv

{

/// <summary>

/// Summary description for PeerNetServer.

/// </summary>

public class httpserv

{

TcpListener listener;

private int port = 5050;

private Thread serverThread;

public httpserv()

{

//

// TODO: Add constructor logic here

//

}

public void Start()

{

listener = new TcpListener(IPAddress.Parse("127.0.0.1"), port);

serverThread = new Thread(new ThreadStart(ServerThread));

listener.Start();

serverThread.Start();

}

public void Stop()

{

listener.Stop();

serverThread.Abort();

}

private void ServerThread()

{

while(true)

{

TcpClient client = listener.AcceptTcpClient();

NetworkStream stream = client.GetStream();

int bytesRead;

Byte[] buffer = new Byte[1024];

string request = "";

while((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)

{

request = request + Encoding.ASCII.GetString(buffer, 0, bytesRead);

}

string output = "HTTP/1.1 200 OK\r\n";

output = output + "Content-type: text/plain\r\n";

output = output + "Content-length: " + request.Length + "\r\n";

output = output + "\r\n";

output = output + request;

byte[] msg = System.Text.Encoding.ASCII.GetBytes(output);

stream.Write(msg, 0, msg.Length);

//stream.Flush(); // Documentation claims it doesn't do anything at this
time but thought I'd try it anyway

client.Close();

}

}

}

}
 
Dave said:
Hi,

I have been using several http server code examples from the web,
include one from msdn, and I can't seem to get a simple http server
thread working. I can connect the server successful using IE6 and
following url: http://127.0.0.1:5050
But when I attempt a second connect the windows symbol in the upper
right corner of ie starts in motion and nothing happens it just sits
there waiting for a response. The code also behaves very strangely
when I try to debug it, it will jump out of the serverThread
function in the middle of the stream.read loop and not return any
exceptions, I'm really lost on this one. The code also really seems
to stumble when I use IE's refresh button rather than typing in the
URL if you can believe it.

I have included the class in question below stripped to the bone.
Any feedback would be appreciated.
[...]

That's a singlethreaded implementation -- the main server thread should
spawn a new thread for each request accepted. Check out the Cassini web
server at www.asp.net.

Cheers,
 
Thanks Joerg,

Cassini looks like it will be very helpful, it is definitely a more complete
package than any of the examples I have come across to date, but I'd still
like to understand why my simple example doesn't work as expected before
getting into more complex examples. Even though it is single threaded
shouldn't the listener make itself available again once it has finished
processing a request? I'd also like to try to understand why it is not
consistently serving up the first request.

Thanks,
Dave

Joerg Jooss said:
Dave said:
Hi,

I have been using several http server code examples from the web,
include one from msdn, and I can't seem to get a simple http server
thread working. I can connect the server successful using IE6 and
following url: http://127.0.0.1:5050
But when I attempt a second connect the windows symbol in the upper
right corner of ie starts in motion and nothing happens it just sits
there waiting for a response. The code also behaves very strangely
when I try to debug it, it will jump out of the serverThread
function in the middle of the stream.read loop and not return any
exceptions, I'm really lost on this one. The code also really seems
to stumble when I use IE's refresh button rather than typing in the
URL if you can believe it.

I have included the class in question below stripped to the bone.
Any feedback would be appreciated.
[...]

That's a singlethreaded implementation -- the main server thread should
spawn a new thread for each request accepted. Check out the Cassini web
server at www.asp.net.

Cheers,
 
News said:
Thanks Joerg,

Cassini looks like it will be very helpful, it is definitely a more
complete package than any of the examples I have come across to
date, but I'd still like to understand why my simple example doesn't
work as expected before getting into more complex examples. Even
though it is single threaded shouldn't the listener make itself
available again once it has finished processing a request? I'd also
like to try to understand why it is not consistently serving up the
first request.

Well, it should. After looking at your code again, there are still a
couple of issues that need to be taken care of:

1. Utter lack of exception handling ;-) This is quite dangerous anyway,
but since 99.9% of all exceptions are to be expected in the main server
thread, a simple try/catch in main() won't help.

2. Review the docs for TcpClient and NetworkStream on MSDN. For example:
"You must close the NetworkStream when you are through sending and
receiving data. Closing TcpClient does not release the NetworkStream."

Unfortunately, MSDN's own samples are sometimes wrong if this is true. I
guess not adhering to those guidelines may have weird side effects.

3. If you know Java (or better its java.net package), a multithreaded
HTTP server can be found at
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=396
&printer=t

ServerSocket becomes TcpListener, and accept() becomes either
AcceptSocket() or AcceptTcpClient() etc.

Cheers,
 
Thanks for the tips. Believe it or not removing the read loop and replacing
it with a single read command helped correct the problem. I still haven't
figured out why but I'll keep playing with it until it hits me.

Once the first request was working closing the socket, as you suggested,
made a huge difference in performance. I'm surprised it was not in any of
the online examples I looked at.

As far as exception handling goes - I know it is less than optimal :). I
removed any code that was not necessary for the server to run, once I
determine that exceptions were not being thrown when my code was
misbehaving, in order to make pin pointing the problem a little easier, but
don't worry, I will be sure to add plenty of exception handling once I am
happy with the basic functionality.

Thanks Again,
Dave
 
Back
Top