Winsock的Connection_Request事件是否多线程的?
背景:来了n个Connection_Request事件:第一个没处理完,第二个又来了,一共来了n个,这时第一个还没有处理完。
问题:是先处理完第一个,再开始第二处理;处理完第二个再开始第三个......one by one的处理;
还是n个同时处理,互不影响?
例如 :
Private Sub wskSrv_ConnectionRequest(index As Integer, ByVal requestID As Long)
Static ID as long
Dim myID as Long
ID=ID + 1
myID = ID
do someThing
........
sleep 1000
........
if someStat then exit DO
loop
MsgBox "myID=" & myID
end Sub
[解决办法]
系统会把SOCKET连接请求进行排队处理,如果使用API,你可以看到一个设置队列大小的参数:backlog。
[解决办法]
Private Sub tcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
LastIndex = LastIndex + 1
Load tcpServer(LastIndex)
tcpServer(LastIndex).Accept requestID
End Sub
Private Sub tcpServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim Buffer As String
tcpServer(Index).GetData Buffer, vbString
End Sub
加载winsock控件数组,分别处理。