Results 1 to 2 of 2

Thread: Qt Socketprogramming, deliver data repetitively to server

  1. #1

    Default Qt Socketprogramming, deliver data repetitively to server

    Hi
    I'd like to deliver the updated datastring every 20ms to the server on click. at the moment only one datastring is delivered on click.
    I tried to set a loop in void MainWindow::ConnectToServer(), but it does not seem to work.
    below is the affected part of the code

    I would appreciate your help
    thanks in advance



    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. // Initialize
    6. p_MainThread = NULL;
    7. p_SensorThreadArr = NULL;
    8. p_TcpSocket = NULL;
    9.  
    10. // set GUI
    11. SetGUIDialogBox();
    12.  
    13. ///p_MainThread = new MainThread();
    14.  
    15. // ********************
    16. // Connect to Server
    17. // ********************
    18. p_TcpSocket = &tcpSocket;
    19. //connect(p_TcpSocket, SIGNAL(connected()), this, SLOT(SendRequest()));
    20. connect(p_TcpSocket, SIGNAL(connected()), this, SLOT(StartTransfer()));
    21. // Two Buttons
    22. [B] connect(searchButton, SIGNAL(clicked()), this, SLOT(ConnectToServer()));[/B]
    23. connect(stopButton, SIGNAL(clicked()), this, SLOT(StopSearch()));
    24.  
    25. // **********************
    26. // Connect GUI Dialog Box
    27. // **********************
    28. connect(p_TcpSocket, SIGNAL(disconnected()), this, SLOT(ConnectionClosedByServer()));
    29. connect(p_TcpSocket, SIGNAL(readyRead()), this, SLOT(UpdateTableWidget()));
    30. connect(p_TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(Error()));
    31. }
    32.  
    33.  
    34. void MainWindow::ConnectToServer()
    35. {
    36. p_MainThread->start();
    37.  
    38. bool isRuning = true;
    39. while (isRuning == true)
    40. for (int Ind=0; Ind<5; Ind++)
    41. {
    42. qDebug( " connectToServer! " );
    43.  
    44. ////p_ClientDataSocket->ConnectToServer(address, port);
    45. quint16 port = 8888;
    46. QString address = "127.0.0.1";
    47. QHostAddress addr(address);
    48. p_TcpSocket->connectToHost(addr, port);
    49.  
    50. UpdateConnectionGUI();
    51. //sleep( 1 ); // Sleep xx ms
    52. }
    53.  
    54. }
    55.  
    56.  
    57. void MainWindow::StartTransfer()
    58. {
    59. // *********************************
    60. // read sensor data from all sensors
    61. // *********************************
    62. for (int Ind=0; Ind<m_SensorSum; Ind++)
    63. {
    64. QByteArray qBetyArray = p_SensorThreadArr[Ind]->GetSensorData();
    65.  
    66. // *****************************************
    67. // build client socket message as QByreArray
    68. // *****************************************
    69. // int sensorDataStatus = p_SensorThread->GetSensorDataStatus();
    70. // no updated sensor data
    71. // if (sensorDataStatus != 1)
    72. // return;
    73.  
    74. // *******************************************
    75. // send client socket message to server socket
    76. // *******************************************
    77. int len = qBetyArray.length();
    78. tcpSocket.write(qBetyArray, len);
    79. }
    80.  
    81. return;
    82. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Qt Socketprogramming, deliver data repetitively to server

    Your loop doesn't make any sense. connectToHost() is a non-blocking call, it just schedules a connect and returns immediately. You need a timer that is started when the socket manages to connect and that triggers a slot that will write to the socket. Oh, and if you expect that you'll get the data on the receiving end with time differences of 20ms, that's probably not going to happen. I would expect to see it all coming at the same time (or almost at the same time) unless you send enough data to fill one TCP window each time.
    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.


Similar Threads

  1. Problem in reading data from server.
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2011, 23:30
  2. server not getting client data
    By raj_iv in forum Qt Programming
    Replies: 0
    Last Post: 31st May 2011, 08:30
  3. Keep data in QSqlQueryModel when server is offline
    By Tottish in forum Qt Programming
    Replies: 10
    Last Post: 28th April 2011, 09:14
  4. Server data exchange strategy
    By thru in forum General Programming
    Replies: 3
    Last Post: 21st July 2010, 20:15
  5. Sql Server cannot retrieve data from select
    By Atomino in forum Qt Programming
    Replies: 10
    Last Post: 7th September 2006, 16:37

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.