[solved]null-terminated stream

einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
Hi there,

afaik strings in NAV are containing their length in the first character. So, they are not null-terminated (like in C). I want to transfer some data to an external system. The connection type is socket and the data should be transfered as stream. The external system expects a null-termination at the end of a stream.
I did the following...
IF ISCLEAR(CC2) THEN 
  CREATE(CC2);
IF ISCLEAR(SBA) THEN 
  CREATE(SBA);

CC2.AddBusAdapter(SBA, 0);
OutMsg := CC2.CreateoutMessage('Sockets://localhost:5006');

OutS := OutMsg.GetStream();

OutS.WRITETEXT('Some Text 1');
OutS.WRITETEXT('Some Text 2');
OutS.WRITETEXT('Some Text 3');
OutS.WRITETEXT();

IF NOT OutMsg.Send(0) THEN
...
but the external system doesn't react. So, I wonder if the stream isn't null-terminated and the external system doesn't catch the message.
"Money is likewise the greatest chance and the greatest scourge of mankind."

Comments

  • XypherXypher Member Posts: 297
    You could try doing...

    //nullchar declared as Text (length 1~) or Char, whatever you want
    nullchar[1] := 0;
    
    //...YourCode...
    
    OutS.WRITETEXT('Some Text 1');
    OutS.WRITETEXT('Some Text 2');
    OutS.WRITETEXT('Some Text 3');
    OutS.WRITETEXT(); 
    OutS.WRITE(nullchar);
    
    //...etc...
    


    Let me know if it works :D
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    I'm not sure if I could test it like this

    CU 50000 (single instance)
    OnRun()
    IF ISCLEAR(CC2) THEN
      CREATE(CC2);
    
    IF ISCLEAR(SBA) THEN
      CREATE(SBA);
    
    CC2.AddBusAdapter(SBA,0);
    SBA.OpenSocket(6006,'');
    
    CC2::MessageReceived(VAR InMessage : Automation "''.IDISPATCH")
    InMsg := InMessage;
    
    InS := InMsg.GetStream();
    
    WHILE NOT (InS.EOS) DO BEGIN
      IF InS.READ(Txt) > 0 THEN
        FOR i := 1 TO STRLEN(Txt) DO BEGIN
          charint := Txt[i];
          MESSAGE('%1 = %2',Txt[i],charint);
        END;
    END;
    
    InMsg.CommitMessage();
    
    CU 50001
    OnRun()
    IF ISCLEAR(CC2) THEN 
      CREATE(CC2);
    IF ISCLEAR(SBA) THEN
      CREATE(SBA);
    NullChar[1] := 0;
    
    CC2.AddBusAdapter(SBA, 0);
    OutMsg := CC2.CreateoutMessage('Sockets://localhost:6006');
    
    OutS := OutMsg.GetStream();
    OutS.WRITE('1st Text');
    OutS.WRITE('2nd Text');
    OutS.WRITE(NullChar);
    
    OutMsg.Send(0);
    
    but if... it doesn't work. I don't know how null character would be displayed in NAV, but should I get a message off it?
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • XypherXypher Member Posts: 297
    I'm not sure what you're asking...

    Are you referring to the MessageReceived procedure, or in general how NAV would handle a NULL terminating string when displaying?
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    2nd one. General handling. After some test I think NAV won't display anything. And I think NAV will interpret a zero byte as EOS.
    OnRun()
    IF ISCLEAR(CC2) THEN
      CREATE(CC2);
    IF ISCLEAR(SBA) THEN
      CREATE(SBA);
    
    Text1 := '1st Text';
    Text2 := '2nd Text';
    Text1[4] := 0;
    
    CC2.AddBusAdapter(SBA, 0);
    OutMsg := CC2.CreateoutMessage('Sockets://localhost:6006');
    
    OutS := OutMsg.GetStream();
    OutS.WRITE(Text1);
    OutS.WRITE(Text2);
    
    OutMsg.Send(0);
    
    If so I can be sure of that NAV creates a stream the external system is able to understand.
    If not and there are other reasons why NAV doesn't display the whole stream, I have no chance to proof if the external system receives an understandable stream. In any case not with two NAV clients.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
  • einsTeIn.NETeinsTeIn.NET Member Posts: 1,050
    With an outstream variable you will get the null-terminated string / zero byte as EOS if you use
    OutS.WRITE
    
    If you use
    OutS.WRITETEXT
    
    you won't get it.
    "Money is likewise the greatest chance and the greatest scourge of mankind."
Sign In or Register to comment.