Hi everyone,
I have found a strange behavior by QTcpSocket working on it.
The explanation isn't easy, but I'll try it anyway.
The idea was to create a thread in the application that works with a QTcpSocket to, of course, connect to a server.
So, subclass QThread, and there what is needed for the socket.
Now, depending on how and where QTcpSocket is declared, connection is successful or not.
Details:
considering the same code to connect, that is a simple
socket->connectToHost("someserver",someport);
socket->connectToHost("someserver",someport);
To copy to clipboard, switch view to plain text mode
let's change the QTcpSocket declaration.
1) as a local variabile in run() (QThread::run() reimplementation)
socket.connectToHost("server...",port);
QTcpSocket socket;
socket.connectToHost("server...",port);
To copy to clipboard, switch view to plain text mode
and in this way works fine.
2) as a dinamically a allocated pointer in thread class (member pointer)
class mysocketthread
:public QThread{
private:
...
};
mysocketthread::mysocketthread()
{
...
}
void mysocketthread::run()
{
socket->connectToHost("server...",port);
...
}
class mysocketthread:public QThread
{
private:
QTcpSocket *socket;
...
};
mysocketthread::mysocketthread()
{
socket=new QTcpSocket();
...
}
void mysocketthread::run()
{
socket->connectToHost("server...",port);
...
}
To copy to clipboard, switch view to plain text mode
and in this way keeps failing the connection, giving a constant "Connection refused".
Maybe I'm missing something, but I think of this as quite strange... isn't it?
Bookmarks