fantom
25th January 2012, 15:58
Dear all,
I implemented a simple chat server in order to receive a msg from a device. The msg is a string and i would like to tokenize it in order to put the pieces of it to my sql database. Could somebody send me a code example how this can be done?
Thank you in advance!
#include "simplechatserver.h"
#include <QBuffer>
#include <QByteArray>
#include <QString>
#include <QTcpSocket>
#include <QStringList>
#include <iostream>
SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
{
std::cout << "Message server started" << std::endl;
connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}
SimpleChatServer::~SimpleChatServer()
{
}
void SimpleChatServer::addConnection()
{
std::cout <<"new incomming connection" << std::endl;
QTcpSocket* connection = nextPendingConnection();
connections.append(connection);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(connection, buffer);
connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
}
void SimpleChatServer::removeConnection()
{
std::cout << "removing connection "<< std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}
void SimpleChatServer::receiveMessage()
{
std::cout << "receiving" <<std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.value(socket);
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// while(buffer->canReadLine())
{
line = buffer->readLine();
QString str(line.data());
std::cout << str.toStdString() << std::endl;
QStringList list[] = {str.split(QRegExp(" "), QString::SkipEmptyParts)}; ????????????
//std::cout<< list.<<std::endl; ???????????
emit emitMessage(str);
}
}
I implemented a simple chat server in order to receive a msg from a device. The msg is a string and i would like to tokenize it in order to put the pieces of it to my sql database. Could somebody send me a code example how this can be done?
Thank you in advance!
#include "simplechatserver.h"
#include <QBuffer>
#include <QByteArray>
#include <QString>
#include <QTcpSocket>
#include <QStringList>
#include <iostream>
SimpleChatServer::SimpleChatServer(QObject* parent) : QTcpServer(parent)
{
std::cout << "Message server started" << std::endl;
connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}
SimpleChatServer::~SimpleChatServer()
{
}
void SimpleChatServer::addConnection()
{
std::cout <<"new incomming connection" << std::endl;
QTcpSocket* connection = nextPendingConnection();
connections.append(connection);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(connection, buffer);
connect(connection, SIGNAL(disconnected()), SLOT(removeConnection()));
connect(connection, SIGNAL(readyRead()), SLOT(receiveMessage()));
}
void SimpleChatServer::removeConnection()
{
std::cout << "removing connection "<< std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}
void SimpleChatServer::receiveMessage()
{
std::cout << "receiving" <<std::endl;
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.value(socket);
// missing some checks for returns values for the sake of simplicity
qint64 bytes = buffer->write(socket->readAll());
// go back as many bytes as we just wrote so that it can be read
buffer->seek(buffer->pos() - bytes);
// while(buffer->canReadLine())
{
line = buffer->readLine();
QString str(line.data());
std::cout << str.toStdString() << std::endl;
QStringList list[] = {str.split(QRegExp(" "), QString::SkipEmptyParts)}; ????????????
//std::cout<< list.<<std::endl; ???????????
emit emitMessage(str);
}
}