This repository has been archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpClient.vb
48 lines (38 loc) · 1.51 KB
/
TcpClient.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Imports System.Linq
Imports System.Net.Sockets
Public Class TcpClient
Const Host As String = "localhost"
Const Port As Integer = 8000
ReadOnly _readCmd As Byte() = New Byte() {&HFE, &H2, &H0, &H0, &H0, &H8, &H6D, &HC3}
Const RunCode As Byte = &H1
ReadOnly _reportCmd As Byte() = New Byte() {&HFE, &H5, &H0, &H0, &HFF, &H0, &H98, &H35}
ReadOnly _tcpClient As New Net.Sockets.TcpClient()
Dim _networkStream As NetworkStream
Public Function Connect() As Boolean
' Try to connect to the server
' Return true if connected successfully, false if not
Try
_tcpClient.Connect(Host, Port)
Catch ex As SocketException
Return False
End Try
_networkStream = _tcpClient.GetStream()
Return True
End Function
Public Function Check() As Boolean
' Check if the button is pressed
Return Command(_readCmd)(3) = RunCode
End Function
Public Function Report() As Boolean
Return Not _tcpClient.Connected OrElse Command(_reportCmd).Take(_reportCmd.Length).SequenceEqual(_reportCmd)
End Function
Private Function Command(cmd As Byte()) As Byte()
' Send a command to the server and return the response.
' send the command
_networkStream.Write(cmd, 0, cmd.Length)
' read the response
Dim response(_tcpClient.ReceiveBufferSize) As Byte
_networkStream.Read(response, 0, CInt(_tcpClient.ReceiveBufferSize))
Return response
End Function
End Class