QTcpServer connect to selected client
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 ?
Re: QTcpServer connect to selected client
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,
_
Re: QTcpServer connect to selected client
Code:
QList<QTcpSocket*> connections;
QHash<QTcpSocket
*,
QBuffer*> buffers;
here i listen the clients:
Code:
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 :
Code:
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()));
}
disconnected :
Code:
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);
}
}
readyread() :
Code:
void rc::readyRead()
{
In_Buf.append(data);
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 ?
Re: QTcpServer connect to selected client
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,
_
Re: QTcpServer connect to selected client
i got it ;-) now i select client from treewidget with its peerport() ;
Code:
void MainWindow::selectToClient()
{
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