locked
C# Window application hangs/stops responding after i a function is called...

    Question

  • Hi!!

     

    i wanted some help with a code... the window hags when i call the func conn_man.. but the same program when turned into a console based program works fine... whats wrong with the windows form app..

     

    can anyone help..

    do i need to give the code snippet here???

    Tuesday, October 12, 2010 1:32 AM

Answers

  • AcceptSocket blocks until a connection.  This does not seem wise to do on the UI thread, which is what you are doing.  I think in a forms app you should consider usng BeginAcceptSocket instead.  See this link: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.beginacceptsocket.aspx.
     

    --
    Mike
    Tuesday, October 12, 2010 2:37 AM
  • Hi

    Just start the listener process in different thread and use delegates to communicate with that thread.


    ------------------------------------------------------------ The following is signature, not part of post Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Thursday, October 14, 2010 8:39 AM

All replies

  • It would be nearly impossible to identify why it would work in one case and fail in another without the conn_man function, and how you are calling it from your two applications.  They should work identically though, if called with the same parameters.

    --
    Mike
    Tuesday, October 12, 2010 1:38 AM
  • public void conn_man()
        {
          try
          {
            //activity_win.AppendText("In conn_man...");
            IPAddress ipAd = IPAddress.Parse("192.168.1.2");
            // use local m/c IP address, and 
            // use the same in the client
            /* Initializes the Listener */
            TcpListener listener = new TcpListener(ipAd, 5000);
            /* Start Listeneting at the specified port */
            listener.Start();
            activity_win.AppendText("\r\nThe server is running at port 5000...");
            activity_win.AppendText("\r\nThe local End point is :" + listener.LocalEndpoint);
            activity_win.AppendText("\r\nWaiting for a connection.....");
            Socket s = listener.AcceptSocket();
            activity_win.AppendText("\r\nConnection accepted from " + s.RemoteEndPoint);
            byte[] b = new byte[100];
            int k = s.Receive(b);
            activity_win.AppendText("Recieved...");
            activity_win.AppendText(ASCIIEncoding.ASCII.GetString(b));
            if (route(5000) == 0)
            {
              activity_win.AppendText("No port free");
              ASCIIEncoding asen = new ASCIIEncoding();
              s.Send(asen.GetBytes("Busy"));
            }
            else
            {
              port = route(5000);
              new master_slave(port);
              ASCIIEncoding asen = new ASCIIEncoding();
              s.Send(asen.GetBytes("Port " + port.ToString()));
            }
            /* clean up */
            s.Close();
          }
          catch (Exception e)
          {
            activity_win.AppendText("Error..... " + e.StackTrace);
          }  
        }
    
        public static int route(int port)
        {
          try
          {
            TcpClient scnr = new TcpClient("localhost", port);
            port += 10;
            if(port<=5100)
              route(port);
            return 0;
            
          }
          catch (Exception e)
          {
            activity_win.AppendText(e.ToString());<br/>        return port;
          }
        }
    
    When i call this func the windows app hangs... while the same func in console app works fine.. i've used Console.WriteLine in place of activity_win.AppendText
    Tuesday, October 12, 2010 1:42 AM

  • Well, not knowing what activity_win is, I would comment out those lines, or instead, write the messages to a file.  That would be a closer equivalent to the console application where you wrote to standard output.

    --
    Mike
    Tuesday, October 12, 2010 1:49 AM
  • activity_win is a text box multiline.. i think it hangs at

    Socket s=listener.AcceptSocket();

    COz the msgs after that aint displayed???

    if its the probelm then how shud i correct it.. coz i need to accept the incoming connection...

    Tuesday, October 12, 2010 1:55 AM
  • AcceptSocket blocks until a connection.  This does not seem wise to do on the UI thread, which is what you are doing.  I think in a forms app you should consider usng BeginAcceptSocket instead.  See this link: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.beginacceptsocket.aspx.
     

    --
    Mike
    Tuesday, October 12, 2010 2:37 AM
  • Hi

    Just start the listener process in different thread and use delegates to communicate with that thread.


    ------------------------------------------------------------ The following is signature, not part of post Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Thursday, October 14, 2010 8:39 AM