QTcpSocket connection problem
Hello everyone. I'm trying to write a chat using QTcpSocket and QTcpServer, but strange things are happening. In the first example QTcpServer emits newConnection(), but QTcpSocket remains in connecting state without emiting error(...). In the second example I receive SIGSEGV when trying connectToHost() . What is more, FortuneClient/Server (single threaded) example runs just fine. In both examples network sessiong is not required. Being discouraged I examined the Fortune code and discovored that it is just the same concerning establishing connection. Here are both examples.
FIRST
Code:
//CLIENT
ChatClient
::ChatClient(QObject *parent
) QNetworkConfigurationManager manager;
if (QNetworkConfigurationManager::NetworkSessionRequired
& manager.capabilities()) {
qDebug() << "Network session required";
}
connect(tcpSocket, SIGNAL(connected()),
this, SLOT(requestForID()));
connect(tcpSocket, SIGNAL(readyRead()),
this, SLOT(receiveMessage()));
tcpSocket->connectToHost("192.168.0.100", PORT);
}
...
qDebug() << "Socket error" << error;
}
void ChatClient::requestForID() {
qDebug() << "Connected, requesting for ID";
out << (quint16)0 << ID;
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
tcpSocket->write(segment);
}
//SERVER
ChatServer
::ChatServer(QObject *parent
) if (!tcpServer->listen(/*QHostAddress::Any, PORT*/)) {
qDebug() << "Unable to start the server"
<< tcpServer->errorString();
}
qDebug() << "Server port" << tcpServer->serverPort();
connect(tcpServer, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
void ChatServer::processConnection() {
qDebug() << "Incoming connection";
QTcpSocket *clientSocket
= tcpServer
->nextPendingConnection
();
/*connect(clientSocket, SIGNAL(readyRead()),
this, SLOT(readData()));
readData(clientSocket);
connect(clientSocket, SIGNAL(disconnected()),
clientSocket, SLOT(deleteLater()));*/
out << (quint16)0 << (quint16)Message
<< "Successfully connected";
out.device()->seek(0);
out << (quint16)(segment.size() - sizeof(quint16));
clientSocket->write(segment);
clientSocket->disconnectFromHost();
}
SECOND
Code:
//CLIENT
Client::Client() {
connect(clientSocket, SIGNAL(connected()),
this, SLOT(connectionEstablished()));
this, SLOT(error()));
}
void Client::establishConnection() {
// throws SIGSEGV
clientSocket
->connectToHost
(QHostAddress("192.168.0.100"),
6178);
}
void Client::connectionEstablished() {
qDebug() << "Connection established";
}
void Client::error() {
qDebug() << clientSocket->errorString();
}
//SERVER
Server::Server() {
QNetworkConfigurationManager manager;
if (manager.capabilities() &
QNetworkConfigurationManager::NetworkSessionRequired) {
qDebug() << "Network session required";
} else {
sessionOpened();
}
connect(server, SIGNAL(newConnection()),
this, SLOT(processConnection()));
}
void Server::processConnection() {
qDebug() << "Incoming connection";
}
void Server::sessionOpened() {
qDebug
() <<
QString("Unable to start the server: %1") .arg(server->errorString());
return;
}
qDebug() << "The server is running...";
}
Any ideas?
Re: QTcpSocket connection problem
Use QThread and Mutex. I solved with them. I want to send it but my code is C based.
Re: QTcpSocket connection problem
Please explain why I should use QMutex and QThread in those examples? Should I implement tcp connection in a seperate thread?
Re: QTcpSocket connection problem
Oh.. its just my opinion :)
No not. Just inherit your class from QThread like this
Code:
{
Q_OBJECT
public:
explicit logthread
(QObject *parent
= 0);
~logthread();
..........
and use mutex only when your application sends data
Code:
...........
.........
.........
mutex.lock();
send_function();
mutex.unlock();
Its a very basic example but my code is running clearly. For more information just read the docs.
Re: QTcpSocket connection problem
Thanks I'll try right now. Should I lock tcpSocket->connectToHost()? But why FortuneClient/Server doesn't use mutex and doesn't inherit QThread and works fine?
Re: QTcpSocket connection problem
There is no reason to use threads and mutexes here.
Re: QTcpSocket connection problem
Yes that hasn't solved the problem. But still I'm confused because Fortune example works and my doesn't. At the same time I do not find differencies except that Fortune iterates over network interfaces to choose one and I choose manually. Even then both IP addresses are the same. May be I should post the full code? It is not long.
Re: QTcpSocket connection problem
Post the debugger backtrace after a crash.
Re: QTcpSocket connection problem
//
int idx = obj->metaObject()->indexOfMethod(sig.constData());
//
return invokeMethod(obj, member, type, QGenericReturnArgument(), val0, val1, val2, val3, val4, val5, val6, val7, val8, val9);
//
QMetaObject::invokeMethod(this, "connectToHostImplementation",
Qt:: DirectConnection,
Q_ARG(QString, hostName),
Q_ARG(quint16, port),
Q_ARG(OpenMode, openMode));
//
connectToHost(address.toString(), port, openMode);
//
clientSocket->connectToHost(QHostAddress("192.168.0.100"), 6178);
please note that I talk about two different projets, where in the first one QTcpSocket remains in connecting state while in the second QTcpSocket::connectToHost() produces SIGSEGV.
Re: QTcpSocket connection problem
Please post the backtrace as requested.
Re: QTcpSocket connection problem
Sorry I misunderstand what is backtrace?
Re: QTcpSocket connection problem
http://en.wikipedia.org/wiki/Call_stack
If you are using Qt Creator, the backtrace is in the "stack" view when you're in the Debug mode.
Re: QTcpSocket connection problem
gdb
Code:
#0 0x00402b73 in QMetaObject::invokeMethod (obj=0x7e9ff4,
member=0x22a33f "connectToHostImplementation", type=Qt::DirectConnection,
ret=..., val0=..., val1=..., val2=..., val3=..., val4=..., val5=..., val6=...,
val7=..., val8=..., val9=...)
at /var/tmp/qt-src/src/corelib/kernel/qmetaobject.cpp:1135
#1 0x001efe73 in invokeMethod (this=0x7e9ff4, hostName=..., port=6178, openMode=...)
at ../../include/QtCore/../../src/corelib/kernel/qobjectdefs.h:408
#2 QAbstractSocket::connectToHost (this=0x7e9ff4, hostName=..., port=6178,
openMode=...) at /var/tmp/qt-src/src/network/socket/qabstractsocket.cpp:1304
#3 0x001f1835 in QAbstractSocket::connectToHost (this=0x7e9ff4, address=...,
port=6178, openMode=...)
at /var/tmp/qt-src/src/network/socket/qabstractsocket.cpp:1417
#4 0x080494e4 in Client::establishConnection (this=0xbffff1fc)
at ../NetTestClient/client.cpp:13
#5 0x08049201 in main (argc=1, argv=0xbffff2d4) at ../NetTestClient/main.cpp:8
qt creator
0 QHashData::detach_helper2 qhash.cpp 206 0x001a18fa
1 QHash<QObject*, QHash<QEvent::Type, int> >::detach_helper qhash.h 582 0x002e69eb
2 detach qhash.h 299 0x002dbc23
3 operator[] qhash.h 737 0x002dbc23
4 QStateMachinePrivate::registerEventTransition qstatemachine.cpp 1598 0x002dbc23
5 invokeMethod qobjectdefs.h 408 0x00a25e73
6 QAbstractSocket::connectToHost qabstractsocket.cpp 1304 0x00a25e73
7 QAbstractSocket::connectToHost qabstractsocket.cpp 1417 0x00a27835
8 Client::establishConnection client.cpp 13 0x080494e4
9 main main.cpp 8 0x08049201
:D
Re: QTcpSocket connection problem
What does the main() function look like?
Re: QTcpSocket connection problem
Code:
int main(int argc, char *argv[])
{
Client client;
client.establishConnection();
return a.exec();
}
Re: QTcpSocket connection problem
Ah.... I missed it. You are creating a local variable called "clientSocket" in your constructor that shadows the class member called "clientSocket" thus it remains uninitialized and a dangling pointer causes a crash.
Re: QTcpSocket connection problem
Oh my god =))
Thanks a lot man!
But what about the first example? Everything seems to be correct there, but the socket stays in connecting state all the time while the server emits newConnection()?
Re: QTcpSocket connection problem
I don't know, I'd need to see code that's more complete than this.