Forum Discussion

julie_mcmanus's avatar
julie_mcmanus
Occasional Contributor
11 years ago

script based TCP IP client

Hi I'm running the code for the .net TCP IP client provided on the support pages but cannot figure out how to populate the array (binaryData).  I'm using JScript.  Can you help?

9 Replies

  • julie_mcmanus's avatar
    julie_mcmanus
    Occasional Contributor
    Thanks again, however this isn't exactly what I needed to do.  I do however have something that works now.




      maxLength = 2;


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


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


     


      binaryData.Set(1, 0xA);



    The binaryData.Set() method works with the SendTo() method.

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi Julie,

     


    I guess you are working with the sample Phil's talking about. You need to uncomment these lines:




      maxLength = 256;


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


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



  • julie_mcmanus's avatar
    julie_mcmanus
    Occasional Contributor
    Hi, yes, I did that.  What I can't figure is how to populate the array with my data.  When I use the setvalue method I get this error.
  • Philip_Baird's avatar
    Philip_Baird
    Community Expert
    Hi Julie, I reproduced the problem you are having, it appears Test Complete is not passing a byte to the SetValue() method.

     


    I would expect the following to work as the code is doing an explicit cast to Byte using System.Convert.ToByte (ToByte_13 is the overload that accepts a String paramter).


     


    var maxLength = 256;


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


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


    binaryData.SetValue( dotNET.System.Convert.ToByte_13( "1" ), 0 );


     


    However, this gives the same type exception.


     


    If you change dotNET.System.Type.GetType("System.Byte"); to dotNET.System.Type.GetType("System.Int32"); it works nicely.


     


    Perhaps someone from SmartBear can provide further details.


     


    Regards,


    Phil Baird

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi,

     


    Do you want to use the SetValue method to set values of the array on a client side? I can suggest that you use the following approach to set the array of bytes from a string.


    var binaryData = dotNET.System_Text.Encoding.ASCII.GetBytes_2("SomeMessage");


     


    The approach with GetType("System.Byte") should work fine on the server side.


     

  • julie_mcmanus's avatar
    julie_mcmanus
    Occasional Contributor
    Thanks for the help with this, but still running into problems.  

    I need to send an array of bytes in hex format so for example I might want to send - 0x02 0x88 0x1e...



    Tanya's solution converts each digit from a char so 02 sets the OleValue to 48,50 or hex 30,31 when what I need is simply 02 so I seem to have an issue with the types.



    Phil's solution seems to indeed fill the array with the values I'm after but there are 2 issues -

    I cannot enter alphanumeric such as "3a" otherwise I get an exception

    if I enter just numbers such as "2", "12" etc. the array fills ok but at the next step where I call - 



    socket.SendTo(binaryData, endpoint);  I get an exception, Invalid Proceedure call or argument.



  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)
    Hi Julie,

     


    Refer to this thread. It contains a function that converts the byte array to the HEX string. It may give you some clue what to do next.


     

  • Hi Staff,



    I am having some trouble with the TCP/IP server code published on your website. It fails on socket.bind. the error says :

    Invalid procedure call or argument



    address = "127.0.0.1";

        port = 3751;

        receiveTimeout = 15000;

        connectionsQueueLength = 2;

     

        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

        );

            

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

        

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

            

        socket.Bind(endpoint);

        socket.Listen(connectionsQueueLength);

     

        connectedSocket = socket.Accept();

        connectedSocket.SetSocketOption_3(

            dotNET.System_Net_Sockets.SocketOptionLevel.Socket,

            dotNET.System_Net_Sockets.SocketOptionName.ReceiveTimeout,

            receiveTimeout);

    ..................



    Is there anything that I'm doing wrong?