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?
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.
Re: multi thread client & server
Quote:
Originally Posted by
jmcilhinney
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 !