VB6 winsock服务器和多个arduino客户端问题

VB6 winsock服务器和多个arduino客户端问题,vb6,winsock,Vb6,Winsock,现在我正在开发一个工具,它允许我使用VB6和带以太网屏蔽的arduino UNO测量网络连接之间的延迟。 现在我在服务器代码(VB6程序)方面遇到了一些问题。 我有两个Winsock,都有不同的端口,它们都在监听arduino客户端的连接。现在,如果我只有一个活动连接,没有任何问题,一切正常,但一旦第二个客户端连接到整个服务器,就会开始疯狂。突然,它报告第一个连接的客户机失去了连接,因此简而言之,服务器不希望一次连接两个客户机,但我确实需要它:/What's your出了什么问题 我将快速解释s

现在我正在开发一个工具,它允许我使用VB6和带以太网屏蔽的arduino UNO测量网络连接之间的延迟。 现在我在服务器代码(VB6程序)方面遇到了一些问题。 我有两个Winsock,都有不同的端口,它们都在监听arduino客户端的连接。现在,如果我只有一个活动连接,没有任何问题,一切正常,但一旦第二个客户端连接到整个服务器,就会开始疯狂。突然,它报告第一个连接的客户机失去了连接,因此简而言之,服务器不希望一次连接两个客户机,但我确实需要它:/What's your出了什么问题

我将快速解释sertain命令通过winsock向服务器发送或从服务器发送的内容

“服务器睡眠”是服务器发送给所有客户端的命令,该命令将告诉客户端进入省电模式。
“服务器请求数据”是服务器发送给特定客户端的命令,并强制客户端发送设备名称和固件版本等信息。
“RESPOND_MESSAGE”是服务器发送给所有客户机的命令,客户机被迫响应以查看我们是否仍然有连接。
“DEVICE_NAME=”是客户端在刚刚连接时发送给服务器的命令,在我们将其放入列表框以显示已连接之前,它是必需的。(在=之后是设备名称)
“DEVICE_NAME_REP=”是当服务器请求有关客户端的信息时客户端发送给服务器的命令,我之所以有两个,是因为我无法重用前一个,因为这样会变得很复杂。(在=之后是设备名称)
“DEVICE_FIRMWARE=”是当服务器请求有关客户端的信息时客户端发送给服务器的命令。(在=之后是设备固件版本)
“DEVICE_OK=”是当服务器请求应答以检查我们是否仍有连接时,客户端发送给服务器的命令。(在=之后是设备名称)
“DEVICE_REBOOTING”(设备重新启动)是客户端在退出睡眠模式时(服务器关闭后再次联机时,它将退出该模式)向服务器发送的命令。客户端发送该消息后,它会立即再次关闭连接,并强制设备重新启动,以确保没有任何问题。

我的代码:

Dim DeviceIP1 As String
Dim DeviceIP2 As String
Dim UpdateListStatus As Integer

Private Sub Command1_Click()
MsgBox Socket1.State
MsgBox Socket2.State
End Sub

Private Sub Command3_Click()
If Dir(App.Path & "\TH.exe") <> "" Then 'Traceroute Helper application i wrote before, Works 100% and is not relevant for the issue i am facing
Shell App.Path & "\TH.exe " & DeviceIP, vbNormalFocus
Else
MsgBox "Missing file!" & vbNewLine & "File TH.exe is required for the requested operation!", vbCritical + vbSystemModal, "Missing file"
End If
End Sub

Private Sub Form_Load()
Socket1.Listen
Socket2.Listen
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim msg As VbMsgBoxResult
msg = MsgBox("Are you sure you want to exit?" & vbNewLine & "All the clients will be put into sleep mode.", vbYesNo + vbQuestion + vbSystemModal, "Quit")
If msg = vbYes Then
  Form3.Show
  Cancel = True
  Form1.Visible = False
Else
  Cancel = True
End If
End Sub

Private Sub List1_Click()
On Error GoTo errhandler
Dim ClientFound As Boolean
DeviceIP = Mid(List1.Text, InStr(List1.Text, "-") + 1)
DeviceIP = LTrim(DeviceIP)
DeviceIPLabel.Caption = "Device IP: " & DeviceIP
Form2.Show
    If Socket1.RemoteHostIP = DeviceIP Then
    Socket1.SendData ("SERVER_REQUESTS_DATA")
    ElseIf Socket2.RemoteHostIP = DeviceIP Then
    Socket2.SendData ("SERVER_REQUESTS_DATA")
    End If
Exit Sub
errhandler:
If Err.Number = 40006 Then
MsgBox "Socket error!" & vbNewLine & "The requested device might be offline.", vbCritical + vbSystemModal, "Socket error"
Unload Form2
End If
End Sub

Private Sub UpdateList_Timer()
On Error Resume Next
If List1.ListCount > 0 Then
If UpdateListStatus = 0 Then
TempList.Clear

Socket1.SendData ("RESPOND_MESSAGE")
Socket2.SendData ("RESPOND_MESSAGE")

UpdateListStatus = 1
UpdateList.Interval = 5000
ElseIf UpdateListStatus = 1 Then
List1.Clear
For x = 0 To TempList.ListCount
List1.AddItem (TempList.List(x))
Next x

For X2 = 0 To List1.ListCount
If List1.List(X2) = "" Then 'Check if we have any items that are nothing
List1.RemoveItem (X2)
End If
Next X2

Label1.Caption = "Connected clients: " & List1.ListCount
UpdateListStatus = 0
UpdateList.Interval = 10000
End If
End If
End Sub

Private Sub Socket1_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Dim TempString As String
Dim TempString2 As String
Dim position As Integer

Socket1.GetData TempString, vbString
    position = InStr(1, TempString, "DEVICE_NAME=")
    If position > 0 Then 'It is a device name command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       List1.AddItem (TempString2 + " - " + Socket1.RemoteHostIP)
       Label1.Caption = "Connected clients: " & List1.ListCount
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_NAME_REP=")
    If position > 0 Then 'It is a device name command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       DeviceNameLabel.Caption = "Device name: " & TempString2
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_FIRMWARE=")
    If position > 0 Then 'It is a device firmware command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       DeviceFirmwareLabel.Caption = "Firmware version: " & TempString2
       Unload Form2 'Since this is the last piece we will be receiving we can close this window
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_OK=")
    If position > 0 Then 'It is a device respond command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       TempList.AddItem (TempString2 + " - " + Socket1.RemoteHostIP)
       Label1.Caption = "Connected clients: " & List1.ListCount
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_REBOOTING")
    If position > 0 Then 'It is a device respond command from a client
       Socket1.Close
       TempString2 = ""
    End If
Text1.Text = Text1.Text & TempString & vbNewLine
TempString = ""
position = 0
TempString2 = ""
End Sub

Private Sub Socket2_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Dim TempString As String
Dim TempString2 As String
Dim position As Integer

Socket2.GetData TempString, vbString
    position = InStr(1, TempString, "DEVICE_NAME=")
    If position > 0 Then 'It is a device name command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       List1.AddItem (TempString2 + " - " + Socket2.RemoteHostIP)
       Label1.Caption = "Connected clients: " & List1.ListCount
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_NAME_REP=")
    If position > 0 Then 'It is a device name command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       DeviceNameLabel.Caption = "Device name: " & TempString2
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_FIRMWARE=")
    If position > 0 Then 'It is a device firmware command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       DeviceFirmwareLabel.Caption = "Firmware version: " & TempString2
       Unload Form2 'Since this is the last piece we will be receiving we can close this window
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_OK=")
    If position > 0 Then 'It is a device respond command from a client
       TempString2 = Mid(TempString, InStr(TempString, "=") + 1)
       TempList.AddItem (TempString2 + " - " + Socket2.RemoteHostIP)
       Label1.Caption = "Connected clients: " & List1.ListCount
       TempString2 = ""
    End If
    position = 0
    position = InStr(1, TempString, "DEVICE_REBOOTING")
    If position > 0 Then 'It is a device respond command from a client
       Socket2.Close
       TempString2 = ""
    End If
Text1.Text = Text1.Text & TempString & vbNewLine
TempString = ""
position = 0
TempString2 = ""
End Sub


Private Sub Socket1_ConnectionRequest(ByVal requestID As Long)
If Socket1.State <> sckClosed Then
    Socket1.Close
    ' Accept the request with the requestID
    ' parameter.
    Socket1.Accept requestID
End If
End Sub

Private Sub Socket2_ConnectionRequest(ByVal requestID As Long)
If Socket2.State <> sckClosed Then
    Socket2.Close
    Socket2.Accept requestID 'Allow the connection
End If
End Sub
Dim DeviceIP1作为字符串
将设备2设置为字符串
Dim UpdateListStatus为整数
专用子命令1_Click()
MsgBox Socket1.State
MsgBox Socket2.状态
端接头
专用子命令3_Click()
如果Dir(App.Path&“\TH.exe”)“Then”Traceroute Helper应用程序是我以前编写的,它100%工作,与我面临的问题无关
Shell App.Path&“\TH.exe”&DeviceIP,vbNormalFocus
其他的
MsgBox“缺少文件!”&vbNewLine&“请求的操作需要文件TH.exe!”,vbCritical+vbSystemModal,“缺少文件”
如果结束
端接头
专用子表单_加载()
短袜1.听
短袜2.听
端接头
私有子表单_queryLoad(取消为整数,卸载为整数)
将消息设置为VbMsgBoxResult
msg=MsgBox(“您确定要退出吗?”&vbNewLine&“所有客户端都将进入睡眠模式。”,vbYesNo+vbQuestion+vbSystemModal,“退出”)
如果msg=vbYes,则
表格3.展示
取消=真
表1.可见=假
其他的
取消=真
如果结束
端接头
私有子列表1_Click()
关于错误转到错误处理程序
Dim ClientFound为布尔值
DeviceIP=Mid(List1.Text,InStr(List1.Text,“-”)+1)
DeviceIP=LTrim(DeviceIP)
DeviceIPLabel.Caption=“设备IP:&DeviceIP
表格2.展示
如果Socket1.RemoteHostIP=DeviceIP,则
Socket1.SendData(“服务器请求数据”)
ElseIf Socket2.RemoteHostIP=DeviceIP然后
Socket2.SendData(“服务器请求数据”)
如果结束
出口接头
错误处理程序:
如果错误编号=40006,则
MsgBox“套接字错误!”&vbNewLine&“请求的设备可能脱机。”,vbCritical+vbSystemModal,“套接字错误”
卸载表格2
如果结束
端接头
私有子更新列表_计时器()
出错时继续下一步
如果List1.ListCount>0,则
如果UpdateListStatus=0,则
圣殿骑士,明白了
Socket1.SendData(“响应消息”)
Socket2.SendData(“响应消息”)
UpdateListStatus=1
UpdateList.Interval=5000
ElseIf UpdateListStatus=1,则
清单1.清除
对于x=0到templast.ListCount
List1.AddItem(templast.List(x))
下一个x
对于X2=0到List1.ListCount
如果List1.List(X2)=“那么”检查我们是否有任何不存在的项目
列表1.RemoveItem(X2)
如果结束
下一个X2
Label1.Caption=“已连接的客户端:”&List1.ListCount
UpdateListStatus=0
UpdateList.Interval=10000
如果结束
如果结束
端接头
专用子插座1_数据到达(ByVal ByTest总长度)
出错时继续下一步
将字符串设置为字符串
将TempString2设置为字符串
变暗位置为整数
Socket1.GetData TempString,vbString
位置=仪器(1,临时字符串,“设备名称=”)
如果位置>0,则“它是来自客户端的设备名称命令”
TempString2=Mid(TempString,InStr(TempString,“=”)+1)
列表1.AddItem(TempString2+“-”+Socket1.RemoteHostIP)
Label1.Caption=“已连接的客户端:”&List1.ListCount
TempString2=“”
如果结束
位置=0
位置=InStr(1,TempString,“设备名称和代表=”)
如果位置>0,则“它是来自客户端的设备名称命令”
TempString2=Mid(TempString,InStr(TempString,“=”)+1)
DeviceNameLabel.Caption=“设备名称:”&TempString2
TempString2=“”
如果结束
位置=0
位置=InStr(1,TempString,“设备固件=”)
如果位置>0,则是来自客户端的设备固件命令
TempString2=Mid(TempString,InStr(TempString,“=”)+1)
DeviceFirmwareLabel.Caption=“固件版本:”&TempString2
卸载Form2'因为这是我们将收到的最后一块,我们可以关闭此窗口
TempString2=“”
如果结束
位置=0
位置=仪表(1,临时字符串