Unlike the typical client/server situation, my chat application is both a client and a server. It is a server because it runs an instance of qtcpserver and it is a client, because you can make connections using qtcpsocket.
To have multiple connections i assumed you need multithreading although I have no experience with multithreading+ I'm new to QT.
Here is how I'm implementing the ability to establish a connection and then pass off that connection to its own thread.
// background info
MSocket tcpSocket;
connect(&tcpSocket, SIGNAL(connected()),
this, SLOT(connectionEstablished()));
tcpSocket.connectToHost(remoteIP, remotePort);
// end background.
void MChat::connectionEstablished() {
chatOutput->append("<font color=green>Connection Established.</font>");
MSocketThread* mSockThread = new MSocketThread(tcpSocket.socketDescriptor());
connect(this, SIGNAL(sendText(QString&)),
mSockThread, SIGNAL(sigSend(QString&)));
connect(mSockThread, SIGNAL(relayIncomingText(QString&)),
this, SLOT(outputText(QString&)));
connect(mSockThread, SIGNAL(socketError()),
this, SLOT(error()));
connect(mSockThread, SIGNAL(finished()),
mSockThread, SLOT(deleteLater()));
mSockThread->start();
}
// background info
MSocket tcpSocket;
connect(&tcpSocket, SIGNAL(connected()),
this, SLOT(connectionEstablished()));
tcpSocket.connectToHost(remoteIP, remotePort);
// end background.
void MChat::connectionEstablished() {
chatOutput->append("<font color=green>Connection Established.</font>");
MSocketThread* mSockThread = new MSocketThread(tcpSocket.socketDescriptor());
connect(this, SIGNAL(sendText(QString&)),
mSockThread, SIGNAL(sigSend(QString&)));
connect(mSockThread, SIGNAL(relayIncomingText(QString&)),
this, SLOT(outputText(QString&)));
connect(mSockThread, SIGNAL(socketError()),
this, SLOT(error()));
connect(mSockThread, SIGNAL(finished()),
mSockThread, SLOT(deleteLater()));
mSockThread->start();
}
To copy to clipboard, switch view to plain text mode
Here's how I'm implementing the MSocketThread
void MSocketThread::run() {
MSocket mSocket;
if ( !mSocket.setSocketDescriptor(socketDescriptor) ) {
emit error(mSocket.error());
return;
}
connect(&mSocket, SIGNAL(inboundText(QString&)),
this, SIGNAL(relayIncomingText(QString&)));
connect(this, SIGNAL(sigSend(QString&)),
&mSocket, SLOT(send(QString&)));
/*
connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SIGNAL(socketError()));
*/
mSocket.waitForDisconnected();
}
void MSocketThread::run() {
MSocket mSocket;
if ( !mSocket.setSocketDescriptor(socketDescriptor) ) {
emit error(mSocket.error());
return;
}
connect(&mSocket, SIGNAL(inboundText(QString&)),
this, SIGNAL(relayIncomingText(QString&)));
connect(this, SIGNAL(sigSend(QString&)),
&mSocket, SLOT(send(QString&)));
/*
connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SIGNAL(socketError()));
*/
mSocket.waitForDisconnected();
}
To copy to clipboard, switch view to plain text mode
Finally, here is the problem I'm encountering. When I try to send some chat, the console window displays this message:
QObject::connect: Cannot queue arguments of type 'QString&'
(Make sure 'QString&' is registed using qRegisterMetaType().)
And no chat gets displayed on the remote chat app. output.
Any tips, solutions, etc. are welcome as I am very new.
Bookmarks