Hallo Leute
Ich probiere einen zuverlässigen stabilen Multi Client Server zu bauen allerdings harkt es an einer stelle. Durch das Socket ordentlich die listview id abrufen und so ein Client entfernen. Der Source code den ich poste funktioniert allerdings bin ich mit einer stelle ganz und gar nicht zufrieden diese habe ich markiert.
Wäre toll wenn ihr mir helfen könntet
Source code :
PS : Wenn ich den Beitrag mit Chrome abschicke macht er keine absätze
Ich probiere einen zuverlässigen stabilen Multi Client Server zu bauen allerdings harkt es an einer stelle. Durch das Socket ordentlich die listview id abrufen und so ein Client entfernen. Der Source code den ich poste funktioniert allerdings bin ich mit einer stelle ganz und gar nicht zufrieden diese habe ich markiert.
Wäre toll wenn ihr mir helfen könntet
Source code :
VB.NET-Quellcode
- Dim serverSocket As Socket
- Dim clientSocket As Socket
- Dim byteData(1023) As Byte
- ''
- '' Custom Functions
- ''
- Private Sub refreshClient(ByVal client As Socket)
- Send("|getinfo|", client)
- End Sub
- ''
- '' Custom Functions end
- ''
- Private Sub beginAccept()
- serverSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
- Dim IpEndPoint As IPEndPoint = New IPEndPoint(IPAddress.Any, 33724)
- serverSocket.Bind(IpEndPoint)
- serverSocket.Listen(5)
- serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
- End Sub
- Private Sub OnAccept(ByVal ar As IAsyncResult)
- clientSocket = serverSocket.EndAccept(ar)
- serverSocket.BeginAccept(New AsyncCallback(AddressOf OnAccept), Nothing)
- clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _
- New AsyncCallback(AddressOf OnRecieve), clientSocket)
- AddClient(clientSocket)
- End Sub
- Delegate Sub _AddClient(ByVal client As Socket)
- Private Sub AddClient(ByVal client As Socket)
- If InvokeRequired Then
- Invoke(New _AddClient(AddressOf AddClient), client)
- Exit Sub
- End If
- '' IP Splitten
- Dim clientIP() As String = client.LocalEndPoint.ToString.Split(":")
- '' Items definieren
- Dim items(2) As String
- items(0) = clientIP(0)
- items(1) = "loading"
- items(2) = clientIP(1)
- Dim lvi As New ListViewItem(items)
- lvi.Tag = client
- lv_clients.Items.Add(lvi)
- End Sub
- Private Sub Send(ByVal msg As String, ByVal client As Socket)
- Try
- Dim sendBytes As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(msg)
- client.BeginSend(sendBytes, 0, sendBytes.Length, SocketFlags.None, New AsyncCallback(AddressOf OnSend), client)
- Catch ex As Exception
- lv_clients.SelectedItems(0).Remove()
- End Try
- End Sub
- Private Sub OnSend(ByVal ar As IAsyncResult)
- Dim client As Socket = ar.AsyncState
- client.EndSend(ar)
- End Sub
- Delegate Sub _OnRecieve(ByVal ar As IAsyncResult)
- Private Sub OnRecieve(ByVal ar As IAsyncResult)
- If InvokeRequired Then
- Invoke(New _OnRecieve(AddressOf OnRecieve), ar)
- Exit Sub
- End If
- Dim client As Socket = ar.AsyncState
- Try
- client.EndReceive(ar)
- Dim bytesRec As Byte() = byteData
- Dim message As String = System.Text.ASCIIEncoding.ASCII.GetString(bytesRec)
- Read(message)
- clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, _
- New AsyncCallback(AddressOf OnRecieve), clientSocket)
- Catch ex As Exception
- '' HIER
- ' An dieser stelle harkt es
- ' Ich kriegs nicht hin ohne eine For schleife die alle einträge prüft
- ''' HIER
- Dim activeSocket As Socket
- For i As Integer = 0 To lv_clients.Items.Count - 1
- activeSocket = lv_clients.Items(i).Tag
- If activeSocket.Handle = client.Handle Then
- lv_clients.Items(i).Remove()
- End If
- Next
- '' bis hier
- End Try
- End Sub
- Delegate Sub _Read(ByVal msg As String)
- Private Sub Read(ByVal msg As String)
- If InvokeRequired Then
- Invoke(New _Read(AddressOf Read), msg)
- Exit Sub
- End If
- Dim cmd() As String = msg.Split("|")
- Select Case cmd(1)
- Case "info"
- '' Empfange benutzerinfos
- MsgBox(cmd(2))
- End Select
- End Sub
- ''
- '' Client / Server Section ends
- ''
- End Class
PS : Wenn ich den Beitrag mit Chrome abschicke macht er keine absätze