I'm tired. Help with modules?

herbally

Member
Joined
May 12, 2008
Messages
5
Programming Experience
1-3
I was under the impression that this would work in the background but when I call tcpServer.Main on the form load event, it waits for connection and doesn't load the form....can someone shed some light for me?


Here's the code for the modules:
VB.NET:
Public Module TcpServer

    Public Sub Main()
        'Create new listener on port 8892
        Dim Listener As New TcpListener(New IPAddress("192.168.0.160"), 8892)

        Console.WriteLine("About to initialize port.")
        Listener.Start()

        Console.WriteLine("Listenening for a connection...")
        Dim ClientNum As Integer
        Do
            Try
                'Wait for a connection request,
                'and return a TcpClient initialized for communication.
                Dim Client As TcpClient = Listener.AcceptTcpClient
                Console.WriteLine("Server: Connection accepted.")

                'Create a new object to handle this connection.
                ClientNum += 1
                Dim Handler As New ClientHandler(Client, "Client " & ClientNum.ToString)

                'Start this object working on another thread.
                Dim HandlerThread As New System.Threading.Thread(AddressOf Handler.Start)
                HandlerThread.IsBackground = True
                HandlerThread.Start()


            Catch ex As Exception
                Console.WriteLine(ex.ToString)
            End Try
        Loop


        Console.ReadLine()
    End Sub

End Module

VB.NET:
Module TcpClients
    Public Sub Main()
        Dim Client As New TcpClient
        Try
            Console.WriteLine("Attempting to connect to the server " & "on port 8892")
            Client.Connect(IPAddress.Parse("127.0.0.1"), 8892)
            Console.WriteLine("Connection established.")

            'Retrieve the network stream.
            Dim Stream As NetworkStream = Client.GetStream

            'Create a BinaryWriter for writing to the stream.
            Dim w As New BinaryWriter(Stream)

            'Create a BinaryReader for reading from the stream.
            Dim r As New BinaryReader(Stream)

            'Start a dialogue.
            w.Write(ClientMessages.RequestConnect)
            If r.ReadString() = ServerMessages.AcknowledgeOK Then
                Console.WriteLine("Connected.")
                Console.WriteLine("Press Enter to disconnect.")
                Console.ReadLine()
                Console.WriteLine("Disconnecting...")
                w.Write(ClientMessages.Disconnect)
            Else
                Console.WriteLine("Connection not completed.")
            End If

            'Close the connection socket.
            Client.Close()
            Console.WriteLine("Port closed.")

        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try

        Console.ReadLine()

    End Sub
End Module

Please....throw a dummie a bone here...

Thanks
 
Last edited by a moderator:
Create a thread for your Main method to run in background. You can't run a continuous loops neither do time consuming blocking calls in UI thread.
 
Back
Top