Options

Recieve response from TCP - function call amiguous

PJE99PJE99 Member Posts: 2
Hi,
I am having problems recieving response from TCP-server through dotnet-vars.
I am connecting to a TCP-server. Then I am sending a command to the TCP-server. When the TCP-server recieves the command it sends simple info back (just to make sure that the command from NAV has been recieved by the TCP-server).
I am able to send the command, but when I add code to recieve the response, I get compile error "Function call is ambiguous. No matching method found". Am I doing it wrong? Or is this not possible?
My code and vars goes here:
TCPDotNet DotNet System.Net.Sockets.TcpClient.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
NetStream DotNet System.Net.Sockets.NetworkStream.'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Encoding DotNet System.Text.Encoding.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Response Text
NoOfBytes Integer
Data DotNet System.Byte.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'


SendCommand(IPAddress : Text[30];PortNo : Integer;MsgToSend : Text[50];VAR ResponseMsg : Text[50]) : Text
TCPDotNet := TCPDotNet.TcpClient(IPAddress, PortNo);
IF NOT TCPDotNet.Connected THEN BEGIN
ResponseMsg := STRSUBSTNO(NVP000, '');
EXIT(FALSE);
END;

// Send command
NetStream := TCPDotNet.GetStream;
NetStream.Write(Encoding.ASCII.GetBytes(MsgToSend), 0, Encoding.ASCII.GetBytes(MsgToSend).Length);

// Recieve response
Response := '';
NoOfBytes := NetStream.Read(Data, 0, 256);
Response := Encoding.GetString(Data, 0, NoOfBytes); // Error here when compiling - function call is ambiguous

NetStream.Close();
TCPDotNet.Close();

EXIT(Response);
Thank you in advance for any answer :)

Comments

  • Options
    xStepaxStepa Member Posts: 106
    Hi, try something like this:
      IF NOT lFloodMode THEN BEGIN
          lTcpClient.SendTimeout(pSyncSetup.Timeout);
          lOutStream := lTcpClient.GetStream;
          lOutStream.WRITETEXT('READ' + lCF.GetCRLF);
      END;
    
      lTcpClient.ReceiveTimeout(pSyncSetup.Timeout);
      lInStream := lTcpClient.GetStream;
      lInStream.READTEXT(lReceivedMessage);
    
    Regards
    xStepa
  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-06-04
    I thnk you need to use an array if bytes, not a byte itself. I am reading and converting the data from TCP/IP stream into a text like this:
    NETType := NETType.GetType('System.Byte',FALSE);
    ByteArr := ByteArr.CreateInstance(NETType, BufferLen);
    BytesRead := TCPNetworkStream.Read(ByteArr, 0, BufferLen);
    ReceivedMsg := NETEncoding.UTF8.GetString(ByteArr);
    
    Where my NETType var is System.Type.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089', the ByteArr var is System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and the NETEncoding var is the same type as yours Encoding
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    PJE99PJE99 Member Posts: 2
    Hi,
    Thank you very much for taking your time on posting reply.
    @Slawek - I tried your solution and it worked perfectly - thank you very very much - I highly appreciate it. Thank you :)
Sign In or Register to comment.