Microsoft Access VBA 代码无法从 TCP/IP 端口接收数据

问题描述

我在 Microsoft Access 中使用 VBA 连接到小工具的 IP 地址和端口,连接和发送 VBA 代码功能工作正常,但接收 VBA 功能阻塞了我的计算机并导致它停止响应.因此,我无法从小工具中获得结果,因此我需要您的帮助。一旦整理出来,它将帮助我实现我的目标。

以下是接收数据失败的接收VBA代码的结构:

Public Function RecvAscii(dataBuf As String,ByVal maxLength As Integer) As Integer
    Dim count As Long
    Dim c As String * 30720
    Dim length As Integer
    
    dataBuf = "30720"
    While length < maxLength
        DoEvents
        count = recv(socketId,c,30720,0)
        If count < 1 Then
            RecvAscii = RECV_ERROR
            dataBuf = Chr$(0)
            Exit Function
        End If
        
        If c = Chr$(10) Then
           dataBuf = dataBuf + Chr$(0)
           RecvAscii = NO_ERROR
           Exit Function
        End If
        
        length = length + count
        dataBuf = dataBuf + c
    Wend
    
    RecvAscii = RECV_ERROR
    
End Function

下面是我用来调用上述函数来接收数据的 VBA 代码

Private Sub CmdCread_Click()
Dim x As Long
Dim strData As String * 30720
Call OpenSocket("192.168.1.197",8888)
' Read maximum of 30720 bytes from TCP/IP Port.Please note 30720 = buffer while 30720 = Length
x = RecvAscii(strData,30720)
End Sub

我还包含了 VBA 模块 Bas,它旨在帮助上述接收 VBA 函数工作:

Type Hostent
  h_name As Long
  h_aliases As Long
  h_addrtype As String * 2
  h_length As String * 2
  h_addr_list As Long
End Type
Public Const SZHOSTENT = 16


'Set the Internet address type to a long integer (32-bit)
Type in_addr
   s_addr As Long
End Type


'A note to those familiar with the C header file for Winsock
'Visual Basic does not permit a user-defined variable type
'to be used as a return structure.  In the case of the
'variable deFinition below,sin_addr must
'be declared as a long integer rather than the user-defined
'variable type of in_addr.
Type sockaddr_in
   sin_family As Integer
   sin_port As Integer
   sin_addr As Long
   sin_zero As String * 8
End Type

Public Const WSADESCRIPTION_LEN = 256
Public Const WSASYS_STATUS_LEN = 128
Public Const WSA_DescriptionSize = WSADESCRIPTION_LEN + 1
Public Const WSA_SysstatusSize = WSASYS_STATUS_LEN + 1

'Setup the structure for the information returned from
'the WSAStartup() function.
Type WSAData
   wVersion As Integer
   wHighVersion As Integer
   szDescription As String * WSA_DescriptionSize
   szSystemStatus As String * WSA_SysstatusSize
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   lpvendorInfo As String * 200
End Type

'Define socket return codes
Public Const INVALID_SOCKET = &HFFFF
Public Const SOCKET_ERROR = -1

'Define socket types
Public Const SOCK_STREAM = 1           'Stream socket
Public Const SOCK_DGRAM = 2            'Datagram socket
Public Const SOCK_RAW = 3              'Raw data socket
Public Const SOCK_RDM = 4              'Reliable Delivery socket
Public Const SOCK_SEQPACKET = 5        'Sequenced Packet socket


'Define address families
Public Const AF_UNSPEC = 0             'unspecified
Public Const AF_UNIX = 1               'local to host (pipes,portals)
Public Const AF_INET = 2               'internetwork: UDP,TCP,etc.
Public Const AF_IMPLINK = 3            'arpanet imp addresses
Public Const AF_PUP = 4                'pup protocols: e.g. BSP
Public Const AF_CHAOS = 5              'mit CHAOS protocols
Public Const AF_NS = 6                 'XEROX NS protocols
Public Const AF_ISO = 7                'ISO protocols
Public Const AF_OSI = AF_ISO           'OSI is ISO
Public Const AF_ECMA = 8               'european computer manufacturers
Public Const AF_DATAKIT = 9            'datakit protocols
Public Const AF_CCITT = 10             'CCITT protocols,X.25 etc
Public Const AF_SNA = 11               'IBM SNA
Public Const AF_DECnet = 12            'DECnet
Public Const AF_DLI = 13               'Direct data link interface
Public Const AF_LAT = 14               'LAT
Public Const AF_HYLINK = 15            'NSC Hyperchannel
Public Const AF_APPLETALK = 16         'AppleTalk
Public Const AF_NETBIOS = 17           'NetBios-style addresses
Public Const AF_MAX = 18               'Maximum # of address families


'Setup sockaddr data type to store Internet addresses
Type sockaddr
  sa_family As Integer
  sa_data As String * 14
End Type
Public Const SADDRLEN = 16


'Declare Socket functions

Public Declare PtrSafe Function closesocket Lib "wsock32.dll" (ByVal s As LongPtr) As Long

Public Declare PtrSafe Function connect Lib "wsock32.dll" (ByVal s As LongPtr,addr As sockaddr_in,ByVal namelen As Long) As Long

Public Declare PtrSafe Function htons Lib "wsock32.dll" (ByVal hostshort As LongPtr) As Integer

Public Declare PtrSafe Function inet_addr Lib "wsock32.dll" (ByVal cp As String) As Long

Public Declare PtrSafe Function recv Lib "wsock32.dll" (ByVal s As LongPtr,ByVal buf As Any,ByVal buflen As LongPtr,ByVal flags As LongPtr) As Long

Public Declare PtrSafe Function recvB Lib "wsock32.dll" Alias "recv" (ByVal s As LongPtr,buf As Any,ByVal flags As LongPtr) As Long

Public Declare PtrSafe Function send Lib "wsock32.dll" (ByVal s As LongPtr,ByVal flags As LongPtr) As Long

Public Declare PtrSafe Function socket Lib "wsock32.dll" (ByVal af As LongPtr,ByVal socktype As LongPtr,ByVal protocol As LongPtr) As Long

Public Declare PtrSafe Function WSAStartup Lib "wsock32.dll" (ByVal wVersionrequired As LongPtr,lpWSAData As WSAData) As Long

Public Declare PtrSafe Function WSACleanup Lib "wsock32.dll" () As Long

Public Declare PtrSafe Function WSAUnhookBlockingHook Lib "wsock32.dll" () As Long

Public Declare PtrSafe Sub copyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any,hpvSource As Any,ByVal cbcopy As LongPtr)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)