Results 1 to 4 of 4

Thread: form do not take any action when tcpListener is start

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    form do not take any action when tcpListener is start

    hi
    i create a very simple server-client chat program.
    in the server form , i put a textbox to recieve msg from clients and a button to start tcpListener.when i click this button tcpListener is start but the form is freeze.i cant choose any thing in this form.what can i do to fix this problem?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: form do not take any action when tcpListener is start

    You are presumably calling a synchronous method, i.e. one that blocks until a connection request is received. If a method is blocking the UI thread then nothing else can be done on that thread, so the UI freezes. You need to either call your synchronous method(s) on a secondary thread or else call an asynchronous method(s). Follow the CodeBank link in my signature and check out my thread on Asynchronous TCP for an example of the second option.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    Re: form do not take any action when tcpListener is start

    thanks
    as you said , i did ... or maby i think i did
    see this code : this is my server codes
    Code:
    Private Sub Chat()
            While OutputForm.CheckBox1.Checked = True
                Try
                    Dim ip As IPAddress = GetMyIP()
                    Dim SysPort As Int32 = Convert.ToInt32(txtPort.Text)
                    Dim TCPlistener As TcpListener = New TcpListener(ip, SysPort)
                    TCPlistener.Start()
                    ListBox1.Items.Clear()
                    ListBox1.Items.Add("The server is ruuning as port :" & Str(SysPort))
                    Dim MyEndpoint As IPEndPoint = CType(TCPlistener.LocalEndpoint, IPEndPoint)
                    ListBox1.Items.Add("The local EndPoint address is :" & MyEndpoint.Address.ToString)
                    ListBox1.Items.Add("Waiting for connectiom ...")
                    Dim sock As Socket = TCPlistener.AcceptSocket
                    MyEndpoint = CType(sock.RemoteEndPoint, IPEndPoint)
                    ListBox1.Items.Add("Connection accepted from :" & MyEndpoint.Address.ToString)
                    Dim ByteData(100) As Byte
                    Dim size As Integer = sock.Receive(ByteData)
                    ListBox1.Items.Add("Recieved")
                    Dim RecieveText As String = Nothing
                    For i = 0 To size
                        RecieveText = RecieveText & Convert.ToChar(ByteData(i))
                    Next
                    ListBox1.Items.Add(RecieveText)
                    Dim AsciiEncod As New ASCIIEncoding
                    ListBox1.Items.Add("Sending alert to client !")
                    sock.Send(AsciiEncod.GetBytes("The string was recieved by the server !"))
                    sock.Close()
                    Me.Refresh()
                    TCPlistener.Stop()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End While
        End Sub
    now ... i put it in secend thread by code below :
    Code:
     Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Dim t As New Threading.Thread(AddressOf Chat)
            t.Start()
        End Sub
    but it dont work . please correct my code if it is possible .

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: form do not take any action when tcpListener is start

    You can'tmove everything to a secondary thread as a single block and expect it to work. Only the calls that were previously blocking should be performed on the secondary thread. You need to restrucr code to separate the blocking calls, e.g. TcpListener.AcceptSocket and Socket.Receive, and then execute them on a secondary thread. That would probably not even be the same secondary thread if you want to be able to connect multiple clients. This is going to require you to think about your code a bit harder.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width