HTTP communication, Problem with tcpRead

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
Search

Post Reply
4 posts • Page 1 of 1
mfers
Posts: 2
Joined: Thu Dec 06, 2012 9:23 am

HTTP communication, Problem with tcpRead

Post by mfers »

Hello ETM forum,

while programming a script I am experiencing some problems I can't solve on my own, so I registered to this forum to ask for some help.

What I'm trying to accomplish with this script is to send a HTTP GET-request with tcpWrite via a TCP-connection to a gateway. The server (= gateway) sends a response which generally consists of (at least) two TCP-segments, where the first segment is the HTTP-header and the others are containing the requested data. When receiving an error response (HTTP state "400 NOT FOUND" and equal) everything works as intended, the header is read first and the data-segment containing the error-code and message is read by a second tcpRead function.
Of course the programm is supposed to receive valid responses in the first place, but unfortunately when the server is sending a "200 OK" HTTP-message back, the header and the data-segment are read at once. In some rare cases everything is working as intended (correct segmentation, header and data read step by step), but this occurs normally only after starting up the PVSS II project anew.

When I tried to figure out what causes the problem I analysed the TCP-traffic using wireshark. At first I thought that the fast series of the TCP-segments (small time difference between the segments) is causing the tcpRead function to mess up, but I figured that this wasn't the case with the "400 *" responses so why should it be an issue with "200 OK" messages?

Here is how I coded the programm (without the error-handling):

Code: Select all

#using "CtrlXML"
#using "CtrlZlib"

int iSocket, iBytesSent, iReadCycle;
blob bResponse, bBlobEnding;
string sRequest;
dyn_string dsResponse;

iReadCycle = 0;
sRequest = "GET /addUPI?function=login&user=&passwd= HTTP/1.0\\r\\nConnection: Keep-Alive\\r\\n\\r\\n";

iSocket = tcpOpen("host-address", 80);
iBytesSent = tcpWrite(iSocket, sRequest);

// Reading HTTP-header
tcpRead(iSocket, bResponse, 5);
// BlobToString ... function to convert a blob variable into a string
dsResponse[1] = BlobToString(bResponse);

if ( patternMatch("HTTP/1.[01] 20[01234567] *", dsResponse[1]) )
{
   while ( (string)bBlobEnding != "0D0A" ) // 0x0D0A specifies the ending of the data, 0D = CR (\\r), 0A = LF (\\n)
   {
      iReadCycle++;

      // Reading remaining TCP-segments
      tcpRead(iSocket, bResponse, 5);
      blobGetValue(bResponse, bloblen(bResponse) - 2, bBlobEnding, 2, false);
      gunzip(bResponse, dsResponse[(iReadCylce + 1)]);
   }
}
else if ( patternMatch("HTTP/1.[01] 4[0125][0123456789] *", dsResponse[1]) )
{
   while ( (string)bBlobEnding != "0D0A" )
   {
      iReadCycle++;

      // Reading remaining TCP-segments
      tcpRead(iSocket, bResponse, 5);
      blobGetValue(bResponse, bloblen(bResponse) - 2, bBlobEnding, 2, false);
      dsResponse[(iReadCylce + 1)] = BlobToString(bResponse);
   }
}

// Connection usually terminated by server
try
{
   tcpClose(iSocket);
}
case {}
Right now I'm not sure how to move on as it seems to me that the tcpRead function is indeterminate? Or am I missing out on something?
At this point any help or advice will be appreciated.

Best regards

Maximilian Ferstl

Gertjan van Schijndel
Posts: 634
Joined: Mon Aug 02, 2010 10:37 am

Re: HTTP communication, Problem with tcpRead

Post by Gertjan van Schijndel »

I think that tcpRead will get the data that is available in some internal buffer. This buffer can also contain multiple messages and perhaps even half messages.

When receiving a chunked http response you have to read until you have received a zero sized chunk, which indicates that the message is complete.

mfers
Posts: 2
Joined: Thu Dec 06, 2012 9:23 am

Re: HTTP communication, Problem with tcpRead

Post by mfers »

Yes that sounds logical. But even considering that, I can't think of the reason why the function would behave different if the header, or much rather the first tcp-segment, differs in length?
What I figured would be the best solution for me is to code the general communication-functions in c++ and implement those functions into my control script by function calling.

If there are other solutions or ideas please let me know.

Thanks a lot for the response anyway.

Gertjan van Schijndel
Posts: 634
Joined: Mon Aug 02, 2010 10:37 am

Re: HTTP communication, Problem with tcpRead

Post by Gertjan van Schijndel »

With this library https://www.winccoa.com/fileadmin/image ... Client.ctl I was able to receive chunked http responses.

Post Reply
4 posts • Page 1 of 1