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 works in a console app but not windows forms?

Webserver works in a console app but not windows forms?

Scheduled Pinned Locked Moved C#
htmldatabasewinformssysadminhelp
2 Posts 2 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 class:

    public class WebServer
    {
    private readonly TcpListener _listener;
    private readonly Thread _listenerThread;

        public WebServer()
        {
            \_listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
            \_listenerThread = new Thread(ListenForClients);
        }
    
        public void StartListening()
        {
            \_listenerThread.Start();
        }
    
        private void ListenForClients()
        {
            \_listener.Start();
    
            while (true)
            {
                TcpClient client = \_listener.AcceptTcpClient();
    
                var clientThread = new Thread(HandleClient);
                clientThread.Start(client);
            }
            \_listener.Stop();
        }
    
    
        private void HandleClient(object client)
        {
            var tcpClient = client as TcpClient;
            var clientStream = tcpClient.GetStream();
            var encoder = new ASCIIEncoding();
            var message = new byte\[4096\];
    
            while (true)
            {
                int bytesRead = 0;
                string fileToGrab;
                try
                {
                    //blocks until a client sends a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                    string msg = encoder.GetString(message);
                    Console.WriteLine("Client {0} connected", tcpClient.Client.LocalEndPoint);
                    Console.WriteLine(msg);
    
                    string\[\] parts = msg.Split(' ');
    
                    fileToGrab = parts\[1\];
    
                }
                catch
                {
                    //a socket error has occured
                    break;
                }
    
                if (bytesRead != 0)
                {
                    if (fileToGrab == "/") fileToGrab = @"/index.html";
                    else if (!File.Exists(Environment.CurrentDirectory + fileToGrab))
                    {
                        fileToGrab = @"/error.html";
                    }
    
                    byte\[\] buffer = File.ReadAllBytes(Environment.CurrentDirectory + fileToGrab);
    
    
                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
    
                    string serverIp = "From server " + \_listener.LocalEndpoint;
                    clientStream.Write(encoder.GetBytes(serverIp), 0, serverIp.Length);
                    clientSt
    
    B 1 Reply Last reply
    0
    • V venomation

      I have the following class:

      public class WebServer
      {
      private readonly TcpListener _listener;
      private readonly Thread _listenerThread;

          public WebServer()
          {
              \_listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 80);
              \_listenerThread = new Thread(ListenForClients);
          }
      
          public void StartListening()
          {
              \_listenerThread.Start();
          }
      
          private void ListenForClients()
          {
              \_listener.Start();
      
              while (true)
              {
                  TcpClient client = \_listener.AcceptTcpClient();
      
                  var clientThread = new Thread(HandleClient);
                  clientThread.Start(client);
              }
              \_listener.Stop();
          }
      
      
          private void HandleClient(object client)
          {
              var tcpClient = client as TcpClient;
              var clientStream = tcpClient.GetStream();
              var encoder = new ASCIIEncoding();
              var message = new byte\[4096\];
      
              while (true)
              {
                  int bytesRead = 0;
                  string fileToGrab;
                  try
                  {
                      //blocks until a client sends a message
                      bytesRead = clientStream.Read(message, 0, 4096);
                      string msg = encoder.GetString(message);
                      Console.WriteLine("Client {0} connected", tcpClient.Client.LocalEndPoint);
                      Console.WriteLine(msg);
      
                      string\[\] parts = msg.Split(' ');
      
                      fileToGrab = parts\[1\];
      
                  }
                  catch
                  {
                      //a socket error has occured
                      break;
                  }
      
                  if (bytesRead != 0)
                  {
                      if (fileToGrab == "/") fileToGrab = @"/index.html";
                      else if (!File.Exists(Environment.CurrentDirectory + fileToGrab))
                      {
                          fileToGrab = @"/error.html";
                      }
      
                      byte\[\] buffer = File.ReadAllBytes(Environment.CurrentDirectory + fileToGrab);
      
      
                      clientStream.Write(buffer, 0, buffer.Length);
                      clientStream.Flush();
      
                      string serverIp = "From server " + \_listener.LocalEndpoint;
                      clientStream.Write(encoder.GetBytes(serverIp), 0, serverIp.Length);
                      clientSt
      
      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      There's a simple way to test that: put [STAThread] on your console app. However, I'm fairly sure I've served TCP from within a WinForms app without problems in the past. I don't immediately see the problem. You should put some logging in the socket error catch block, as well as breaking out of the loop. Actually never mind that, you should use the asynchronous socket methods (BeginReceive and BeginAccept), instead of creating a thread for each client. (Also, your accept thread will never terminate.) Take a look at my Sockets library[^] for an example if the MSDN docs don't make it clear enough (specifically the ClientInfo class which uses asynchronous receive, and the Server class which uses asynchronous accept).

      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