Multiple Connection TCP Listener

tonelm54
tonelm54 used Ask the Experts™
on
Good evening,
Ive spent most of the day with this, and cant figure it out.

Im trying to write a program, that will listen to port 115, start a new thread to deal with the communication, and then then start listening for another port.

The original code I found from VB.net Cookbook, does not have the lines 'Listener.Start()' and 'Listener.Stop()', but wothout these I get run-time errors, saying they need to listen.

Any ideas what Im doing wrong in this code, to pass the communication to the tread??

Thanks in advance
---------------------------------------------------------------------------------------------------------------
Module1.vb
---------------------------------------------------------------------------------------------------------------
Imports System.Net.Sockets
Imports System.IO
 
Module Module1
    Dim Listener As New TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), "115")
    Sub Main()
        Dim ClientNum As Integer
        Do
            Try
                ' Wait for a connection request,
                ' and return a TcpClient initialized for communication
                Listener.Start()
 
                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()
                Listener.Stop()
                ' (You could also add the Handler and HandlerThread to
                ' a collection to track client sessions.)
            Catch Err As Exception
                Console.WriteLine(Err.ToString())
            End Try
        Loop
    End Sub
 
End Module
 
 
 
 
 
 
 
 
 
 
---------------------------------------------------------------------------------------------------------------
ClientHandler.vb
---------------------------------------------------------------------------------------------------------------
Imports System.Net.Sockets
Imports System.IO
 
Public Class ServerMessages
    Public Const AcknowledgeOK As String = "OK"
    Public Const AcknowledgeCancel As String = "Cancel"
    Public Const Disconnect As String = "Bye"
End Class
Public Class ClientMessages
    Public Const RequestConnect As String = "Hello"
    Public Const Disconnect As String = "Bye"
End Class
 
Public Class ClientHandler
    Private Client As TcpClient
    Private ID As String
    Public Sub New(ByVal client As TcpClient, ByVal ID As String)
        Me.Client = client
        Me.ID = ID
    End Sub
    Public Sub Start()
        ' 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)
        Console.WriteLine("-" & r.ReadString)
 
        If r.ReadString() = ClientMessages.RequestConnect Then
            w.Write(ServerMessages.AcknowledgeOK)
            Console.WriteLine(ID & ": Connection completed.")
            Do
                console.writeline(r.ReadString())
            Loop
            Console.WriteLine(ID & ": Disconnect request received.")
            w.Write(ServerMessages.Disconnect)
        Else
            Console.WriteLine(ID & ": Could not complete connection.")
        End If
        ' Close the connection socket.
        Client.Close()
        Console.WriteLine(ID & ": Client connection closed.")
        Console.ReadLine()
    End Sub
End Class

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014
Commented:
Here are a couple of examples showing how to do client/server TCP in VB.NET

http://www.codeproject.com/KB/IP/VbNetSendReceiveTcp.aspx

http://www.eggheadcafe.com/articles/20020323.asp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial