Raccoon29
13th January 2009, 09:13
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);
let's change the QTcpSocket declaration.
1) as a local variabile in run() (QThread::run() reimplementation)
QTcpSocket socket;
socket.connectToHost("server...",port);
and in this way works fine.
2) as a dinamically a allocated pointer in thread class (member pointer)
class mysocketthread:public QThread
{
private:
QTcpSocket *socket;
...
};
mysocketthread::mysocketthread()
{
socket=new QTcpSocket();
...
}
void mysocketthread::run()
{
socket->connectToHost("server...",port);
...
}
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?
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);
let's change the QTcpSocket declaration.
1) as a local variabile in run() (QThread::run() reimplementation)
QTcpSocket socket;
socket.connectToHost("server...",port);
and in this way works fine.
2) as a dinamically a allocated pointer in thread class (member pointer)
class mysocketthread:public QThread
{
private:
QTcpSocket *socket;
...
};
mysocketthread::mysocketthread()
{
socket=new QTcpSocket();
...
}
void mysocketthread::run()
{
socket->connectToHost("server...",port);
...
}
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?