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:
client 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
I start the server withCode: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
and i connect the client withCode:Public Server As New clsServerSocket Server.start
it says in my patcher window(where it should add the text) that it's succesfully connected.Code:Dim cl As New ClientSocket cl.Start()
but at the server side its not saying there is a new client connected/disconnected, how can I do this?


Reply With Quote

