与一个或多个服务器通信的VB6客户端应用程序

时间:2015-12-12 12:57:00

标签: sockets tcp vb6 sleep

我有一个VB6客户端应用程序,它创建一个或多个(最多4个)套接字并连接到一个或多个TCP服务器。

客户端应该不断向服务器发送请求并等待服务器响应某个responseTime。如果响应未到达" responseTime",则客户端应在其中一个套接字上发送下一个请求。

让客户端等到响应到达套接字的最佳方法是什么?

我执行以下操作让客户端等待响应/数据到达:(这里dataProcessed标志由dataArrival()例程调用的辅助函数设置为True。此标志表示已收到响应并加工。

 *Do While ((Timer < SentRequestTime) + responseTimeout) And (dataProcessed = False))

     'DoEvents OR Sleep
     Sleep 50
 End If
Loop*
  1. 如果我使用&#34; DoEvents&#34;在while循环中,应用程序工作正常一段时间,但后来即使响应返回到TCP层(我通过wireshark检查过),应用程序也没有得到DataArrival事件。

  2. 如果我使用&#34; sleep&#34;,dataArrival事件在while循环期间不会被传递,但是一旦循环结束就会到达。使用sleep会使应用程序无响应。

  3. 让单线程VB6套接字客户端应用程序发送请求的最佳方法是什么,&#34;等待数据&#34;到达一段时间,然后继续下一个请求?

1 个答案:

答案 0 :(得分:1)

I would forget about both DoEvents() and Sleep() here. Those are tools of last resort, and nearly no program should contain either one. You need to "think 4th dimensionally" i.e. "Trust the Events, Luke!" This ain't your daddy's QBasic.

Here's a simulation where four Command buttons act as the servers, i.e. you click them manually as they become enabled. Two Timer controls are used here because we need to simulate processing time and transmission delay.

Option Explicit

'Use 4 Command buttons to simulate TCP sockets making server
'requests and getting back responses.  Each "send" must get
'a response within RESPONSE_TIME_MS or be counted as a "miss."
'A new request is sent in either case.

Private Const PROCESS_TIME_MS As Long = 2000
Private Const PROCESS_TICKS As Long = PROCESS_TIME_MS \ 10
Private Const PROCESS_TICK_MS As Long = PROCESS_TIME_MS \ PROCESS_TICKS

Private Const RESPONSE_TIME_MS As Long = 4000
Private Const RESPONSE_TICKS As Long = RESPONSE_TIME_MS \ 10
Private Const RESPONSE_TICK_MS As Long = RESPONSE_TIME_MS \ RESPONSE_TICKS

Private ProcessCountdowns(0 To 3)
Private ResponseCountdowns(0 To 3)
Private Misses(0 To 3)

Private Sub SendRequest(ByVal Socket As Integer)
    ResponseCountdowns(Socket) = RESPONSE_TICKS
    cmdResponse(Socket).Enabled = True
End Sub

Private Sub cmdResponse_Click(Index As Integer)
    'This is a "DataArrival" event.

    'Process the response, then send a new request:
    cmdResponse(Index).Enabled = False
    ResponseCountdowns(Index) = 0
    ProcessCountdowns(Index) = PROCESS_TICKS
End Sub

Private Sub Form_Load()
    Dim Socket As Integer

    For Socket = 0 To 3
        SendRequest Socket
    Next
    tmrProcess.Interval = PROCESS_TICK_MS
    tmrProcess.Enabled = True
    tmrResponse.Interval = RESPONSE_TICK_MS
    tmrResponse.Enabled = True
End Sub

Private Sub tmrProcess_Timer()
    'This just simulates delay involved in processing responses and
    'then sending new ones.
    Dim Socket As Integer

    For Socket = 0 To 3
        If ProcessCountdowns(Socket) > 0 Then
            ProcessCountdowns(Socket) = ProcessCountdowns(Socket) - 1
            If ProcessCountdowns(Socket) <= 0 Then
                SendRequest Socket
            End If
        End If
    Next
End Sub

Private Sub tmrResponse_Timer()
    Dim Socket As Integer

    For Socket = 0 To 3
        If ResponseCountdowns(Socket) > 0 Then
            ResponseCountdowns(Socket) = ResponseCountdowns(Socket) - 1
            If ResponseCountdowns(Socket) <= 0 Then
                Misses(Socket) = Misses(Socket) + 1
                lblMisses(Socket).Caption = CStr(Misses(Socket))
                SendRequest Socket
            End If
        End If
    Next
End Sub

Running the simulation requires two control arrays: one of 4 Command buttons and one of 4 Labels. Then it becomes a game of "Whack a Mole."

Pretty routine stuff actually, and the main reason we have Timer controls in the first place.

相关问题