mraittin
3rd September 2007, 15:44
How can i get reading the message from client,Sorry My English
I was using this example http://wiki.qtcentre.org/index.php?title=Simple_Chat
Server.cpp
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("Fortune Server"));
}
void Server::sendFortune()
{
QTcpSocket *tcpSocket = tcpServer->nextPendingConnection();
connections.append(tcpSocket);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(tcpSocket, buffer);
QDataStream out;
out.setDevice(tcpSocket);
QString tiedosto=haeTiedosto(kuvaIndex);
QFile file(tiedosto);
file.open(QIODevice::ReadOnly);
QDataStream fstr;
fstr.setDevice(&file);
file.seek(0);
char* data = new char[65536];
while(!file.atEnd())
{
int read = fstr.readRawData(data, 65536);
int sent = out.writeRawData(data, read);
}
delete[] data;
tcpSocket->waitForBytesWritten();
connect(tcpSocket, SIGNAL(readyRead()), SLOT(receiveMessage()));
connect(tcpSocket, SIGNAL(disconnected()), SLOT(removeConnection()));
}
void Server::removeConnection()
{
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}
void Server::receiveMessage()
{
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);
// read only full lines, line by line
while (buffer->canReadLine())
{
QByteArray line = buffer->readLine();
foreach (QTcpSocket* tcpSocket, connections)
{
int d=line.toInt();
QString data=data.setNum(d);
statusLabel->setText(data);
}
}
I was using this example http://wiki.qtcentre.org/index.php?title=Simple_Chat
Server.cpp
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("Fortune Server"));
}
void Server::sendFortune()
{
QTcpSocket *tcpSocket = tcpServer->nextPendingConnection();
connections.append(tcpSocket);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(tcpSocket, buffer);
QDataStream out;
out.setDevice(tcpSocket);
QString tiedosto=haeTiedosto(kuvaIndex);
QFile file(tiedosto);
file.open(QIODevice::ReadOnly);
QDataStream fstr;
fstr.setDevice(&file);
file.seek(0);
char* data = new char[65536];
while(!file.atEnd())
{
int read = fstr.readRawData(data, 65536);
int sent = out.writeRawData(data, read);
}
delete[] data;
tcpSocket->waitForBytesWritten();
connect(tcpSocket, SIGNAL(readyRead()), SLOT(receiveMessage()));
connect(tcpSocket, SIGNAL(disconnected()), SLOT(removeConnection()));
}
void Server::removeConnection()
{
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
}
void Server::receiveMessage()
{
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);
// read only full lines, line by line
while (buffer->canReadLine())
{
QByteArray line = buffer->readLine();
foreach (QTcpSocket* tcpSocket, connections)
{
int d=line.toInt();
QString data=data.setNum(d);
statusLabel->setText(data);
}
}