{
// in class ctor...
connect(m_socket, SIGNAL(connected()), this,SLOT(connected()));
connect(m_socket, SIGNAL(disconnected()), this,SLOT(disconnected()));
connect(m_socket, SIGNAL(readyRead()), this,SLOT(receiveMessage()));
}
void XmlSocket::receiveMessage()
{
int bytesRead;
// READ_BUF_SIZE is defined as 512 and the message I receive are 100 chars...
char buf[READ_BUF_SIZE];
// read data from socket
// NB: loop only if we read more than 512 bytes...
do {
bzero(buf,READ_BUF_SIZE);
bytesRead = m_socket->read(buf, READ_BUF_SIZE);
if(bytesRead < 0)
qCritical("Error reading data from socket %d",bytesRead);
else if(bytesRead == 0)
qWarning("No more data on socket...");
qDebug("++ %s", qPrintable(msgString));
} while(bytesRead == READ_BUF_SIZE);
// emit signal with available data
if(!msg.setContent(msgString))
qWarning("Error setting content for string %s", qPrintable(msgString));
emit messageReceived(msg);
}
{
// in class ctor...
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(connected()), this,SLOT(connected()));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(error(QAbstractSocket::SocketError)));
connect(m_socket, SIGNAL(disconnected()), this,SLOT(disconnected()));
connect(m_socket, SIGNAL(readyRead()), this,SLOT(receiveMessage()));
}
void XmlSocket::receiveMessage()
{
int bytesRead;
// READ_BUF_SIZE is defined as 512 and the message I receive are 100 chars...
char buf[READ_BUF_SIZE];
// read data from socket
// NB: loop only if we read more than 512 bytes...
QString msgString;
do {
bzero(buf,READ_BUF_SIZE);
bytesRead = m_socket->read(buf, READ_BUF_SIZE);
if(bytesRead < 0)
qCritical("Error reading data from socket %d",bytesRead);
else if(bytesRead == 0)
qWarning("No more data on socket...");
msgString += QString(buf);
qDebug("++ %s", qPrintable(msgString));
} while(bytesRead == READ_BUF_SIZE);
// emit signal with available data
QDomDocument msg;
if(!msg.setContent(msgString))
qWarning("Error setting content for string %s", qPrintable(msgString));
emit messageReceived(msg);
}
To copy to clipboard, switch view to plain text mode
Bookmarks