tcpRead problem

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
6 posts • Page 1 of 1
MattPaulissen
Posts:26
Joined: Fri Feb 19, 2016 9:27 pm

tcpRead problem

Post by MattPaulissen »

I'm using tcpWrite and tcpRead to communicate to a pop3 email server, but I'm seeing some weird behavior. I'm sending a write to retrieve the emails in a for loop, but the tcpRead is actually truncating portions of the message off. In effect it is breaking one message into two and causing me some problems. I'm not sure if this is a buffer issue or what, but it goes away if I induce a slight delay between the write and the read. Is there something I'm doing incorrectly, or is there a better way to ensure that the read has grabbed the full message before I continue along parsing the message?

Here is my code if you want to look. It's only in testing right now:

Code: Select all

  int readStatus, messageIndex, numberOfMessages;
  string response, from, messageSubject;
  //Query messages    
  tcpWrite (pop3connection, "STAT \\r\\n");
  //Read response from pop3 server
  readStatus = tcpRead(pop3connection, response,5);
  //Show response
  DebugN(response);
  //Parse number of messages
  numberOfMessages = strsplit(response, ' ')[2];
  DebugN("Number of Emails in mbox: "+ numberOfMessages);
  
  //Retrieve messages
  for (messageIndex = 1; messageIndex

mkoller
Posts:741
Joined: Fri Sep 17, 2010 9:03 am

Re: tcpRead problem

Post by mkoller »

It's important to not assume a specific number of bytes (or lines, etc.) to receive on tcpRead(). What was received can be any arbitrary part of the byte stream the server sends, depending on
how the tcp packets were created.
The correct approach is to scan the received bytes for specific delimiters which tell you if you already have received a part of the whole you can already parse.
E.g. if you read line by line, you can just know that a whole line was received when you also received already the newline character. Anything before having that is just a partly received message
and you need to tcpRead() again in a loop until your received bytes include the delimiter.
Also note that using a timeout is not enough! You can not know how the parts of the whole message are received and when.

MattPaulissen
Posts:26
Joined: Fri Feb 19, 2016 9:27 pm

Re: tcpRead problem

Post by MattPaulissen »

Got it!

So I should probably look for "\\r\\n . \\r\\n" since that signifies the end of the pop message? Thank you!

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

Re: tcpRead problem

Post by Gertjan van Schijndel »

Why do you not just use the function 'emRetrieveMail'?

MattPaulissen
Posts:26
Joined: Fri Feb 19, 2016 9:27 pm

Re: tcpRead problem

Post by MattPaulissen »

I tried it, but it was failing to retrieve any messages. I wasn't sure exactly why, but I did look at it to reverse engineer the commands. Mine seems to be working now, and I'm even trying to decode a picture that is attached to the email to show it on screen!


Right now I've got everything done, but for some reason the base64decode is failing. It says I have an invalid argument, but it looks like how the help says to use it. Here is that snippet:

string fileName;
string decoded;
int status;
fileName = getPath(PICTURES_REL_PATH, "icon.jpg");
if (fileName == "")
fileName = getPath(PICTURES_REL_PATH) + "icon.jpg";
DebugN(fileName);
status = base64Decode(imageFile, decoded);
DebugN(status);

MattPaulissen
Posts:26
Joined: Fri Feb 19, 2016 9:27 pm

Re: tcpRead problem

Post by MattPaulissen »

Nevermind! I got it!

I wrote my own base 64 decoder and got it working. I'm not sure what the OA version was complaining about :blink:

6 posts • Page 1 of 1