wmclient.vb

来自「本源码是用VB开发的GPS追踪定位系统。」· VB 代码 · 共 50 行

VB
50
字号
Imports System.Net.Sockets

Public Class WMClient
    Public Event LatLng(ByVal ID As Integer, ByVal lat As Double, ByVal lng As Double)

    Private _client As TcpClient
    '---used for sending/receiving data---
    Private data() As Byte

    Public Sub New(ByVal client As TcpClient)
        _client = client
        '---start reading data from the client in a separate thread---
        ReDim data(_client.ReceiveBufferSize)
        _client.GetStream.BeginRead(data, 0, _
           CInt(_client.ReceiveBufferSize), _
           AddressOf ReceiveMessage, Nothing)
    End Sub

    Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
        '---read from client---
        Dim bytesRead As Integer
        Try
            SyncLock _client.GetStream
                bytesRead = _client.GetStream.EndRead(ar)
            End SyncLock
            '---client has disconnected---
            If bytesRead < 1 Then
                Exit Sub
            Else
                '---get the message sent---
                Dim messageReceived As String = _
                    System.Text.Encoding.ASCII. _
                    GetString(data, 0, bytesRead)
                Dim field() As String = messageReceived.Split(":")
                '---raise an event to pass back the lat and lng---
                RaiseEvent LatLng(field(0), Convert.ToDouble(field(1)), Convert.ToDouble(field(2)))
            End If
            '---continue reading from client---
            SyncLock _client.GetStream
                _client.GetStream.BeginRead(data, 0, _
                   CInt(_client.ReceiveBufferSize), _
                   AddressOf ReceiveMessage, Nothing)
            End SyncLock
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try
    End Sub
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?