PDA

View Full Version : QTcpServer connect to selected client



hazel
17th February 2016, 08:37
i have listed every connected client in a listbox and i want to see the incoming data only from the selected client from the listbox but i can only see the last connected client's messages. I searching about that and only saw nextpendingconnection() to connect. How can i select client what is set ?

anda_skoa
17th February 2016, 09:12
How do you store the accepted client connections?
Where do you connect to each connection's readyRead() signal?
Do you use client connection handler objects or do you work with just the QTcpSocket objects?

Cheers,
_

hazel
17th February 2016, 09:43
QTcpServer* server;
QTcpSocket* client;
QList<QTcpSocket*> connections;
QHash<QTcpSocket*, QBuffer*> buffers;

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()));
}

if new connection is coming :


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()));
}

disconnected :


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);
}
}

readyread() :


void rc::readyRead()
{
QByteArray data = client->readAll();
In_Buf.append(data);
ui->text->moveCursor(QTextCursor::End);
ui->text->insertPlainText(In_Buf);
}

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 ?

anda_skoa
17th February 2016, 12:53
Well, you are calling client->readAll(), so you are calling this always on the last received connection, no matter which one actually has new data.

1) Maybe you want to read the data from the connection that actually has new data
2) Maybe you want to put that data into the associated buffer
3) Maybe you want to use the data in the buffer of the selected connection for display

Cheers,
_

hazel
17th February 2016, 13:06
i got it ;-) now i select client from treewidget with its peerport() ;



void MainWindow::selectToClient()
{
foreach( QTreeWidgetItem *item, ui->twClientConnected->selectedItems() ) {

foreach (QTcpSocket* tcp, connections) {
if(item->text(1).toUInt() == tcp->peerPort())
{
client=tcp;
}
}
}
}


and after that client is equal to my selected and call readall() .
It is very simple but i couldnt think like that ...
Thank you so much :o