大概例子如下:
Global $aConnected[1] = [0]
; 建立TrayItem,启动TCP,开始侦听端口。略
While 1
Switch TrayGetMsg()
Case $hExitItem
ExitLoop
Case Else
Accept()
Recv()
EndSwitch
WEnd
; 退出处理
Exit
Func Accept()
$ConnectedSocket = TCPAccept($MainSocket)
If $ConnectedSocket <> -1 Then
_ArrayAdd($aConnected, $ConnectedSocket)
$aConnected[0] += 1
EndIf
Return
EndFunc
Func Recv()
$i = 1
While $i <= UBound($aConnected) - 1
$sRecv = TCPRecv($aConnected[$i], 256)
If @error Then ; 客户端已关闭
_ArrayDelete($aConnected, $i)
$aConnected[0] -= 1
ElseIf $sRecv = "" Then ; 客户端未发送数据,继续处理下一客户端
$i += 1
Else
; 接收数据处理。完成后TCPCloseSocket,然后删除数组单元并将 $aConnected[0] -= 1
EndIf
WEnd
Return
EndFunc
|