I am trying to use the code from Microsoft for an Async Socket connection. It appears the listener runs in the main thread locking the GUI. I am new at both socket connections and multi-threading all at the same time. Having a hard time getting my mind wrapped around this all at once.
The code used is at http://msdn.microsoft.com/en-us/library/fx6588te.aspx
Using this example, how can I move the listener to its own thread?
Public Shared Sub Main()
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](1023) {}
' Establish the local endpoint for the socket.
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(1)
Dim localEndPoint As New IPEndPoint(ipAddress, 11000)
' Create a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Bind the socket to the local endpoint and listen for incoming connections.
listener.Bind(localEndPoint)
listener.Listen(100)
You can simply invoke the Main method of the socket in an asynchronous manner. You can use either:
or:
(or use a BackgroundWorker)