Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Webserver only sending text occasionally? [modified]

Webserver only sending text occasionally? [modified]

Scheduled Pinned Locked Moved C#
helpquestionlearning
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    venomation
    wrote on last edited by
    #1

    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

    Richard Andrew x64R B 2 Replies Last reply
    0
    • V venomation

      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

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      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.

      V 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        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.

        V Offline
        V Offline
        venomation
        wrote on last edited by
        #3

        Richard Andrew x64 wrote:

        calling HandleClient to calling client.Close().

        You are completly right, thank you :-D

        1 Reply Last reply
        0
        • V venomation

          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

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          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[^] ;) ).

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups