Forum Discussion

MulgraveTester's avatar
MulgraveTester
Frequent Contributor
8 years ago
Solved

Please help with VBScript UDP Server

I need to send/receive UDP to my tested app to verify that the correct packets are sent and the GUI behaves correctly. I've tried to convert the JScript example into the following VBScript. It binds ...
  • MulgraveTester's avatar
    MulgraveTester
    8 years ago

    Thanks to Evgeny at SmartBear Customer Care this is now resolved. A "Set" statement was required when defining the array so that an array object was created instead of an array. In case anyone else is trying to achieve the same aim the code now looks like this:

     

    sub UDPReceive()
      'Converted from JScript example: https://support.smartbear.com/viewarticle/9005/
      dim address, port, receiveTimeout, socket, endpoint, byteType, _
            byteData, maxLength, receivedLength, byteStr, addressObj
      address = "0.0.0.0" 
      port = 13004
      receiveTimeout = 15000
     
      'Create the byte array to receive data into
      maxLength = 256
      set byteType = dotNET.System.Type.GetType("System.Byte")
      Set byteData = dotNET.System.Array.CreateInstance(byteType, maxLength)

     

      'Define the socket
      set socket = dotNET.System_Net_Sockets.Socket.zctor_2( _
                  dotNET.System_Net_Sockets.AddressFamily.InterNetwork, _
                  dotNET.System_Net_Sockets.SocketType.Dgram, _
                  dotNET.System_Net_Sockets.ProtocolType.Udp)
      
      socket.ReceiveTimeout = receiveTimeout
       
      'Define and bind to the Endpoint           
      set addressObj = dotNET.System_Net.IPAddress.Parse(address)
      set endpoint = dotNET.System_Net.IPEndPoint.zctor_2(addressObj, port)
      socket.Bind(endpoint)
     
      'Read the data
      on error resume next
      receivedLength = socket.Receive(byteData) 'An exception occurs if no data is received
      if err.number <> 0 then receivedLength = 0
      on error goto 0 'turn off error handling
     
      'Close the socket
      socket.Close()

     

      'Log the data received
      byteStr = ByteArrayToHexString(byteData.oleValue, "")
      Log.Message(receivedLength & " bytes were received: " & byteStr)  

    end sub

    '-----------------------------------------------------------------------------------------------
    function ByteArrayToHexString(byteArray, prefix)
      dim i
      ByteArrayToHexString = ""

      for i = 0 to ubound(byteArray)
        ByteArrayToHexString = ByteArrayToHexString & prefix & right("0" & hex(byteArray(i)), 2) & " "
      next
    end function