QList<QTcpSocket*> connections;
QHash<QTcpSocket
*,
QBuffer*> buffers;
QTcpServer* server;
QTcpSocket* client;
QList<QTcpSocket*> connections;
QHash<QTcpSocket*, QBuffer*> buffers;
To copy to clipboard, switch view to plain text mode
here i listen the clients:
void MainWindow::slotStartClicked()
{
showStatusMessage(tr("Server starting"));
if (!server
->listen
(QHostAddress::Any, ui
->port
->text
().
toInt())) { QMessageBox::critical(this, tr
("Server"), tr
("Unable to start the server: %1.").
arg(reco
->server
->errorString
()));
return;
}
showStatusMessage(tr("Server started at port %1").arg(server->serverPort()));
}
void MainWindow::slotStartClicked()
{
showStatusMessage(tr("Server starting"));
if (!server->listen(QHostAddress::Any, ui->port->text().toInt())) {
QMessageBox::critical(this, tr("Server"), tr("Unable to start the server: %1.").arg(reco->server->errorString()));
return;
}
showStatusMessage(tr("Server started at port %1").arg(server->serverPort()));
}
To copy to clipboard, switch view to plain text mode
if new connection is coming :
connect(server , SIGNAL(newConnection()),this, SLOT(newConnection()));
void MainWindow::newConnection()
{
if (!connections.isEmpty())
{
{
connection->write(msg);
}
}
client= server->nextPendingConnection();
connections.append(client);
buffers.insert(client, buffer);
connect(client, SIGNAL(disconnected()), this, SLOT(slotClientDisconnected()));
connect(client, SIGNAL(readyRead()) , this, SLOT(readyReadR()));
}
connect(server , SIGNAL(newConnection()),this, SLOT(newConnection()));
void MainWindow::newConnection()
{
if (!connections.isEmpty())
{
QByteArray msg = "New Client connected\n";
foreach (QTcpSocket* connection, connections)
{
connection->write(msg);
}
}
client= server->nextPendingConnection();
QStringList tmpClientAddressPort;
connections.append(client);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(client, buffer);
connect(client, SIGNAL(disconnected()), this, SLOT(slotClientDisconnected()));
connect(client, SIGNAL(readyRead()) , this, SLOT(readyReadR()));
}
To copy to clipboard, switch view to plain text mode
disconnected :
void MainWindow::slotClientDisconnected()
{
QTcpSocket* socket
= static_cast<QTcpSocket
*>
(sender
());
QBuffer* buffer
= buffers.
take(socket
);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
{
connection->write(msg);
}
}
void MainWindow::slotClientDisconnected()
{
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.take(socket);
buffer->close();
buffer->deleteLater();
connections.removeAll(socket);
socket->deleteLater();
QByteArray msg = "Client disconnected\n";
foreach (QTcpSocket* connection, connections)
{
connection->write(msg);
}
}
To copy to clipboard, switch view to plain text mode
readyread() :
void rc::readyRead()
{
In_Buf.append(data);
ui->text->insertPlainText(In_Buf);
}
void rc::readyRead()
{
QByteArray data = client->readAll();
In_Buf.append(data);
ui->text->moveCursor(QTextCursor::End);
ui->text->insertPlainText(In_Buf);
}
To copy to clipboard, switch view to plain text mode
Also i have tree widget and i seperate all clients according to client->peerport() ... i can choose client but than what can i do i dont know ?
Bookmarks