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();
}
}
}
}
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();
}
}
}
}