Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, April 2007
blocking socket error detection problem


Message 1 in thread

Hi,

I'm trying to use a QTcpSocket in a thread *without* an event loop - so
far everything's been great - I use waitForReadyRead() and
waitForConnected() - whioch allow me to block until the desired event
takes place.

I'm trying to detect if the socket connects properly. I'm connecting
like this:

sock.connectToHost(
	cf->getValue("ServerAddress"), 
	cf->getValue("ServerPort").toInt()
	);

(cf) is my own class - basically the IP address and port could be
anything - they're set by the user. I'd like to check whether the socket
has connected properly. So, I do this:

// block until socket is in the connected state:
if (sock.state() != QTcpSocket::ConnectedState)
{
	// timeout after 30 seconds:
	if (! sock.waitForConnected(30000))
	{
		// error handling here...
	}
}


The problem seems to be that QTcpSocket sets the default state to
ConnectedState, and relies on the user catching the error() signal,
which I can't do since I have no event loop. Even worse, there's no
QAbstractSocket::SocketError entry that indicates the absence of an
error, so I can't check the error manually.


Am I missing something here? How can I check that the socket connected
properly without using an event loop?


Thanks,

-- 
 [ signature omitted ] 

Message 2 in thread

Hi,

> The problem seems to be that QTcpSocket sets the default state to
> ConnectedState, and relies on the user catching the error() signal,
> which I can't do since I have no event loop. Even worse, there's no
> QAbstractSocket::SocketError entry that indicates the absence of an
> error, so I can't check the error manually.

What's the exact problem exactly? What do you see and what do you expect?

The initial state of QTcpSocket does not seem to be ConnectedState:
	#include <QApplication>
	#include <QTcpSocket>
	int main(int argc, char *argv[]) {
	    QApplication app(argc, argv);
	    const char host[] = "yyy.trolltech.zzz";
	    int port = 80;
	    QTcpSocket socket;
	    socket.connectToHost(host, port);
	    if (socket.state() != QTcpSocket::ConnectedState) {
	        qDebug() << host << "initially not connected";
	}

--
 [ signature omitted ]