做一个C/S系统,需要要服务端同时向几个客户端发送消息,想用如下代码实现,可是,所有的客户端中,很多客户经常收不到消息,请大家帮看看指点指点。不胜感谢。
这是服务器代码:
'广播信息
ReDim ByteArray(Len(Gave(Index + 1)))'Gave()为自定义结构类型变量
CopyMemory ByteArray(0), Gave(Index + 1), Len(Gave(Index + 1))'将Gave()变量转换为Byte
For i = 0 To 3
  If Gave(Index + 1).CHY(i).CEN <> 0 Then
    tcpServer(Gave(Index + 1).CHY(i).CEN).SendData ByteArray()
  End If
Next
这段代码在运行过程中,确实执行了,但几个在线的客户中,经常是只有一个客户端收到信息,其实收不到。(几个在线的客户向服务器发送信息却很正常)
还有更怪的事:如果上述代码在断点调试方式一行一行运行,所有的客户都能收到,如果不断点,大部分都收不到。
怪!
盼大家帮忙解答。盼大家帮忙解决:如果才能让所有的客户都能收到消息?

解决方案 »

  1.   

    ReDim ByteArray(Len(Gave(Index + 1)))'Gave()为自定义结构类型变量
    CopyMemory ByteArray(0), Gave(Index + 1), Len(Gave(Index + 1))'将Gave()变量转换为Byte
    For i = 0 To 3
      If Gave(Index + 1).CHY(i).CEN <> 0 Then
        tcpServer(Gave(Index + 1).CHY(i).CEN).SendData ByteArray()
       '//加
       DoEvents    
      End If
    Next
    或者
    ReDim ByteArray(Len(Gave(Index + 1)))'Gave()为自定义结构类型变量
    CopyMemory ByteArray(0), Gave(Index + 1), Len(Gave(Index + 1))'将Gave()变量转换为Byte
    For i = 0 To 3
      If Gave(Index + 1).CHY(i).CEN <> 0 Then
        tcpServer(Gave(Index + 1).CHY(i).CEN).SendData ByteArray()
       '//加
        Dim WaitTime As Integer           '延迟
        WaitTime = 0                      
        Do Until WaitTime = 1000            
        DoEvents:WaitTime = WaitTime + 1: Loop  
    End If
    Next
      

  2.   

    ReDim ByteArray(Len(Gave(Index + 1)))'Gave()为自定义结构类型变量
    CopyMemory ByteArray(0), Gave(Index + 1), Len(Gave(Index + 1))'将Gave()变量转换为Byte
    For i = 0 To 3
      If Gave(Index + 1).CHY(i).CEN <> 0 Then
        tcpServer(Gave(Index + 1).CHY(i).CEN).SendData ByteArray()
    doevents
      End If
    Next