#Region "Client\Connect"
''' <summary>
''' Stellt eine Verbindung zum Remote Computer her.
''' </summary>
Public Sub Connect()
If _Protokoll = WinsockProtocol.TCP Then
If Status = WinsockStates.Connected Or Status = _
WinsockStates.Listening Then
MsgBox("Verbindung muss erst geschlossen werden um eine" & _
"Verbindung herzustellen!", CType(48, MsgBoxStyle), _
Application.ProductName)
Exit Sub
End If
Try
ChangeState(WinsockStates.HostResolved)
_ClientServerTCP = New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
Dim rEP As New IPEndPoint(IPAddress.Parse(LokalHostIP), _
LokalPort)
'_ClientServerTCP.Connect(rEP)
ChangeState(WinsockStates.Connecting)
_ClientServerTCP.BeginConnect(rEP, New AsyncCallback(AddressOf _
OnConnected), Nothing)
Catch ex As Exception
ChangeState(WinsockStates.Error)
CrossThread.RunGui(AddressOf DoHandleError, ex.HResult, _
ex.Message)
End Try
ElseIf _Protokoll = WinsockProtocol.UDP Then
If Status = WinsockStates.Connected Or Status = _
WinsockStates.Listening Then
MsgBox("Verbindung muss erst geschlossen werden um eine" & _
"Verbindung herzustellen!", CType(48, MsgBoxStyle), _
Application.ProductName)
Exit Sub
End If
Try
If Socket.OSSupportsIPv4 Then
ChangeState(WinsockStates.HostResolved)
_ClientServerUDP = New Socket(AddressFamily.InterNetwork, _
SocketType.Dgram, ProtocolType.Udp)
Dim rEP As New IPEndPoint(IPAddress.Parse(LokalHostIP), _
LokalPort)
'_ClientServerTCP.Connect(rEP)
ChangeState(WinsockStates.Connecting)
_ClientServerUDP.BeginConnect(rEP, New AsyncCallback( _
AddressOf OnConnected), Nothing)
ElseIf Socket.OSSupportsIPv6 Then
ChangeState(WinsockStates.HostResolved)
_ClientServerUDP = New Socket(AddressFamily.InterNetworkV6, _
SocketType.Dgram, ProtocolType.Udp)
Dim rEP As New IPEndPoint(IPAddress.Parse(LokalHostIP), _
LokalPort)
'_ClientServerTCP.Connect(rEP)
ChangeState(WinsockStates.Connecting)
_ClientServerUDP.BeginConnect(rEP, New AsyncCallback( _
AddressOf OnConnected), Nothing)
End If
Catch ex As Exception
ChangeState(WinsockStates.Error)
CrossThread.RunGui(AddressOf DoHandleError, ex.HResult, _
ex.Message)
End Try
End If
End Sub
''' <summary>
''' Stellt eine Verbindung zum Remote Computer her.
''' </summary>
''' <param name="IP">Gibt den Anschluß zurück, der auf dem lokalen
' Computer verwendet wird, oder legt den Anschluß fest.</param>
''' <param name="Port">Gibt den Anschluß auf dem Remote Computer zurück,
' über den eine Verbindung hergestellt werden soll, oder legt den Anschluß
' fest.</param>
'''
Public Sub Connect(ByVal IP As String, ByVal Port As Integer)
If _Protokoll = WinsockProtocol.TCP Then
_LocalIP = IP
_LocalPort = Port
Connect()
ElseIf _Protokoll = WinsockProtocol.UDP Then
Beep()
End If
End Sub
Private Sub OnClientConnect(ByVal asyn As IAsyncResult)
If _Protokoll = WinsockProtocol.TCP Then
Try
Dim tmpSock As Socket
If Status = WinsockStates.Listening Then
tmpSock = _SocketListTCP.EndAccept(asyn)
CrossThread.RunGui(AddressOf DoConnectionRequest, tmpSock)
_SocketListTCP.BeginAccept(New AsyncCallback(AddressOf _
OnClientConnect), Nothing)
End If
Catch ex As Exception
Me.Close()
ChangeState(WinsockStates.Error)
CrossThread.RunGui(AddressOf DoHandleError, ex.HResult, _
ex.Message)
End Try
ElseIf _Protokoll = WinsockProtocol.UDP Then
Beep()
End If
End Sub |