multiple connections to a tcp listener RRS feed

  • Question

  • i am trying to write a tcp listener that can accpet multiple conections on a port -- basically converting some old vb6 winsock stuff over, index socket connections etc)

    i am running into some trouble understanding how to make multiple threads, etc  -- been digging around and found some interesting code

    the following code listens and wait for a connection and then creates a child thread each time something connects

    where i am running into trouble is i cant figure out is 1) in my ReadDataClient() how to get the data that is being read in the thread , 2) how to send something back to the client and 3) how do i tell when it disconnects

    Public Sub OpenConnection(ByVal Port As Integer) 'ByVal IpAdress As String,
            objListener = New TcpListener(System.Net.IPAddress.Any, Port)
            objThread = New Thread(AddressOf WaitClient)
        End Sub

        Private Sub WaitClient()
            Dim sctClient As Client

            While True
                'Wait for a client
                sctClient.Socket = objListener.AcceptSocket()
                sctClient.EndPoint = sctClient.Socket.RemoteEndPoint ' objListener.LocalEndpoint


                'sctClient.Thread = New Thread(AddressOf ReadDataClient)
                SyncLock Me
                    ClientList.Add(lgnIdClient, sctClient)

                End SyncLock


            End While
        End Sub

        Public Sub ReadDataClient()
            Dim lngIdReal As Long = lgnIdClient
            Dim sctClient As Client = CType(ClientList(lngIdReal), Client)

            TextBox1.Text = TextBox1.Text & "Accepted Connection: " & lngIdReal & vbCrLf

        End Sub
    Monday, February 6, 2006 8:42 PM

All replies

  • This sample should answer you questions.


    Monday, February 6, 2006 11:19 PM
  • that probably does exactly what i want it to but with only a setup and no code it is not very helpful
    Tuesday, February 7, 2006 1:33 PM
  • Please tpiazza55,
    you can send me this sample for study? I am new in programming with multiple connections.
    very thanks!
    Tuesday, February 7, 2006 2:43 PM
  • as soon as i get one working i will be happy to send it

    still looking myself right now

    Tuesday, February 7, 2006 3:17 PM
  • Very very thanks!!! tpiazza55

    Tuesday, February 7, 2006 3:35 PM
  • Oh hell, I posted the wrong link.  Sorry here's the right one...



    Tuesday, February 7, 2006 3:43 PM
  • working with that code and running into a few problems

    i changed the module code to run in a form and then call chatclient.vb (code below) 

    i am getting an error in the chatclient.vb

    doesnt like the client.client in

    '---get the client IP address
            _clientIP = client.Client.RemoteEndPoint.ToString

    any ideas?

    form code


    Public Class Form1

    Inherits System.Windows.Forms.Form

    Const portNo As Integer = 1000

    Dim listener As New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, portNo)


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


    While True

    Dim user As New ChatClient(listener.AcceptTcpClient)

    End While

    End Sub

    End Class



    Tuesday, February 7, 2006 8:01 PM