title: MultiThreading and Sockets/TCP [VB.NET]
body: <p>I started learning about TCP/Sockets yesterday and decided to make a chatbox for a friend and I.</p>
  
  <p>Unfortunately, i am having some difficulties with MultiThreading. </p>
  
  <p>Whenever i am using it, i can no longer receive messages from my friend.</p>
  
  <p>But, if i disable it then, everything works perfectly.</p>
  
  <p>I don't know what's going on here, could somebody help?</p>
  
  <pre><code>Imports System.Net.Sockets
  Imports System.Net
  
  Public Class ServerClient
  
      Dim _TCPServer As Socket
      Dim _TCPListener As TcpListener
  
      Dim _ListenerThread As System.Threading.Thread
  
      Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  
          'When Submit is pressed, send some text to the client
  
          Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(txtInput.Text)
          txtBox.AppendText(vbCrLf &amp; "Server: " &amp; txtInput.Text)
          txtInput.Clear()
          _TCPServer.Send(bytes)
  
      End Sub
  
      Private Sub TCPListen()
  
          'If somebody calls port 2424, accept it, unblock the socket and start the timer
  
          _TCPListener = New TcpListener(IPAddress.Any, 2424)
          _TCPListener.Start()
          _TCPServer = _TCPListener.AcceptSocket()
          btnSend.Enabled = True
          txtBox.AppendText("Connection Established" &amp; vbCrLf)
          _TCPServer.Blocking = False
          _Timer.Enabled = True
  
      End Sub
  
      Private Sub _Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer.Tick
  
          'If data has been sent, receive it
  
          Try
  
              Dim rcvdbytes(_TCPServer.ReceiveBufferSize) As Byte
              _TCPServer.Receive(rcvdbytes)
              txtBox.AppendText(vbCrLf &amp; "Client: " &amp; System.Text.Encoding.ASCII.GetString(rcvdbytes) &amp; vbCrLf)
  
          Catch ex As Exception
          End Try
  
      End Sub
  
      Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
          'Link the new thread to TCPListen(), allow access to all threads and wait for a call
  
          _ListenerThread = New Threading.Thread(AddressOf TCPListen)
          Control.CheckForIllegalCrossThreadCalls = False
  
          txtBox.AppendText("Waiting for connection.." &amp; vbCrLf)
          btnSend.Enabled = False
          _ListenerThread.Start()
  
      End Sub
  
  End Class
  </code></pre>