PDA

View Full Version : QT4.2.2 and QTcpSocket



Nyphel
21st February 2007, 15:28
Hi :),

I'm using QT4.2.2 and I can't use the QTcpSockets (QtNetwork) :mad:.
What about you ?

Here is my code :

#include <QtGui>
#include <QObject>
#include <QTcpSocket>
#include <QtNetwork/QTcpSocket>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QTcpSocket socket = new QTcpSocket();

socket->connectToHost("imap.free.fr", 143);

if ( !(socket->waitForConnected(-1)) )
{
printf("Unable to connect To Imap Server!\n");
return 0;
}
else printf("Connection OK\n");

return app.exec();
}

For me, this code produce the following error :

error: base operand of '->' has non-pointer type 'QTcpSocket'

However, I would like to write a small application that will check new mails from an Intranet IMAP server. I wanted to use the libvmime library, but it's difficult to use on Windows 2000... So I need to make my own class to check the mails on the server (no need to display content, no need to send mails, ...).

Do you know if I can produce a socket oriented solution ?
So you know if I can implement a securised connection, like the IMAPs is ? (IMAP on SSL)

Thanks :)

camel
21st February 2007, 15:39
QTcpSocket socket = new QTcpSocket();



Try


QTcpSocket *socket = new QTcpSocket();


;-)



Do you know if I can produce a socket oriented solution ?

Sure, why shouldn't you? You might just have to reimplement the protocol...and that is probably not much fun.

There is, if I remember correctly, a project currently going on in the kde project to create a small imap-email client....you might want to check how they do it...



So you know if I can implement a securised connection, like the IMAPs is ? (IMAP on SSL)


You would need an SSL socket for that. You can either write one yourself, using for example gnutls (http://www.gnu.org/software/gnutls/), or, in case you can affort it, there is the QtSslSocket (http://doc.trolltech.com/solutions/4/qtsslsocket/qtsslsocket.html) solution for Qt commercial customers.

Nyphel
21st February 2007, 15:49
Ok, thanks a lot for your detailed answer !

But, what do you mean by "there is the QtSslSocket solution for Qt commercial customers" ?
The QtSslSocket can be used only with a commercial licence of Qt ?

I know that I'm not able to create my own SSL Socket :o...
And I can't buy a commercial licence of Qt.

camel
21st February 2007, 16:27
But, what do you mean by "there is the QtSslSocket solution for Qt commercial customers" ?
The QtSslSocket can be used only with a commercial licence of Qt ?

The Qt Solutions are addons made by Trolltech, some are available only to license holders. See this page (http://www.trolltech.com/products/qt/addon/solutions).

The QtSslSocket (http://www.trolltech.com/products/qt/addon/solutions/catalog/4/Utilities/qtsslsocket/) is (sadly) one of them. There is probably a reason behind it, but I do not know.



I know that I'm not able to create my own SSL Socket :o...
And I can't buy a commercial licence of Qt.

Well, I guess the best would be to first get the security-less version on the road, and worry about the ssl thing later... ;-)

Nyphel
22nd February 2007, 08:54
Hello,

I had several problems using the QTcpSocket, and I can't find examples of code that use the QTcpSocket in order to access a server with IMAP.

Do you have a sample code, please ?
Or, perhaps, do you know where to find more informations ?

Thanks :)



PS : The following code throws me stranges errors :
- "undefined reference to _imp___ZN10QTcpSocketC1EP7QObject"
- "undefined reference to _imp___ZN15QAbstractSocket13connectToHostERK7QStri ng6QFlagsIN9QIODevice120OpenModeFlagEE"

#include <QtGui>
#include <QObject>
#include <QtNetwork/QTcpSocket>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTcpSocket * socket = new QTcpSocket();
socket->connectToHost("imap.free.fr", 143);

return app.exec();
}

camel
22nd February 2007, 09:08
Hello,

I had several problems using the QTcpSocket, and I can't find examples of code that use the QTcpSocket in order to access a server with IMAP.
Do you have a sample code, please ?

I have non for imap, but there are a lot if QTcpSocket examples in the Trolltech documentation (http://doc.trolltech.com/4.2/examples.html#network-examples).

Or just google a bit. I found for example mailody (http://www.mailody.net/), an KDE-Imap client.
Or search via Google Codesearch (http://www.google.com/codesearch).
A simple search (http://www.google.com/codesearch?q=QTcpSocket+Imap&hl=en&btnG=Search+Code) produced for example this file (http://www.google.com/codesearch?hl=en&q=+QTcpSocket+Imap+show:Or0U-HQfNXg:ZuXgOpYJ4bQ:_RDZXaRHGSg&sa=N&cd=8&ct=rc&cs_p=http://download.free-go.net/Freego-08juin.tgz&cs_f=Freego-08juin/src/MailChecker.cpp#a0) which seems to be an inbox checker for imap written in Qt.

Just remember, if you let yourself inspire by other code...honor the license and do not copy code...unless allowed by the license of course :-)


Or, perhaps, do you know where to find more informations ?

On what? QTcpSocket? Best use the Trolltech docs ;-)
IMAP? Check the RFC (http://www.ietf.org/rfc/rfc2060.txt)?



PS : The following code throws me stranges errors :
- "undefined reference to _imp___ZN10QTcpSocketC1EP7QObject"


Do you use qmake?
Add "QT += network" to you pro file. It looks as if the network lib is not linked to your app.

Nyphel
22nd February 2007, 09:23
Thanks a lot !

The "QT += network" doesn't make anything better :(
Perhaps my Qt installation is bad... I'll try to see it.

Google Code Search is too usefull !
It's the first time I hear baout it, thanks !

Sorry if I let think that I didn't search a lot, but my QtAssistant doesn't display me the file and I didn't know Google Code Search... So it was a bit difficult ;)

camel
22nd February 2007, 09:32
Google Code Search is too usefull !
It's the first time I hear baout it, thanks !

That's why I mentioned it...how does this old proverb go...

Give a man a fish, and he will eat for a day,
Teach a man to fish, and he will eat for a lifetime.
:D


Regarding your linking problem, I sadly have no idea what it could be...probably someone else does though :-)

maxpower
23rd February 2007, 16:09
I was able to compile the following code:


#include <QTcpSocket>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTcpSocket * socket = new QTcpSocket();
socket->connectToHost("imap.free.fr", 143);
return app.exec();
}


With this pro file:


################################################## ####################
# Automatically generated by qmake (2.01a) Fri Feb 23 09:05:34 2007
################################################## ####################

QT += network
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += main.cpp


mAx

Nyphel
5th March 2007, 08:40
Thanks, the error was that my "qpake -project" doesn't add "QT += network" to my .pro file.

I don't know how to solve that problem, but now I know that I can use the Sockets :)

camel
5th March 2007, 09:02
Thanks, the error was that my "qpake -project" doesn't add "QT += network" to my .pro file.

I don't know how to solve that problem, but now I know that I can use the Sockets :)

Well, you only use qmake -project once (at least that is what it is intendet for from what I gather), and from then on you simply modify the ".pro" file by hand.

The project file that qmake return is just a very rudimentary thing, and the .pro syntax is not too hard :-)

Burgpflanze
10th March 2007, 01:11
From command-line:
qmake -project "QT = core gui network"

camel
10th March 2007, 07:40
I know that I'm not able to create my own SSL Socket :o...
And I can't buy a commercial licence of Qt.

Just a small update:
You are in luck: Check out the snapshots of Qt 4.3:
There you will find a QSslSocket (http://doc.trolltech.com/snapshot/qsslsocket.html) ;-)

For more info check this blog entry (http://labs.trolltech.com/blogs/2007/02/28/ssl-proxies-and-md5-sums/)

Nyphel
11th March 2007, 11:30
Nice !

Thanks for the tip Camel :)