#Region "Server\Listen"
''' <summary>
''' Wartet auf eingehende Verbindungsanforderungen
''' </summary>
Public Sub Listen()
Dim x As New System.Threading.Thread(AddressOf DoListen)
x.Start()
End Sub
Private Sub DoListen()
If Status = WinsockStates.Closed Then
If _Protokoll = WinsockProtocol.TCP Then
Try
_SocketListTCP = New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
Dim ipLocal As New IPEndPoint(localAddr, LokalPort)
_SocketListTCP.Bind(ipLocal)
_SocketListTCP.Listen(1)
ChangeState(WinsockStates.Listening)
_SocketListTCP.BeginAccept(New AsyncCallback(AddressOf _
OnClientConnect), Nothing)
Catch ex As Exception
Me.Close()
ChangeState(WinsockStates.Error)
CrossThread.RunGui(AddressOf DoHandleError, ex.HResult, _
ex.Message)
End Try
ElseIf _Protokoll = WinsockProtocol.UDP Then
Try
If Socket.OSSupportsIPv4 Then
_SocketListUDP = New Socket(AddressFamily.InterNetwork, _
SocketType.Dgram, ProtocolType.Udp)
Dim localAddr As IPAddress = IPAddress.Parse( _
"127.0.0.1")
Dim ipLocal As New IPEndPoint(localAddr, LokalPort)
Dim xe As New UdpReceiveState()
xe.SendingSocket = _SocketListUDP
xe.ReceivingEndpoint = ipLocal
_SocketListUDP.Bind(ipLocal)
_SocketListUDP.BeginReceiveFrom(_byteBuffer, 0, _
_BytesTotal, SocketFlags.None, xe.ReceivingEndpoint, _
New AsyncCallback(AddressOf EmpfangServerClientUDP), _
xe)
ChangeState(WinsockStates.Listening)
ElseIf Socket.OSSupportsIPv6 Then
_SocketListUDP = New Socket( _
AddressFamily.InterNetworkV6, SocketType.Dgram, _
ProtocolType.Udp)
Dim localAddr As IPAddress = IPAddress.Parse( _
"127.0.0.1")
Dim ipLocal As New IPEndPoint(localAddr, LokalPort)
Dim xe As New UdpReceiveState()
xe.SendingSocket = _SocketListUDP
xe.ReceivingEndpoint = ipLocal
_SocketListUDP.Bind(ipLocal)
ChangeState(WinsockStates.Listening)
_SocketListUDP.BeginReceiveFrom(_byteBuffer, 0, _
_BytesTotal, SocketFlags.None, xe.ReceivingEndpoint, _
New AsyncCallback(AddressOf EmpfangServerClientUDP), _
xe)
ChangeState(WinsockStates.Listening)
End If
Catch ex As Exception
Me.Close()
ChangeState(WinsockStates.Error)
CrossThread.RunGui(AddressOf DoHandleError, ex.HResult, _
ex.Message)
End Try
End If
End If
End Sub
''' <summary>
''' Akzeptiert eine eingehende Verbindungsanforderung
''' </summary>
''' <param name="requestID">gibt die Identität über die Akzeptierte
' Verbindung ab oder legt es fest.</param>
Public Sub Accept(ByVal requestID As Socket)
If _Protokoll = WinsockProtocol.TCP Then
Try
ChangeState(WinsockStates.ConnectionPending)
_ClientServerTCP = requestID
ChangeState(WinsockStates.Connected)
CrossThread.RunGui(AddressOf DoConnected)
_ClientServerTCP.BeginReceive(_byteBuffer, 0, _Paketgröße, _
SocketFlags.None, AddressOf EmpfangServerClientTCP, Nothing)
Catch ex As Exception
ChangeState(WinsockStates.Error)
CrossThread.RunGui(AddressOf DoHandleError, ex.HResult, _
ex.Message)
End Try
ElseIf _Protokoll = WinsockProtocol.UDP Then
Beep()
End If
End Sub
#End Region
Beitrag wurde zuletzt am 15.06.17 um 13:09:59 editiert. |