在启动TCPListener之后冻结主线程

时间:2019-03-31 12:46:24

标签: vb.net tcp

当前,我正在创建一个使用TCP的简单聊天应用程序,我的问题是,当我单击启动TCP的按钮时,窗口冻结并且无法执行任何操作(即使我连接了TCP客户端,它也保持冻结)我试图将TCP服务器放入另一个线程,但是在某种情况下它无法正常工作,甚至无法与TCP客户端连接

这是我的代码

Sub TCPServerStart()
    TCPListenerz = New TcpListener(IPAddress.Any, 1000)
    TCPListenerz.Start()
    TCPServer = TCPListenerz.AcceptSocket
    Timer1.Start()
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
    Dim adddress As IPAddress = CType(TCPServer.RemoteEndPoint, IPEndPoint).Address
    ConnectedTo.Text = adddress.ToString
End Sub
Private Sub TCP_connction_type_Click(sender As Object, e As EventArgs) Handles TCP_connction_type.Click
    TCPServerStart
End Sub
Sub TCPServcerSub()
    Dim sendbytes() As Byte = System.Text.Encoding.ASCII.GetBytes(TCPTextbox.Text)
    TCPServer.Send(sendbytes)
    ''   TCPServer.SendFile("")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim rcvbytes(TCPServer.ReceiveBufferSize) As Byte
    TCPServer.Receive(rcvbytes)
    TextBox4.Text = System.Text.Encoding.ASCII.GetString(rcvbytes)
End Sub ''TCP Recive

感谢任何帮助

0 个答案:

没有答案