Вот код компонента:
- Код: Выделить всё
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class TCPServer
Dim server As TcpListener
Dim bytes(1024) As Byte
Dim client As TcpClient
Dim localAddr As IPEndPoint
Dim connecting As Boolean = False
Public Event DataArrival(ByVal data As String, ByVal From_IP As IPEndPoint, ByVal DataLength As Long) 'Событие получения данных
Public Event Remote_Connect(ByVal RemoteHost As IPEndPoint) 'Событие соединения удаленного компьютера
Public Event Remote_Disconnect(ByVal RemoteHost As IPEndPoint) 'Событие отсоединения удаленного компа
Public Event ListenStart()
Public Event ListenStop()
Public Function StartListen(ByVal port As Integer) As Boolean ' Возвращаем True если успешно стартанули сервер
Try
localAddr = New IPEndPoint(IPAddress.Any, port)
server = New TcpListener(localAddr)
'Запуск сервера!
server.Start()
Worker.RunWorkerAsync()
StartListen = True
Catch
StartListen = False
End Try
End Function
Public Sub StopListen() 'Останавливаем сервер
If connecting = True Then 'Если созданно
client.Close()
connecting = False
End If
server.Stop()
Worker.CancelAsync()
End Sub
Private Sub Worker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
Try
While True
'Вызов события ListenStart
ListenerStart()
[b]client = server.AcceptTcpClient' КАК ОСТАНОВИТЬ ЭТО ПОСЛЕ ПОДКЛЮЧЕНИЯ КЛИЕНТА?Я идиот! Убейте меня, кто-нибудь![/b]
'Вызов события соединения с клиентом
RemoteHostConnect(client.Client.RemoteEndPoint)
'Создаем поток для обмена данными и читаем данные в массив байт
Dim stream As NetworkStream
Dim i As Int32
stream = client.GetStream()
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
ReciveDataSub(Encoding.Default.GetString(bytes, 0, i), client.Client.RemoteEndPoint, i)
i = stream.Read(bytes, 0, bytes.Length)
End While
'Тут событие Отключение
RemoteHostDisconnect(client.Client.RemoteEndPoint)
client.Close()
End While
Catch
ListenerStoped()
Finally
server.Stop()
End Try
End Sub
Delegate Sub ReceveData(ByVal data As String, ByVal FromIp As IPEndPoint, ByVal DataLength As Long) 'Передаем данные из потока в поток формы
Delegate Sub ConnectRemoteHost(ByVal RemoteHost As IPEndPoint) 'Передаем данные из потока в поток формы
Delegate Sub Disconnect(ByVal RemoteHost As IPEndPoint)
Delegate Sub Listen()
Delegate Sub StopListener()
Private Sub ReciveDataSub(ByVal data As String, ByVal From_Ip As IPEndPoint, ByVal DataLength As Long)
If Me.InvokeRequired Then
Dim Invoker As New ReceveData(AddressOf ReciveDataSub)
Me.Invoke(Invoker, New Object() {data, From_Ip, DataLength})
Else
RaiseEvent DataArrival(data, From_Ip, DataLength)
End If
End Sub
Private Sub RemoteHostDisconnect(ByVal RemoteHost As IPEndPoint)
If Me.InvokeRequired Then
Dim Invoker As New Disconnect(AddressOf RemoteHostDisconnect)
Me.Invoke(Invoker, New Object() {RemoteHost})
Else
RaiseEvent Remote_Disconnect(RemoteHost)
End If
End Sub
Private Sub RemoteHostConnect(ByVal RemoteHost As IPEndPoint)
If Me.InvokeRequired Then
Dim Invoker As New ConnectRemoteHost(AddressOf RemoteHostConnect)
Me.Invoke(Invoker, New Object() {RemoteHost})
Else
RaiseEvent Remote_Connect(RemoteHost)
connecting = True
End If
End Sub
Private Sub ListenerStoped()
If Me.InvokeRequired Then
Dim Invoker As New StopListener(AddressOf ListenerStoped)
Me.Invoke(Invoker, New Object() {})
Else
RaiseEvent ListenStop()
End If
End Sub
Private Sub ListenerStart()
If Me.InvokeRequired Then
Dim Invoker As New Listen(AddressOf ListenerStart)
Me.Invoke(Invoker, New Object() {})
Else
RaiseEvent ListenStart()
End If
End Sub
End Class