一個Winsock組件

來源:互聯網
上載者:User
我自己寫了一個Winsock組件,很簡單,將帶發送的報文發給指定IP的指定連接埠,並返回應答報文。技術粗糙, 請大家指教。附源碼如下:

-------------------------------------


VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsWinSockEm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
'******************************************************************************
'
' clsWinSockEm.CLS
' 嵌入式WinSocket類
' 輸入:伺服器IP、伺服器Port、待發送報文
' 輸出:接收報文
'
'******************************************************************************

Option Explicit

'******************************************************************************

Const RECV_TIME_OUT = 60        ' 接收逾時
Const SEND_TIME_OUT = 60        ' 發送逾時
Const CONN_TIME_OUT = 120       ' 連線逾時
Const RECV_MAX_LEN = 2048       ' 接收報文最大數

'******************************************************************************

Private WithEvents wskEm As MSWinsockLib.Winsock    ' WinSock Control
Attribute wskEm.VB_VarHelpID = -1
Private mstrServerIP As String                      ' 遠程伺服器IP
Private mintServerPort As Integer                   ' 遠程伺服器連接埠
Private mstrTextToSend As String                    ' 發送報文
Private mstrTextReceived As String                  ' 接收報文
Private bConnected As Boolean                       ' 串連標誌

'******************************************************************************

' 初始化
Public Sub Init(strServerIP As String, intServerPort As Integer)
    mstrServerIP = strServerIP
    mintServerPort = intServerPort
    mstrTextToSend = ""
    mstrTextReceived = ""
    bConnected = False
End Sub

' 返回報文
Public Function ReturnText(strTextToSend As String) As String
    Dim dtStart As Date
    dtStart = Now

    Set wskEm = New MSWinsockLib.Winsock
    
    ' 擷取發送字串
    mstrTextToSend = strTextToSend
    mstrTextToSend = mstrTextToSend & Chr(0)
    
    ConnectServer
    SendText
    
    ' 接收報文逾時控制
    Do Until Not (mstrTextReceived = "")
        DoEvents
        If DateDiff("s", dtStart, Now) > RECV_TIME_OUT Then
            Err.Raise vbObjectError, "Socket錯誤", "通訊逾時"
        End If
    Loop
    
    wskEm.Close
    Set wskEm = Nothing

    ReturnText = mstrTextReceived
End Function

' 串連伺服器
Private Sub ConnectServer()
    Dim dtStart As Date
    
    dtStart = Now()
    
    wskEm.RemoteHost = mstrServerIP
    wskEm.RemotePort = mintServerPort
    wskEm.Connect
    
    Do Until bConnected
        DoE



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。