Webserver only sending text occasionally? [modified]
-
I have the following code (I am learning sockets for the first time):
static void Main() { var listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 80); listener.Start(); bool serverIsOnline = true; while (serverIsOnline) { Console.WriteLine("Polling for a client"); TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Client {0} has connected", client.Client.RemoteEndPoint); Console.WriteLine("Handling client"); serverIsOnline = HandleClient(client); Console.WriteLine("Finished handling client"); Console.WriteLine("Closing client"); client.Close(); } listener.Stop(); } private static bool HandleClient(TcpClient client) { using(var writer = new StreamWriter(client.GetStream())) { writer.WriteLine("Welcome to the system..."); writer.Flush(); } return true; }
When I open the browser (firefox and IE) and enter 127.0.0.1 it sometimes displays the welcome message, but it mostly just displays "The connection was reset". What could be causing this and how would I resolve this issue? Thanks :-D
modified on Friday, April 8, 2011 10:50 PM
-
I have the following code (I am learning sockets for the first time):
static void Main() { var listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 80); listener.Start(); bool serverIsOnline = true; while (serverIsOnline) { Console.WriteLine("Polling for a client"); TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Client {0} has connected", client.Client.RemoteEndPoint); Console.WriteLine("Handling client"); serverIsOnline = HandleClient(client); Console.WriteLine("Finished handling client"); Console.WriteLine("Closing client"); client.Close(); } listener.Stop(); } private static bool HandleClient(TcpClient client) { using(var writer = new StreamWriter(client.GetStream())) { writer.WriteLine("Welcome to the system..."); writer.Flush(); } return true; }
When I open the browser (firefox and IE) and enter 127.0.0.1 it sometimes displays the welcome message, but it mostly just displays "The connection was reset". What could be causing this and how would I resolve this issue? Thanks :-D
modified on Friday, April 8, 2011 10:50 PM
The reason is that you're closing the connection before all the bytes have been transmitted. It takes time to transmit the bytes, but you go straight from calling HandleClient to calling client.Close(). Don't explicitly close client connections until you terminate the server program.
The difficult we do right away... ...the impossible takes slightly longer.
-
The reason is that you're closing the connection before all the bytes have been transmitted. It takes time to transmit the bytes, but you go straight from calling HandleClient to calling client.Close(). Don't explicitly close client connections until you terminate the server program.
The difficult we do right away... ...the impossible takes slightly longer.
Richard Andrew x64 wrote:
calling HandleClient to calling client.Close().
You are completly right, thank you :-D
-
I have the following code (I am learning sockets for the first time):
static void Main() { var listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 80); listener.Start(); bool serverIsOnline = true; while (serverIsOnline) { Console.WriteLine("Polling for a client"); TcpClient client = listener.AcceptTcpClient(); Console.WriteLine("Client {0} has connected", client.Client.RemoteEndPoint); Console.WriteLine("Handling client"); serverIsOnline = HandleClient(client); Console.WriteLine("Finished handling client"); Console.WriteLine("Closing client"); client.Close(); } listener.Stop(); } private static bool HandleClient(TcpClient client) { using(var writer = new StreamWriter(client.GetStream())) { writer.WriteLine("Welcome to the system..."); writer.Flush(); } return true; }
When I open the browser (firefox and IE) and enter 127.0.0.1 it sometimes displays the welcome message, but it mostly just displays "The connection was reset". What could be causing this and how would I resolve this issue? Thanks :-D
modified on Friday, April 8, 2011 10:50 PM
Your server isn't a HTTP server, and what you're sending back isn't a valid HTTP header. 'The connection was reset' means that it was closed before a valid header was received (roughly speaking). You need a lower level test client than a browser if you are writing a custom server. Alternatively, get yourself some HTTP server code (for example, mine[^] ;) ).