标题:winsock 控件的问题
只看楼主
sam_jiang
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:542
专家分:781
注 册:2021-10-13
结帖率:97.44%
已结贴  问题点数:20 回复次数:5 
winsock 控件的问题
winsock控件连接断开后再连接就出错,是什么原因?
搜索更多相关主题的帖子: winsock 连接 原因 控件 出错 
2023-01-01 23:27
sam_jiang
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:542
专家分:781
注 册:2021-10-13
得分:0 
附上2个form的源代码:
程序代码:
**************************************************
*-- Form:         form1 (d:\documents\visual foxpro 项目\server.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   01/01/23 11:16:01 PM
*
DEFINE CLASS form1 AS form


    Top = 0
    Left = 0
    Height = 629
    Width = 851
    DoCreate = .T.
    Caption = "Server"
    Name = "Form1"


    ADD OBJECT olecontrol1 AS olecontrol WITH ;
        Top = 0, ;
        Left = 820, ;
        Height = 100, ;
        Width = 100, ;
        Visible = .F., ;
        Name = "Olecontrol1"


    ADD OBJECT edit1 AS editbox WITH ;
        Height = 420, ;
        Left = 0, ;
        ReadOnly = .T., ;
        Top = 0, ;
        Width = 570, ;
        Name = "Edit1"


    ADD OBJECT edit2 AS editbox WITH ;
        Height = 208, ;
        Left = 0, ;
        Top = 421, ;
        Width = 770, ;
        Name = "Edit2"


    ADD OBJECT command2 AS commandbutton WITH ;
        Top = 570, ;
        Left = 780, ;
        Height = 50, ;
        Width = 60, ;
        Caption = "\<Send", ;
        Name = "Command2"


    PROCEDURE Init
        *!*    Set the LocalPort property to an integer.
        *!*    Then invoke the Listen method.
        this.olecontrol1.object.LocalPort=1001
        this.olecontrol1.object.Listen()
        *!*    frmClient.Show ' Show the client form.
        DO FORM client
        *!*    this.lostfocus()
        *!*    _screen.Forms(2).gotfocus()
    ENDPROC


    PROCEDURE olecontrol1.Error
        *** ActiveX 控件事件 ***
        LPARAMETERS number, description, scode, source, helpfile, helpcontext, canceldisplay
    ENDPROC


    PROCEDURE olecontrol1.DataArrival
        *** ActiveX 控件事件 ***
        LPARAMETERS bytestotal
        *!*    (ByVal bytesTotal As Long)
        *!*    Declare a variable for the incoming data.
        *!*    Invoke the GetData method and set the Text
        *!*    property of a TextBox named txtOutput to
        *!*    the data.
        *!*    Dim strData As String
        LOCAL strdata
        strdata=REPLICATE(CHR(0),255)
        thisform.olecontrol1.object.getdata(@strdata)
        thisform.edit1.value =thisform.edit1.value+TRANSFORM(DATE())+" "+TRANSFORM(TIME())+CHR(13)+"Client:"
        thisform.edit1.value =thisform.edit1.value+strData
    ENDPROC


    PROCEDURE olecontrol1.ConnectionRequest
        *** ActiveX 控件事件 ***
        LPARAMETERS requestid
        *!*    Private Sub thisform.olecontrol1.object_ConnectionRequest _
        *!*    (ByVal requestID As Long)
        *!*    Check if the control's State is closed. If not,
        *!*    close the connection before accepting the new
        *!*    connection.
        If thisform.olecontrol1.object.State <> 0
            thisform.olecontrol1.object.Close
            *!*    Accept the request with the requestID
            *!*    parameter.
            thisform.olecontrol1.object.Accept(requestID)
        ENDIF
    ENDPROC


    PROCEDURE olecontrol1.Accept
        *** ActiveX 控件方法程序 ***
        LPARAMETERS requestid
    ENDPROC


    PROCEDURE edit2.KeyPress
        LPARAMETERS nKeyCode, nShiftAltCtrl
        IF nkeycode=10 &&AND nshiftaltctrl=2 &&Ctrl+Enter
            ()
        ENDIF

    ENDPROC


    PROCEDURE command2.Click
        *!*    The TextBox control named txtSendData
        *!*    contains the data to be sent. Whenever the user
        *!*    types into the textbox, the string is sent
        *!*    using the SendData method.
        IF !EMPTY(thisform.edit2.value)
            thisform.edit1.Value=thisform.edit1.Value+TRANSFORM(DATE())+" "+TRANSFORM(TIME())+CHR(13)+"Me:"
            thisform.edit1.Value=thisform.edit1.Value+thisform.edit2.Value+CHR(13)
            thisform.olecontrol1.object.SendData(thisform.edit2.value+CHR(13))
            thisform.edit2.Value=""
        ENDIF

    ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************
2023-01-01 23:30
sam_jiang
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:542
专家分:781
注 册:2021-10-13
得分:0 
另一个form的源代码:
程序代码:
**************************************************
*-- Form:         form1 (d:\documents\visual foxpro 项目\client.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   01/01/23 10:52:08 PM
*
DEFINE CLASS form1 AS form


    Height = 629
    Width = 852
    DoCreate = .T.
    AutoCenter = .T.
    Caption = "Client"
    Name = "Form1"


    ADD OBJECT olecontrol1 AS olecontrol WITH ;
        Top = 0, ;
        Left = 820, ;
        Height = 100, ;
        Width = 100, ;
        Visible = .F., ;
        Name = "Olecontrol1"


    ADD OBJECT edit1 AS editbox WITH ;
        Height = 420, ;
        Left = 0, ;
        ReadOnly = .T., ;
        Top = 0, ;
        Width = 570, ;
        Name = "Edit1"


    ADD OBJECT edit2 AS editbox WITH ;
        Height = 208, ;
        Left = 0, ;
        Top = 421, ;
        Width = 770, ;
        Name = "Edit2"


    ADD OBJECT command2 AS commandbutton WITH ;
        Top = 570, ;
        Left = 780, ;
        Height = 50, ;
        Width = 60, ;
        Caption = "\<Send", ;
        Name = "Command2"


    ADD OBJECT label1 AS label WITH ;
        AutoSize = .T., ;
        Caption = "当前状态未连接。。。", ;
        Height = 16, ;
        Left = 580, ;
        Top = 10, ;
        Width = 122, ;
        Name = "Label1"


    ADD OBJECT command1 AS commandbutton WITH ;
        Top = 40, ;
        Left = 580, ;
        Height = 25, ;
        Width = 60, ;
        Caption = "Conncet", ;
        Name = "Command1"


    PROCEDURE Init
        *!*    The name of the Winsock control is tcpClient.
        *!*    Note: to specify a remote host, you can use
        *!*    either the IP address (ex: "121.111.1.1") or
        *!*    the computer's "friendly" name, as shown here.
        thisform.olecontrol1.object.RemoteHost = "TC-20200803MDEV"
        thisform.olecontrol1.object.RemotePort = 1001
        *!*    thisform.olecontrol1.object.RemoteHost = "localhost"
        *!*    thisform.olecontrol1.object.RemotePort = 80
    ENDPROC


    PROCEDURE olecontrol1.Error
        *** ActiveX 控件事件 ***
        LPARAMETERS number, description, scode, source, helpfile, helpcontext, canceldisplay
    ENDPROC


    PROCEDURE olecontrol1.DataArrival
        *** ActiveX 控件事件 ***
        LPARAMETERS bytestotal
        *!*    (ByVal bytesTotal As Long)
        *!*    Dim strData As String
        LOCAL strdata
        strdata=REPLICATE(CHR(0),255)
        thisform.olecontrol1.object.GetData(@strData)
        thisform.edit1.value=thisform.edit1.value+TRANSFORM(DATE())+" "+TRANSFORM(TIME())+CHR(13)+"Sever:" 
        thisform.edit1.value=thisform.edit1.value+strData
    ENDPROC


    PROCEDURE olecontrol1.ConnectionRequest
        *** ActiveX 控件事件 ***
        LPARAMETERS requestid
        *!*    Private Sub thisform.olecontrol1.object_ConnectionRequest _
        *!*    (ByVal requestID As Long)
        *!*    Check if the control's State is closed. If not,
        *!*    close the connection before accepting the new
        *!*    connection.
        *!*    If thisform.olecontrol1.object.State <> 0
        *!*        thisform.olecontrol1.object.Close
        *!*        *!*    Accept the request with the requestID
        *!*        *!*    parameter.
        *!*        thisform.olecontrol1.object.Accept(requestID)
        *!*        ACTIVATE SCREEN 
        *!*        CLEAR
        *!*        ?requestid
        *!*    ENDIF
    ENDPROC


    PROCEDURE olecontrol1.Accept
        *** ActiveX 控件方法程序 ***
        LPARAMETERS requestid
    ENDPROC


    PROCEDURE edit2.KeyPress
        LPARAMETERS nKeyCode, nShiftAltCtrl
        IF nkeycode=10 &&AND nshiftaltctrl=2 &&Ctrl+Enter
            ()
        ENDIF
    ENDPROC


    PROCEDURE command2.Click
        IF thisform.oleconTROL1.obJECT.State=0
            RETURN .f.
        *!*        ()
        ENDIF

        IF !EMPTY(thisform.edit2.value)
            thisform.edit1.Value=thisform.edit1.Value+TRANSFORM(date())+" "+TRANSFORM(TIME())+CHR(13)+"Me:"
            thisform.edit1.Value=thisform.edit1.Value+thisform.edit2.value+CHR(13)
            thisform.olecontrol1.object.SendData(thisform.edit2.value+CHR(13))
            thisform.edit2.Value=""
        ENDIF

    ENDPROC


    PROCEDURE command1.Click
        *!*    Invoke the Connect method to initiate a
        *!*    connection.
        IF this.Caption="Disconnect"
            thisform.olecontrol1.object.Close()
            this.Caption="Connect"
            thisform.label1.Caption="当前连接已断开。。。"
        ELSE
            thisform.olecontrol1.object.Connect()
            thisform.label1.Caption="当前状态已连接。。。"
            this.Caption="Disconnect"
        ENDIF
    ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************
2023-01-01 23:31
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:20 
试试先获取WinSock当前连接状态(Connect/Disconnect)再去执行Connect/Disconnect
或者执行Close()后观察一下是否Disconnect成功。

2023-01-02 08:49
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:0 
没用过WinSock控件,不清楚他是用那种I/O模型,有些I/O模型是异步通信的,这点要注意。

2023-01-02 11:19
sam_jiang
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:542
专家分:781
注 册:2021-10-13
得分:0 
调试时,特地注意观察了state的值,发现频繁上线下线,这个值就会出错,一会8,一会9,一会4。看来不能为连接设置一个command,要杜绝用户手动连接。。。
注:
State属性的设置如下:
常量                    值   描述
SckClosed                0   缺省。关闭
SckOpen                  1   打开
SckListening             2   监听
SckConnectionPending     3   连接未决
SckResolvingHost         4   解析主机
SckHostResolved          5   主机被解析
SckConnecting            6   连接
SckConnected             7   已连接
SckClosing               8   对方在关闭连接
SckError                 9   错误
2023-01-02 20:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511037-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.113285 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved