Results 1 to 3 of 3

Thread: multi thread client & server

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    multi thread client & server

    Hello,

    I want to make my client & server fully working. If a client connects to the server it should tell that there is a new client connected. if a client disconnects it should tell me a client disconnected.

    server code:

    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    
    Public Class clsServerSocket
    
        Public TextSent As String = ""                                              'De verzonden tekst
        Public ConnectedClients As List(Of TcpClient)                               'De list met clients
        Public Listener As TcpListener                                              'Ontvangt clients
        Protected IsCreatedWithSubNew As Boolean = False                            'Om te controleren of New() is aangeroepen
        Private t As New Thread(AddressOf _start)                                   'De thread om clients te ontvangen, _Start is de sub om uit te voeren in een andere thread
        Private _started As Boolean                                                 'opslaan of de thread gestart is
    
        Public ReadOnly Property started() As Boolean 'property om te zorgen dat Started read-only is
            Get
                Return _started
            End Get
        End Property 'started()
    
        Public Sub New()
    
            'Listener = New TcpListener(IPAddress.Parse(IP), Port) '
            Listener = New TcpListener(IPAddress.Parse("0.0.0.0"), Port)
            ConnectedClients = New List(Of TcpClient)
            IsCreatedWithSubNew = True
    
        End Sub 'new()
    
        Public Sub start()
            If t.IsAlive Then Exit Sub 'om te vookomen dat de server 2 keer gestart wordt
            If Not IsCreatedWithSubNew Then
                MsgBox("Public Sub New() not used", MsgBoxStyle.Exclamation, "Error")
                Exit Sub
            End If 'not iscreatedwithsubnew
            t.Start()
            Me._started = True
    
        End Sub 'start()
    
        Public Sub _start()
            Try
                Listener.Start()
                While True
                    Dim NewClient As TcpClient = Listener.AcceptTcpClient
                    ConnectedClients.Add(NewClient)
    
                    AddText("Connection received from: ")
    
                End While 'while true
            Catch ex As ThreadAbortException
                Exit Sub
            End Try 'listener.start...end while
        End Sub
    
        Public Sub abort()
            t.Abort()
            End 'abort()
        End Sub
    
        Public Sub SendDataTo(ByVal Index As Integer, ByVal Data As String)
    
            If Not Me.started Then Exit Sub
    
            Try
    
                For Each connectedclient In Server.ConnectedClients
    
                    Dim writer As New NetworkStream(connectedclient.Client)
    
                    If writer.CanWrite Then
    
                        Dim buff(Index) As Byte
    
                        Dim buffer(connectedclient.SendBufferSize) As Byte
                        buffer = System.Text.ASCIIEncoding.ASCII.GetBytes("test")
                        writer.Write(buffer, 0, buffer.Length)
    
                    End If
    
    
                Next
    
    
            Catch ex As Exception
    
            End Try
    
        End Sub
    
    End Class
    client code:

    Code:
    Public Class ClientSocket
    
        Public ClientSocket As TcpClient
    
    
        Public Sub Start()
    
            ClientSocket = New TcpClient
    
            Try
    
                If Not Connected() Then
                    ClientSocket.Connect(Options.IP, Options.Port)
                    AddTextToPatcher("Succesfully connected to: {" & Options.IP & "/" & Options.Port & "}")
                Else
                    AddTextToPatcher("Already connected")
                End If
    
            Catch ex As Exception
    
                AddTextToPatcher("Failed to connect to: {" & Options.IP & "/" & Options.Port & "}")
                AddTextToPatcher("because of reason; " & ex.Message)
    
            End Try
    
    
        End Sub
    
        Public Function Connected() As Boolean
    
            Return ClientSocket.Connected
    
        End Function
    
    End Class
    I start the server with

    Code:
        Public Server As New clsServerSocket
    Server.start
    and i connect the client with

    Code:
            Dim cl As New ClientSocket
    
            cl.Start()
    it says in my patcher window(where it should add the text) that it's succesfully connected.

    but at the server side its not saying there is a new client connected/disconnected, how can I do this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: multi thread client & server

    You might like to follow the CodeBank link in my signature and check out my Asynchronous TCP thread. It's not exactly the same scenario as yours but you should still be able to get some ideas on the multi-threading side of things, which is also built into the Socket class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: multi thread client & server

    Quote Originally Posted by jmcilhinney View Post
    You might like to follow the CodeBank link in my signature and check out my Asynchronous TCP thread. It's not exactly the same scenario as yours but you should still be able to get some ideas on the multi-threading side of things, which is also built into the Socket class.
    thanks !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width