I am still very new to C++/Qt programming. I was hoping that someone could shed some light on something for me:
void Thread::run()
{
//initalize socket, start connection and detect data available
if (socket->setSocketDescriptor(sock))
{
out << (quint16)0;
out << welcomeMsg;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
socket->write(block);
}
{
if (socket->bytesAvailable() < 1)
{
qDebug("Waiting for data...");
if (!socket->waitForReadyRead())
break;
}
// while (socket->bytesAvailable() > 0)
// {
if (socket->bytesAvailable() > 0)
{
qDebug("Data available");
if (incomingBlock.contains("xml") && !incomingBlock.isEmpty())
{
dataHandler = new xmlDataHandler;
dataHandler->doParse( &incomingBlock );
}
/*
char c;
socket->getChar(&c);
printf("%c", c);
*/
}
// }
}
qDebug("Connection closed");
}
void Thread::run()
{
//initalize socket, start connection and detect data available
socket = new QTcpSocket;
if (socket->setSocketDescriptor(sock))
{
QString welcomeMsg = QString("Welcome to the middleMan\n\n");
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << welcomeMsg;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
socket->write(block);
}
while (socket->state() == QAbstractSocket::ConnectedState)
{
if (socket->bytesAvailable() < 1)
{
qDebug("Waiting for data...");
if (!socket->waitForReadyRead())
break;
}
// while (socket->bytesAvailable() > 0)
// {
if (socket->bytesAvailable() > 0)
{
qDebug("Data available");
QByteArray incomingBlock = socket->readAll();
if (incomingBlock.contains("xml") && !incomingBlock.isEmpty())
{
dataHandler = new xmlDataHandler;
dataHandler->doParse( &incomingBlock );
}
/*
char c;
socket->getChar(&c);
printf("%c", c);
*/
}
// }
}
qDebug("Connection closed");
}
To copy to clipboard, switch view to plain text mode
To explain a little bit. This is the run code for a thread that is created and started each time a new connection is requested. I am trying to stream short bits of dynamically created xml like data to the server from a client. I basically want to know when the end of a stream has been reached. How do you figure out the data that is coming across the socket. I assume the beginning of the stream is the header information. Then the data is passed. How do I separate those and only save the data and close the connection after the data is finished streaming? Thanks in advance. Even pointing to a good sockets tutorial would suffice.
Bookmarks