Results 1 to 6 of 6

Thread: TCP Server/Client Application

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default TCP Server/Client Application

    Hi,

    My Server receives data (integer values) from the Clients (I called them "Drivers"). How to send data to the Viewer?

    You need to run the Server at first.

    Thank you!
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TCP Server/Client Application

    What is the actual problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    8Observer8 (4th September 2013)

  4. #3
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: TCP Server/Client Application

    My Driver01 sends the peckage to the Server every second:

    Qt Code:
    1. void Driver01::timeOut()
    2. {
    3. QByteArray buffer;
    4. buffer.append("Driver");
    5. buffer.append(" ");
    6. buffer.append("01");
    7. buffer.append(" ");
    8. buffer.append(QString::number(value));
    9. if(socket->isWritable()) {
    10. socket->write(buffer);
    11. }
    12. value++;
    13. qDebug() << "Driver01" << buffer;
    14. }
    To copy to clipboard, switch view to plain text mode 

    The Server receives the data. I want to send this data to the Viewer (Viewer is the GUI application).

    Server
    Qt Code:
    1. #include <QCoreApplication>
    2. #include "server.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QCoreApplication a(argc, argv);
    7.  
    8. Server server;
    9. server.startServer();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    server.cpp
    Qt Code:
    1. #include "server.h"
    2. #include "client.h"
    3. #include <QDebug>
    4.  
    5. Server::Server(QObject *parent) :
    6. QTcpServer(parent)
    7. {
    8. }
    9.  
    10. void Server::startServer()
    11. {
    12. if (this->listen(QHostAddress::Any, 1234)) {
    13. qDebug() << "Server started";
    14. }
    15. else {
    16. qDebug() << "Server could not start";
    17. }
    18. }
    19.  
    20. void Server::incomingConnection(int socketDescriptor) {
    21. Client *client = new Client(this);
    22. client->setSocket(socketDescriptor);
    23. }
    To copy to clipboard, switch view to plain text mode 

    client.cpp
    Qt Code:
    1. #include "client.h"
    2. #include "viewertask.h"
    3. #include "drivertask.h"
    4. #include <QThreadPool>
    5. #include <QDebug>
    6. #include <QStringList>
    7.  
    8. Client::Client(QObject *parent) :
    9. QObject(parent)
    10. {
    11. QThreadPool::globalInstance()->setMaxThreadCount(11);
    12. }
    13.  
    14. void Client::setSocket(int socketDescriptor)
    15. {
    16. socket = new QTcpSocket(this);
    17.  
    18. connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    19. connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    20. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    21.  
    22. socket->setSocketDescriptor(socketDescriptor);
    23.  
    24. qDebug() << socketDescriptor << " client connected";
    25. }
    26.  
    27. void Client::connected() {
    28. qDebug() << "Client connected event";
    29. }
    30.  
    31. void Client::disconnected() {
    32. qDebug() << "Client disconnected event";
    33. }
    34.  
    35. void Client::readyRead() {
    36. QByteArray buffer;
    37. buffer = socket->readAll();
    38.  
    39. QString package(buffer);
    40. if (package.isEmpty()) {
    41. return;
    42. }
    43.  
    44. QStringList elements = package.split(" ", QString::SkipEmptyParts);
    45. QString name = elements[0];
    46.  
    47. // Time Consumer
    48. if (name == "Driver") {
    49. DriverTask *driverTask = new DriverTask(elements);
    50. driverTask->setAutoDelete(true);
    51.  
    52. connect(driverTask, SIGNAL(result(QString, int, int)), this, SLOT(taskDriverResult(QString, int, int)), Qt::QueuedConnection);
    53.  
    54. QThreadPool::globalInstance()->start(driverTask);
    55. }
    56. else {
    57. ViewerTask *viewerTask = new ViewerTask;
    58. viewerTask->setAutoDelete(true);
    59.  
    60. connect(viewerTask, SIGNAL(result(int)), this, SLOT(taskResult(int)), Qt::QueuedConnection);
    61.  
    62. QThreadPool::globalInstance()->start(viewerTask);
    63. }
    64. }
    65.  
    66. void Client::taskResult(int number)
    67. {
    68. QByteArray buffer;
    69. buffer.append("- - - - - - - - - -");
    70. //buffer.append(QString::number(number));
    71.  
    72. socket->write(buffer);
    73. }
    74.  
    75. void Client::taskDriverResult(QString name, int id, int number)
    76. {
    77. qDebug() << name << " " << QString::number(id) << " " << QString::number(number);
    78. // QByteArray buffer;
    79. // buffer.append(name);
    80. // buffer.append("");
    81. // buffer.append(QString::number(id));
    82. // buffer.append("");
    83. // buffer.append(QString::number(number));
    84. }
    To copy to clipboard, switch view to plain text mode 

    drivertask.cpp
    Qt Code:
    1. #include "drivertask.h"
    2. #include <QDebug>
    3.  
    4. DriverTask::DriverTask(QStringList elements, QObject *parent) :
    5. QObject(parent)
    6. {
    7. this->name = elements[0];
    8. this->id = elements[1].toInt();
    9. this->value = elements[2].toInt();
    10. }
    11.  
    12. void DriverTask::run()
    13. {
    14. emit result(name, id, value);
    15. }
    To copy to clipboard, switch view to plain text mode 

    viewertask.cpp
    Qt Code:
    1. #include "viewertask.h"
    2. #include <QDebug>
    3.  
    4. ViewerTask::ViewerTask()
    5. {
    6. }
    7.  
    8. void ViewerTask::run()
    9. {
    10. // time consumer
    11.  
    12. qDebug() << "Viewer Task Start";
    13.  
    14. int iNumber = 0;
    15. for (int i = 0; i < 100; i++) {
    16. iNumber +=i;
    17. }
    18.  
    19. qDebug() << "Viewer Task Done";
    20.  
    21. emit result(iNumber);
    22. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TCP Server/Client Application

    Can't you send it the same way you received it?

    By the way, your client's readyRead() code is invalid. You are likely to miss some data there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    8Observer8 (4th September 2013)

  7. #5
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: TCP Server/Client Application

    I cannot because the Viewer and the Driver are the different Client-objects:

    Qt Code:
    1. void Server::incomingConnection(int socketDescriptor) {
    2. Client *client = new Client(this);
    3. client->setSocket(socketDescriptor);
    4. }
    To copy to clipboard, switch view to plain text mode 

    How to get the data from the Client-Driver for the Client-Viewer?

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: TCP Server/Client Application

    Quote Originally Posted by 8Observer8 View Post
    I cannot because the Viewer and the Driver are the different Client-objects:
    Sorry, I don't understand. If you use protocol X between points A and B, why can't you use the same protocol between points B and C?

    I don't really see the point of your thread here. Do you want us to invent some mystical hypothetical communication protocol between two (or more) unidentified, magical, hidden, maybe crouching, maybe shy, maybe legendary entities? If you have a specific question then ask it. If you have a specific problem, then describe it and then say what kind of help you expect. If you have an unidentified problem, identify it first.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    8Observer8 (4th September 2013)

Similar Threads

  1. Replies: 3
    Last Post: 17th May 2013, 13:13
  2. Client/server application
    By sali555 in forum Qt Programming
    Replies: 5
    Last Post: 22nd April 2011, 09:55
  3. problem with client/server application
    By marco.stanzani in forum Newbie
    Replies: 1
    Last Post: 9th April 2011, 07:36
  4. TCP Client-Server Application
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2008, 13:20

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
  •  
Qt is a trademark of The Qt Company.