客户端向本机的一个软件发送信息,由软件根据信息执行命令,每次执行命令的间隔为2~3S
客户端关键代码如下
Private Sub Form_Load()
    Winsock1.close
    Winsock1.RemoteHost = "150.158.58.110"
    Winsock1.RemotePort = 5998
    Winsock1.Connect
    SockCL.close
    SockCL.RemoteHost = "127.0.0.1" '指定服务器主机名 
     SockCL.RemotePort = 5999 '指定服务器端口名
     SockCL.Connect '连接到服务器
End SubPrivate Sub Command1_Click()
 Timer2.Enabled = True '发送消息1
 Timer3.Enabled = True '发送消息2
 Timer5.Enabled = True '发送消息3
 Timer9.Enabled = True '发送消息4
 Timer10.Enabled = True '发送消息5
End SubPublic Sub f4()
    Winsock1.close
     Winsock1.RemoteHost = "150.158.58.110"
    Winsock1.RemotePort = 5998
    Winsock1.Connect
    SockCL.close
    SockCL.RemoteHost = "127.0.0.1"
    SockCL.RemotePort = 5999
    SockCL.LocalPort = 0
    SockCL.Connect
End SubPublic Sub f1(Optional s1 As Variant)
Dim b1 As Byte
Dim b2 As Byte
Dim b3 As Byte
Dim b4 As Byte
Dim b5 As Byte
Dim b6 As Byte
Dim b7 As Byte
Dim b8 As Byte
Dim b9 As Byte
Dim b10 As Byte
Dim b11 As Byte
Dim b12 As Byte
Dim b13 As Byte
Dim b14 As Byte
Dim b15 As Byte
Dim b16 As Byte
Dim b17 As Byte
Dim b18 As Byte
Dim b19 As Byte
Dim b20 As Byte
Dim b21 As Byte
Dim b22 As Byte
Dim b23 As Byte
Dim b24 As Byte
Dim SendMsg(24) As Byte
'Dim SendMsg(21) As ByteAdodc1.RecordSource = "SELECT cmd1,cmd2,cmd3,cmd4,cmd5,cmd6,cmd7,cmd8,cmd9,cmd10,cmd11,cmd12,cmd13,cmd14,cmd15,cmd16,cmd17,cmd18,cmd19,cmd20 FROM mode1 WHERE id = " + Trim(s1) + ""
Adodc1.Refreshb1 = Adodc1.Recordset.Fields(0) '&HBB
b2 = Adodc1.Recordset.Fields(1) '&HBB
b3 = Adodc1.Recordset.Fields(2) '&HBB
b4 = Adodc1.Recordset.Fields(3) '&HBB
b5 = Adodc1.Recordset.Fields(4) '&H0
b6 = Adodc1.Recordset.Fields(5) '&H0
b7 = Adodc1.Recordset.Fields(6) '&H0
b8 = Adodc1.Recordset.Fields(7) '&H9 'b8 = &H1
b9 = Adodc1.Recordset.Fields(8) '"&H" & x1 'b9=&H4   b9 = &H3
b10 = Adodc1.Recordset.Fields(9) '&H0
b11 = Adodc1.Recordset.Fields(10) '&H0
b12 = Adodc1.Recordset.Fields(11) '&H0
b13 = Adodc1.Recordset.Fields(12) '"&H" & x2 '&H0
b14 = Adodc1.Recordset.Fields(13) '&H0
b15 = Adodc1.Recordset.Fields(14) '&H0
b16 = Adodc1.Recordset.Fields(15) '&H0
b17 = Adodc1.Recordset.Fields(16) 'x3 '&H0"
b18 = Adodc1.Recordset.Fields(17) '&HDD
b19 = Adodc1.Recordset.Fields(18) '&HDD
b20 = Adodc1.Recordset.Fields(19) '&HDD
    SendMsg(0) = b1
    SendMsg(1) = b2
    SendMsg(2) = b3
    SendMsg(3) = b4
    SendMsg(4) = b5
    SendMsg(5) = b6
    SendMsg(6) = b7
    SendMsg(7) = b8
    SendMsg(8) = b9
    SendMsg(9) = b10
    SendMsg(10) = b11
    SendMsg(11) = b12
    SendMsg(12) = b13
    SendMsg(13) = b14
    SendMsg(14) = b15
    SendMsg(15) = b16
    SendMsg(16) = b17
    SendMsg(17) = b18
    SendMsg(18) = b19
    SendMsg(19) = b20  
    SockCL.SendData SendMsg
End SubPrivate Sub Timer1_Timer() 'Timer1.Interval=200
Call f4
Timer1.Enabled = False
End SubPrivate Sub Timer2_Timer() 'Timer1.Interval=888
Dim x1 As Variant
x1 = 17
Timer1.Enabled = True
Call f1(x1)
Timer2.Enabled = False
End SubPrivate Sub Timer3_Timer() 'Timer3.Interval=1888
Dim x1 As Variant
x1 = 18
Timer1.Enabled = True
Call f1(x1)
Timer3.Enabled = False
End SubPrivate Sub Timer5_Timer() 'Timer5.Interval=2888
Dim x1 As Variant
x1 = 25
Timer1.Enabled = True
Call f1(x1)
Timer5.Enabled = False
End SubPrivate Sub Timer9_Timer() 'Timer9.Interval=5888
Dim x1 As Variant
Adodc1.RecordSource = "SELECT cctv222 FROM lingshi WHERE id = '1'"
Adodc1.Refresh
Select Case Adodc1.Recordset.Fields(0)
Case "0"
x1 = 12
Timer1.Enabled = True
Call f1(x1)
Timer9.Enabled = False
Case "1"
x1 = 13
Timer1.Enabled = True
Call f1(x1)
Timer9.Enabled = False
End Select
End SubPrivate Sub Timer10_Timer() 'Timer10.Interval=6888
Dim x1 As Variant
Adodc1.RecordSource = "SELECT cctv223 FROM lingshi WHERE id = '1'"
Adodc1.Refresh
Select Case Adodc1.Recordset.Fields(0)
Case "0"
x1 = 14
Timer1.Enabled = True
Call f1(x1)
Timer10.Enabled = False
Case "1"
x1 = 15
Timer1.Enabled = True
Call f1(x1)
Timer10.Enabled = False
End Select
End Sub
调试时,有时能正常运行,有时当运行到函数f1的SOCKCL.SENDDATA时弹出40006报错(40006”所需事物或请求的错误协议或连接状态),在SOCKCL.SENDDATA前加入网络状态判断句MSGBOX SOCKCL.STATE,值为6(CONNECTING连接中),所以不能发送
请各位高手指教下如何才能避免这种情况,使程序正常运行,最好能帮我修改下,先谢谢了.

解决方案 »

  1.   

    1.先说原因:
      因为你在Timer1 中每过一段时间就去调用 f4 .
      而 f4 是一个每次运行时都要先断开,再连接的程序.(整过过程因网络等各方面的原因而时间长短不一)2.以后你在每个 Timer 事件中 让 Timer1 运行. 当出现此种情况的时间,每出现 状态值为6.当你发送的时候会出现
      40006”所需事物或请求的错误协议或连接状态 的错误.
      出现的状态为: 当F4 正在断开并在连接的时候,(因为此过程要一段时间)而此时,某个 Timer 事件里面进行了发送信息.3.解决办法.
      F4 不要放在 Timer 事件中.  可以放在Load 事件中,或由二个按扭控件,一个连接,一个断开.
      当连接时,让网络一直处于连接状态. (如果不想连接可以手动断开).
      
      

  2.   

    COMMAND_CLICK事件是客户端给软件发送一连串信息后,让软件执行根据信息执行一连串命令,所以不可能没发送一条信息在按下按钮,这样没有连动性了.
    向本机发送信息应该很快的,有时候很快连上了,有时候却很慢,
    加了延时都不行
    SOCKCL.SENDDATA SNEDMSG 
    FOR I = 1 TO 60000 STEP 1 '延时
    I=I+1
    NEXT I
    请高手解决下,我整个程序就差这个问题了,分少可以加分的
      

  3.   

    有时候FORM_LOAD第一次连接也连接不上,状态为CONNECTING