Server Socket
Hej Jeg er ved at lave en minder server som skal modtage nåde data og sende det vider til bestem klient ud fra deres sessionid men det vil ikke helt som jeg vil<u>min kode</u>
Public Class Host
Public Class ConnectedClient
'Implements IDisposable
Private _sckServerClient As Net.Sockets.TcpClient
Private _server As Server
'Client Event
Public Event disconnected(ByVal client As ConnectedClient)
'ClientVariabler
Private menSessionID As Double
Sub New(sckServerClient As Net.Sockets.TcpClient, server As Server)
' TODO: Complete member initialization
_sckServerClient = sckServerClient
_server = server
End Sub
Public Property SessionID As Double
Get
Return menSessionID
End Get
Set(value As Double)
menSessionID = value
End Set
End Property
Public Sub Close()
_sckServerClient.Close()
_sckServerClient = Nothing
End Sub
End Class
Public Class Server
Private sckServer As System.Net.Sockets.TcpListener
Private sckServerThread As System.Threading.Thread
Private sckClients As New List(Of ConnectedClient)
'Public Sub New(ByVal Port As Long)
Public Sub New()
End Sub
Public Function ServerIsOnline() As Boolean
Try
Return sckServer.Server.IsBound
Catch ex As Exception
Return False
End Try
End Function
Private Sub sckServerListen()
Dim sckServerClient As System.Net.Sockets.TcpClient
Do
sckServerClient = sckServer.AcceptTcpClient
Dim connClient As New ConnectedClient(sckServerClient, Me)
'AddHandler connClient.dataReceived, AddressOf Me.dataReceived
AddHandler connClient.disconnected, AddressOf Me.onClientDisconnect
sckClients.Add(connClient)
Loop
End Sub
Private Sub onClientDisconnect(ByVal client As ConnectedClient)
If sckClients.Contains(client) Then
sckClients.Remove(client)
client.Close()
client = Nothing
End If
End Sub
Public ReadOnly Property ClientCount As Long
Get
Return sckClients.Count
End Get
End Property
Public Sub Start(ByVal Port As Long)
sckServer = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, Port)
If ServerIsOnline() = False Then
'sckServer.Server.Disconnect(False)
sckClients.Clear()
sckServer.Start()
sckServerThread = New System.Threading.Thread(AddressOf sckServerListen) 'This thread will run the doListen method
sckServerThread.IsBackground = True 'Since we dont want this thread to keep on running after the application closes, we set isBackground to true.
sckServerThread.Start()
Else
sckServer.Stop()
sckServerThread.Abort()
sckClients.Clear()
End If
End Sub
Sub disconnected(ByVal client As ConnectedClient)
sckClients.Remove(client)
End Sub
Public Sub Close()
sckServer.Stop()
sckServerThread.Abort()
sckClients.Clear()
End Sub
End Class
Public Class Client
Private sckClient As System.Net.Sockets.TcpClient
Public Function Connect(ByVal ServerIP As String, ByVal ServerPort As Long) As Boolean
Try
sckClient = New System.Net.Sockets.TcpClient(ServerIP, ServerPort)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Sub Close()
sckClient.Close()
End Sub
End Class
End Class
Jeg er komme så langt at jeg kan connect til server men kan ikke få den til at afslutte forbindes igen, samt er jeg ikke helt sikker på min kode da den er tage lidt hist og pist.
som sagt er iden at man oprette kontakt til server og få et id dette id skal være 100% unik for denne client da client A får 1 og client B får 2 og client C 3 og skulle C så blive disconnect og connecter igen få den så igen 3
når alle 3 er aktiv skal man kun kun kalde 1 og få kontakt til A og modtage data derfra