Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
6 posts • Page 1 of 1
6 posts
• Page 1 of 1
MattPaulissen
Posts:26
Joined: Fri Feb 19, 2016 9:27 pm
tcpRead problem
Postby 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:
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.
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);