PDA

View Full Version : Pop3



DmitryNik
15th September 2011, 19:04
Hello!

I have a small problem related to "How does Gpop works?". So the problem is, that STAT-command shows incorrect information about messages amount in my mailbox(I have about 756 messages, STAT-command shows 356). I've read there: http://groups.google.com/group/Gmail-Help-POP-and-IMAP-en/browse_thread/thread/8be2c0b2bf383c15/7c57310cd5b11fc9 , that gmail saves messages in batches and I need to reconnect for getting a new one. And here is the problem:
- Program connects the first time;
- It retrieves all messages;
- Connection is closed by QUIT-command;
- I tried to reconnect by sending once again the empty message. But server, simply, doesn't response me at all.
Message sequence, what I used:


<connection to the server> //Empty string
USER username
PASS pass
STAT
LIST
RETR
QUIT
<connection to the server> //Here server doesn't response.




In program I use a loop for getting it work.

Where can be a problem?

Thank you for answers beforehand.

ChrisW67
15th September 2011, 22:43
I bet that when you QUIT Google's server closes the connection. Anything you send after that is likely being silently ignored (at your end).

BTW: What does this have to do with Qt? Why don't you ask Google how their system works?

DmitryNik
16th September 2011, 05:20
Thank you!

It could relate to QSslSocket too. And in this case it's more QT than google. For instance, sometimes, after the project rebuilding, program will not start send messages at all. Even the first line "Welcome" will not appear on the screen (qDebug()<<"Welcome";). Sockets are not that new subject for me, but in QT, sometimes, I have some problems=)

DmitryNik
17th September 2011, 11:05
I have to admit, that I don't understand something in "How does qDebug() << message work?". Apparently, at any one time, when we call qDebug() << something; something suppose to appear in the console, isn't it? But in this piece of code it will not work in a right way:


socket->write("LIST\r\n");
QString str;
while(socket->waitForReadyRead())
{
str = socket->readAll().data();
qDebug() << str;
qDebug() << str.length();
}


I will not get the length of the string in the console. I tried to put a break-point. Yes, program will stop on that point, but once again there is no result on the screen for some reason. May be I need to include something in .pro file, besides QT += network. I don't know. Could you please explain me, what's going on behind the scene in qDebug() and why it will not show any results?

Thank you beforehand.

p.s. I consider the worst scenario, when message will be received by chunks. In this case either once the length will be shown or nothing at all.
p.p.s. I solved problem in this way:



socket->write("LIST\r\n");
QByteArray arr;
while(socket->waitForReadyRead())
{
arr.insert(arr.length(), socket->readAll());
qDebug() << arr.data();
qDebug() << "Length: " << arr.length();
if(arr.endsWith(".\r\n"))
break;
}


I got the right result. But why for the QString it doesn't work? I'm a bit confused. In the world of C++
std::cout works perfectly. And we always can grab the right result for the string length... Honestly, I don't get it, how does qt interpret length-function for the QString...

ChrisW67
18th September 2011, 05:44
socket->write("LIST\r\n");
QString str;
while(socket->waitForReadyRead())
{
str = socket->readAll().data();
qDebug() << str;
qDebug() << str.length();
}



You are retrieving a bunch of bytes into a QByteArray using QTcpSocket::readAll(). This may not be any sort of character data (although it should be for POP3) or a complete message. You then assume the content of the array is a C-style string (QByteArray::data()) and assign it to a QString. QString::operator=() runs it through fromAscii() which, depending what is actually in the data and the current codec settings, may do odd things to it. After that QString will report the number of characters in the QString.

On Windows you may need "CONFIG += console" to get console output when a debugger is not present.

DmitryNik
18th September 2011, 08:48
Thank you. Now I understand that. I presume, I can use only QByteArray, without QString.

BTW, I have one question: does qt have the class, which is related to readers-writers problem? Or we still need to write something like that:



while(true)
{
mutex.acqure();
readcount++;
if(readcount == 1) mutex.acqure();
mutex.release();

//critical section

mutex.acqure();
readcount--;
if(readcount == 0) mutex.release();
mutex.release();
}



In C#, for instance, we have ReaderWriterLockSlim class, which reduce amount of code.

ChrisW67
18th September 2011, 23:53
Have a look at QMutex and the See Also list on that page.

DmitryNik
19th September 2011, 16:34
Thank you once again for clarifying.

p.s. yeah, that thread is not only about pop3 now =D