none
Form freezes

    Question

  • Hi,

    I have a tcp client / server which can send messages through sockets.
    My problem is:
    To receive a message I have to push a button to activate the receive-method and then the send-buttton on the other side of the connection within a short period of time instead of just receiving it automatically. But if I just keep running the receive-method over and over again or if I use a while (true) loop, the form just freezes...

    So I want to know:
    Is there any way to keep listening to a socket without freezing the form?

    My code:
    public void tjek()
            {
                Socket connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint ip = new IPEndPoint(IPAddress.Any, 1234);
                bool connected;
                connection.Bind(ip);
    
                connection.Listen(1);
    
                try
                {
                    connection = connection.Accept();
                    connected = true;
                }
                catch (Exception e)
                {
                    message.Text = e.ToString();
                    connected = false;
                }
    
    
                if (connected)
                {
                    int bufferSize = connection.ReceiveBufferSize;
                    byte[] buffer = new byte[bufferSize];
                    connection.Receive(buffer);
                    statusBox.Text = Encoding.UTF8.GetString(buffer);
                }
    
                connection.Close();
            }
    Sunday, May 10, 2009 5:31 PM

Answers

  • Two options.

    1. Create a separate thread for listening.

    2. Use Asynchronous receive methods.



    Thanks,
    A.m.a.L

    .Net Goodies
    [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Shald Homie Tuesday, May 12, 2009 4:02 AM
    Sunday, May 10, 2009 8:01 PM
  • The problem is that you are calling connection.Receive from your UI thread. The UI thread is responsible for handling input such as button clicks and for updating the graphic contents of your Form. If you block the UI thread (e.g. using a While(true) loop) then the Form will "freeze".
    Just as A.m.a.L suggested you need a background thread that waits for incoming messages in order to keep the UI thread free. This can be achieved in different ways but to begin with a simple asynchronious call should do the trick. Have a look at the Socket.BeginReceive() or Socket.ReceiveAsync() methods.

    /Calle


    - Still confused, but on a higher level -
    • Marked as answer by Shald Homie Tuesday, May 12, 2009 4:02 AM
    Sunday, May 10, 2009 11:26 PM

All replies

  • Two options.

    1. Create a separate thread for listening.

    2. Use Asynchronous receive methods.



    Thanks,
    A.m.a.L

    .Net Goodies
    [Remember to click "mark as answered" when you get a correct reply to your question]
    • Marked as answer by Shald Homie Tuesday, May 12, 2009 4:02 AM
    Sunday, May 10, 2009 8:01 PM
  • The problem is that you are calling connection.Receive from your UI thread. The UI thread is responsible for handling input such as button clicks and for updating the graphic contents of your Form. If you block the UI thread (e.g. using a While(true) loop) then the Form will "freeze".
    Just as A.m.a.L suggested you need a background thread that waits for incoming messages in order to keep the UI thread free. This can be achieved in different ways but to begin with a simple asynchronious call should do the trick. Have a look at the Socket.BeginReceive() or Socket.ReceiveAsync() methods.

    /Calle


    - Still confused, but on a higher level -
    • Marked as answer by Shald Homie Tuesday, May 12, 2009 4:02 AM
    Sunday, May 10, 2009 11:26 PM
  • Thank you for your help :)
    Tuesday, May 12, 2009 4:03 AM