Private Const DeviceCodePage As Int32 = 936
Private TcpServer As TcpListenerPlus = Nothing
Private IsServerRunning As Boolean = False
Public Sub New()
InitializeComponent()
End Sub
Private Sub buttonSetServerHost_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Using Client As FaceId = New FaceId(textBoxDeviceIP.Text, Convert.ToInt32(textBoxDevicePort.Text))
Dim Command As String = "SetServerHost(ip=""" & comboBoxServerIP.Text & """ port=""" + textBoxServerPort.Text & """)"
Dim Answer As String
If Client.Execute(Command, Answer, DeviceCodePage) = FaceId_ErrorCode.Success Then
MessageBox.Show("设置服务器参数成功!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("设置服务器参数失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End If
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub
Private Sub buttonClear_Click(ByVal sender As Object, ByVal e As EventArgs)
textBoxRecords.Clear()
End Sub
Private Sub buttonStartListener_Click(ByVal sender As Object, ByVal e As EventArgs)
If IsServerRunning Then
TcpServer.[Stop]()
TcpServer = Nothing
IsServerRunning = False
buttonStartListener.Text = "开启侦听"
Else
Try
TcpServer = New TcpListenerPlus(IPAddress.Parse(comboBoxServerIP.Text), Convert.ToInt32(textBoxServerPort.Text))
TcpServer.SecretKey = String.Empty
TcpServer.OnServerTaskRequest += AddressOf OnServerTaskProcess
TcpServer.StartListenThread(New String() {textBoxDeviceIP.Text}, 0, 10 * 60 * 1000)
Catch ex As Exception
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Return
End Try
IsServerRunning = True
buttonStartListener.Text = "停止侦听"
End If
End Sub
Private Sub OnServerTaskProcess(ByVal sender As Object, ByVal e As EventArgs)
Dim Stream As NetworkStreamPlus = CType(sender, NetworkStreamPlus)
While True
Try
Dim Answer As String
Stream.Read(Answer, DeviceCodePage)
textBoxRecords.Invoke(New Action(Sub()
textBoxRecords.AppendText(Answer & vbCrLf)
End Sub))
If Answer.StartsWith("PostRecord") Then
Stream.Write("Return(result=""success"" postphoto=""false"")", DeviceCodePage)
ElseIf Answer.StartsWith("Record") Then
Stream.Write("Return(result=""success"")", DeviceCodePage)
ElseIf Answer.StartsWith("PostEmployee") Then
Stream.Write("Return(result=""success"")", DeviceCodePage)
ElseIf Answer.StartsWith("Employee") Then
Stream.Write("Return(result=""success"")", DeviceCodePage)
ElseIf Answer.StartsWith("Quit") Then
Exit While
End If
Catch __unusedSocketException1__ As System.Net.Sockets.SocketException
Exit While
Catch __unusedIOException2__ As System.IO.IOException
Exit While
Catch __unusedObjectDisposedException3__ As System.ObjectDisposedException
Exit While
Catch ex As Exception
Dim ErrorMessage As String = "错误信息:" & ex.[GetType]().Name
MessageBox.Show(ErrorMessage, "错误", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End While
End Sub