Results 1 to 5 of 5

Thread: QTcpServer connect to selected client

  1. #1
    Join Date
    Dec 2015
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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 ?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Dec 2015
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer connect to selected client

    Qt Code:
    1. QTcpServer* server;
    2. QTcpSocket* client;
    3. QList<QTcpSocket*> connections;
    4. QHash<QTcpSocket*, QBuffer*> buffers;
    To copy to clipboard, switch view to plain text mode 
    here i listen the clients:
    Qt Code:
    1. void MainWindow::slotStartClicked()
    2. {
    3. showStatusMessage(tr("Server starting"));
    4.  
    5. if (!server->listen(QHostAddress::Any, ui->port->text().toInt())) {
    6. QMessageBox::critical(this, tr("Server"), tr("Unable to start the server: %1.").arg(reco->server->errorString()));
    7. return;
    8. }
    9.  
    10. showStatusMessage(tr("Server started at port %1").arg(server->serverPort()));
    11. }
    To copy to clipboard, switch view to plain text mode 
    if new connection is coming :
    Qt Code:
    1. connect(server , SIGNAL(newConnection()),this, SLOT(newConnection()));
    2. void MainWindow::newConnection()
    3. {
    4. if (!connections.isEmpty())
    5. {
    6. QByteArray msg = "New Client connected\n";
    7. foreach (QTcpSocket* connection, connections)
    8. {
    9. connection->write(msg);
    10. }
    11. }
    12.  
    13. client= server->nextPendingConnection();
    14. QStringList tmpClientAddressPort;
    15.  
    16. connections.append(client);
    17.  
    18. QBuffer* buffer = new QBuffer(this);
    19. buffer->open(QIODevice::ReadWrite);
    20. buffers.insert(client, buffer);
    21.  
    22. connect(client, SIGNAL(disconnected()), this, SLOT(slotClientDisconnected()));
    23. connect(client, SIGNAL(readyRead()) , this, SLOT(readyReadR()));
    24. }
    To copy to clipboard, switch view to plain text mode 
    disconnected :
    Qt Code:
    1. void MainWindow::slotClientDisconnected()
    2. {
    3. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    4. QBuffer* buffer = buffers.take(socket);
    5. buffer->close();
    6. buffer->deleteLater();
    7. connections.removeAll(socket);
    8. socket->deleteLater();
    9.  
    10. QByteArray msg = "Client disconnected\n";
    11. foreach (QTcpSocket* connection, connections)
    12. {
    13. connection->write(msg);
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    readyread() :
    Qt Code:
    1. void rc::readyRead()
    2. {
    3. QByteArray data = client->readAll();
    4. In_Buf.append(data);
    5. ui->text->moveCursor(QTextCursor::End);
    6. ui->text->insertPlainText(In_Buf);
    7. }
    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 ?
    Last edited by hazel; 17th February 2016 at 12:56.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Dec 2015
    Posts
    9
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer connect to selected client

    i got it ;-) now i select client from treewidget with its peerport() ;

    Qt Code:
    1. void MainWindow::selectToClient()
    2. {
    3. foreach( QTreeWidgetItem *item, ui->twClientConnected->selectedItems() ) {
    4.  
    5. foreach (QTcpSocket* tcp, connections) {
    6. if(item->text(1).toUInt() == tcp->peerPort())
    7. {
    8. client=tcp;
    9. }
    10. }
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. how QTcpserver and and a client communicate
    By gauravg in forum Qt Programming
    Replies: 5
    Last Post: 11th June 2012, 13:07
  2. How to connect client sever in linux
    By onlybilal in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2011, 14:44
  3. receive data from client via QTcpServer
    By Fallen_ in forum Qt Programming
    Replies: 4
    Last Post: 8th September 2010, 16:08
  4. How to automaticaly connect client to server
    By sksingh73 in forum Newbie
    Replies: 1
    Last Post: 4th July 2010, 20:08
  5. Replies: 1
    Last Post: 5th July 2006, 14:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.