Forum Discussion

narender_1's avatar
narender_1
Contributor
12 years ago

How to write a TCP/IP client server socket program in vbscript?

Hi,

   Please anyone help me for writing a TCP/IP client server socket program in vbscript.

In testcomplete documentation the same program is written for javascript.

   Will anyone help me to convert the same into vbsctipt?





Thanks,

Manne

5 Replies

  • reshma_bh's avatar
    reshma_bh
    New Contributor





    Function Test()


     


      Dim address, port, receiveTimeout, socket, connectionsQueueLength, connectedSocket, broadcast, endpoint, byteType, binaryData, maxLength, receivedLength, byteStr, str


      


      address = "122.188.113.56"


      port = 3000


      receiveTimeout = xxxx


      connectionsQueueLength = 0


     


      Set Socket = dotNET.System_Net_Sockets.Socket.zctor(dotNET.System_Net_Sockets.AddressFamily.InterNetwork,dotNET.System_Net_Sockets.SocketType.Stream,dotNET.System_Net_Sockets.ProtocolType.Tcp)


      Set broadcast = dotNET.System_Net.IPAddress.Parse(address)


      Set endpoint = dotNET.System_Net.IPEndPoint.zctor_2(broadcast, port)


      socket.Bind(endpoint)


      socket.Listen(connectionsQueueLength)


      


      connectedSocket = socket.Accept()


      Call connectedSocket.SetSocketOption_3(dotNET.System_Net_Sockets.SocketOptionLevel.Socket,dotNET.System_Net_Sockets.SocketOptionName.ReceiveTimeout,receiveTimeout)


        


      maxLength = 256


      byteType = dotNET.System.Type.GetType("System.Byte")


      binaryData = dotNET.System.Array.CreateInstance(byteType, maxLength)


     


      If connectedSocket.ReceiveFrom(binaryData, endpoint)Then


      


        receivedLength = connectedSocket.ReceiveFrom(binaryData, endpoint)


      Else


          '// An exception occurs if no data is received


        receivedLength = 0


      End IF


      byteStr = ByteArrayToHexString(binaryData, receivedLength, "0x")


      str = dotNET.System_Text.ASCIIEncoding.get_ASCII().GetString(binaryData)


      str.SetLength(receivedLength)


      Log.Message (receivedLength + " bytes were received: " + byteStr +"String Value: " + str)


      


      if (str = "TestMessage") Then


         Log.Message "The received data is valid and a positive response will be sent"


        binaryData = dotNET.System_Text.Encoding.ASCII.GetBytes_2("OK")


      


      else 


          Log.Message "The received data is invalid,a negative response will be sent"


        binaryData = dotNET.System_Text.Encoding.ASCII.GetBytes_2("OK")


      End IF


             


          connectedSocket.Close


          socket.Close   


              


     


    End Function


     


    Function ByteArrayToHexString(byteArray, byteCount, prefix)


     


      Dim i, string


      string = ""


     i = 1 


    Do While i = (i < byteArray.Length) & (i < byteCount)


      string = dotNET.System.String.Format(prefix + "{0:X} ", byteArray.Get(i))


      return string  


    Loop


       


    End Function








    Thanks,

    Reshma


     

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Manne,


     


    TestComplete documentation is huge! Post here the link where you found an example in JScript so that everybody could see the example.


     

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi,


     


    Great! As far as I can see, the System and System.Net assemblies are used there. Calling them in VBScript and JScript is very similar. You only need to use the Set statement to get the result in VBScript. Other changes are related only to the language syntax. Does anybody want to translate the scripts? We can add VBScript samples to these How To articles and specify the author name :).


     

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi Reshma,


     


    Thanks for the VBScript sample! I'll pass it to the team who can update the How To article.