Hello, i got stuck while trying writing to a threaded server. Receiving works fine but the other way around...
Method readData() at tcpsocket.cpp is never called. What am I doing wrong?
Server:
MultiServer server;
close();
return;
}
MultiServer server;
if (!server.listen(QHostAddress::Any, 15000)) {
close();
return;
}
To copy to clipboard, switch view to plain text mode
multiserver.cpp
MultiServer
::MultiServer(QObject *parent
){
}
void MultiServer::incomingConnection(int socketDescriptor)
{
QString msg
= "Message from server";
ConnectionThread *thread = new ConnectionThread(socketDescriptor, msg, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
MultiServer::MultiServer(QObject *parent)
: QTcpServer(parent)
{
}
void MultiServer::incomingConnection(int socketDescriptor)
{
QString msg = "Message from server";
ConnectionThread *thread = new ConnectionThread(socketDescriptor, msg, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
}
To copy to clipboard, switch view to plain text mode
connectionthread.cpp
ConnectionThread
::ConnectionThread(int socketDescriptor,
const QString &msg,
QObject *parent
), socketDescriptor(socketDescriptor)
, text(msg)
{
}
void ConnectionThread::run()
{
my_sock = new CTcpSocket(socketDescriptor);
my_sock->writeData(text);
}
ConnectionThread::ConnectionThread(int socketDescriptor, const QString &msg, QObject *parent)
: QThread(parent)
, socketDescriptor(socketDescriptor)
, text(msg)
{
}
void ConnectionThread::run()
{
my_sock = new CTcpSocket(socketDescriptor);
my_sock->writeData(text);
}
To copy to clipboard, switch view to plain text mode
tcpsocket.cpp
CTcpSocket
::CTcpSocket(int socketDescriptor,
QObject *parent
), m_socketDescriptor(socketDescriptor)
{
if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor)) {
qDebug() << m_tcpSocket->error();
return;
}
[COLOR="Red"]connect(m_tcpSocket, SIGNAL(readyRead()), SLOT(readData()));[/COLOR]
}
[COLOR="Red"]void CTcpSocket::readData()
{
//ToDo Implement reading Data
qDebug() << "try to read";
//m_tcpSocket->waitForReadyRead(10000);
}[/COLOR]
void CTcpSocket
::writeData(QString txt
) {
qDebug() << "try to write";
out << (quint16)0;
out << txt;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
m_tcpSocket->write(block);
m_tcpSocket->waitForBytesWritten(10000);
m_tcpSocket->disconnectFromHost();
m_tcpSocket->waitForDisconnected();
}
CTcpSocket::CTcpSocket(int socketDescriptor, QObject *parent)
: QObject(parent)
, m_socketDescriptor(socketDescriptor)
{
m_tcpSocket = new QTcpSocket();
if (!m_tcpSocket->setSocketDescriptor(m_socketDescriptor)) {
qDebug() << m_tcpSocket->error();
return;
}
[COLOR="Red"]connect(m_tcpSocket, SIGNAL(readyRead()), SLOT(readData()));[/COLOR]
}
[COLOR="Red"]void CTcpSocket::readData()
{
//ToDo Implement reading Data
qDebug() << "try to read";
//m_tcpSocket->waitForReadyRead(10000);
}[/COLOR]
void CTcpSocket::writeData(QString txt)
{
qDebug() << "try to write";
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << txt;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
m_tcpSocket->write(block);
m_tcpSocket->waitForBytesWritten(10000);
m_tcpSocket->disconnectFromHost();
m_tcpSocket->waitForDisconnected();
}
To copy to clipboard, switch view to plain text mode
Bookmarks