Results 1 to 4 of 4

Thread: tcp server poblem

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default tcp server poblem

    I get problems when I want to convert my client program which receives an image size and contents to server program. The client program works properly:
    Qt Code:
    1. void Client::readFortune()
    2. {
    3. QDataStream in(tcpSocket);
    4.  
    5. if (blockSize == 0) {
    6. if (tcpSocket->bytesAvailable() < (int)sizeof(quint32))
    7. return;
    8.  
    9. in >> blockSize;
    10.  
    11. qDebug() << blockSize;
    12. }
    13.  
    14. if (tcpSocket->bytesAvailable() < blockSize)
    15. return;
    16.  
    17. QByteArray nextFortune;
    18. in >> nextFortune;
    19.  
    20. if (nextFortune == currentFortune) {
    21. QTimer::singleShot(0, this, SLOT(requestNewFortune()));
    22. return;
    23. }
    24.  
    25. currentFortune = nextFortune;
    26. statusLabel->setText(currentFortune);
    27. getFortuneButton->setEnabled(true);
    28.  
    29. QImage image;
    30. image.loadFromData(nextFortune);
    31.  
    32. imageLabel->setPixmap(QPixmap::fromImage(image));
    33. }
    To copy to clipboard, switch view to plain text mode 

    However, when I convert client to server the size it gets is wrong. is there anything wrong with the logic of below server program?

    Qt Code:
    1. void Server::sendFortune()
    2. {
    3. clientConnection = tcpServer->nextPendingConnection();
    4.  
    5. connect(clientConnection, SIGNAL(disconnected()),
    6. clientConnection, SLOT(deleteLater()));
    7.  
    8. blockSize = 0;
    9.  
    10. connect(clientConnection, SIGNAL(readyRead()), this, SLOT(recvFortune()));
    11. }
    12.  
    13. void Server::recvFortune()
    14. {
    15. QDataStream in(clientConnection);
    16.  
    17. if (blockSize == 0) {
    18. if (clientConnection->bytesAvailable() < (int) sizeof(quint32))
    19. return;
    20.  
    21. in >> blockSize;
    22.  
    23. qDebug() << blockSize;
    24. }
    25.  
    26. if (clientConnection->bytesAvailable() < blockSize)
    27. return;
    28.  
    29. QByteArray nextFortune;
    30. in >> nextFortune;
    31.  
    32. if (nextFortune == currentFortune) {
    33. QTimer::singleShot(0, this, SLOT(requestNewFortune()));
    34. return;
    35. }
    36.  
    37. QString fileName = "/home/saman/Desktop/png/dest.png";
    38. QFile file(fileName);
    39. if (!file.open(QIODevice::WriteOnly)) return;
    40. file.write(nextFortune);
    41. file.close();
    42.  
    43. currentFortune = nextFortune;
    44. statusLabel->setText(currentFortune);
    45.  
    46. QImage image;
    47. image.loadFromData(nextFortune);
    48.  
    49. imageLabel->setPixmap(QPixmap::fromImage(image));
    50. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by saman_artorious; 3rd August 2013 at 09:30.

  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 poblem

    In what way it is "wrong"?

    Your app is bound to fail when the peer tries to send a second image but the first block of data should work, assuming you preinitialized blockSize to 0.
    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. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: tcp server poblem

    Quote Originally Posted by wysota View Post
    In what way it is "wrong"?

    Your app is bound to fail when the peer tries to send a second image but the first block of data should work, assuming you preinitialized blockSize to 0.
    It reads the image size as 506724864, a very large number. Besides, it does not even read the contents. As the same logic runs properly in the client program, I am wondering if I've done something wrong in the server program.

    Qt Code:
    1. #include <QtWidgets>
    2. #include <QtNetwork>
    3.  
    4. #include <stdlib.h>
    5.  
    6. #include "server.h"
    7.  
    8. Server::Server(QWidget *parent)
    9. : QDialog(parent), tcpServer(0), networkSession(0)
    10. {
    11. statusLabel = new QLabel;
    12. quitButton = new QPushButton(tr("Quit"));
    13. quitButton->setAutoDefault(false);
    14.  
    15. QNetworkConfigurationManager manager;
    16. if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
    17. // Get saved network configuration
    18. QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
    19. settings.beginGroup(QLatin1String("QtNetwork"));
    20. const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
    21. settings.endGroup();
    22.  
    23. // If the saved network configuration is not currently discovered use the system default
    24. QNetworkConfiguration config = manager.configurationFromIdentifier(id);
    25. if ((config.state() & QNetworkConfiguration::Discovered) !=
    26. QNetworkConfiguration::Discovered) {
    27. config = manager.defaultConfiguration();
    28. }
    29.  
    30. networkSession = new QNetworkSession(config, this);
    31. connect(networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
    32.  
    33. statusLabel->setText(tr("Opening network session."));
    34. networkSession->open();
    35. } else {
    36. sessionOpened();
    37. }
    38.  
    39. fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
    40. << tr("You've got to think about tomorrow.")
    41. << tr("You will be surprised by a loud noise.")
    42. << tr("You will feel hungry again in another hour.")
    43. << tr("You might have mail.")
    44. << tr("You cannot kill time without injuring eternity.")
    45. << tr("Computers are not intelligent. They only think they are.");
    46.  
    47. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    48. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
    49.  
    50. QHBoxLayout *buttonLayout = new QHBoxLayout;
    51. buttonLayout->addStretch(1);
    52. buttonLayout->addWidget(quitButton);
    53. buttonLayout->addStretch(1);
    54.  
    55. QVBoxLayout *mainLayout = new QVBoxLayout;
    56. mainLayout->addWidget(statusLabel);
    57. mainLayout->addLayout(buttonLayout);
    58. setLayout(mainLayout);
    59.  
    60. setWindowTitle(tr("Fortune Server"));
    61. }
    62.  
    63. void Server::sessionOpened()
    64. {
    65. // Save the used configuration
    66. if (networkSession) {
    67. QNetworkConfiguration config = networkSession->configuration();
    68. QString id;
    69. if (config.type() == QNetworkConfiguration::UserChoice)
    70. id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
    71. else
    72. id = config.identifier();
    73.  
    74. QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
    75. settings.beginGroup(QLatin1String("QtNetwork"));
    76. settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
    77. settings.endGroup();
    78. }
    79.  
    80. tcpServer = new QTcpServer(this);
    81. if (!tcpServer->listen()) {
    82. QMessageBox::critical(this, tr("Fortune Server"),
    83. tr("Unable to start the server: %1.")
    84. .arg(tcpServer->errorString()));
    85. close();
    86. return;
    87. }
    88. QString ipAddress;
    89. QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
    90. // use the first non-localhost IPv4 address
    91. for (int i = 0; i < ipAddressesList.size(); ++i) {
    92. if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
    93. ipAddressesList.at(i).toIPv4Address()) {
    94. ipAddress = ipAddressesList.at(i).toString();
    95. break;
    96. }
    97. }
    98. // if we did not find one, use IPv4 localhost
    99.  
    100. if (ipAddress.isEmpty())
    101. ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
    102. statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
    103. "Run the Fortune Client example now.")
    104. .arg(ipAddress).arg(tcpServer->serverPort()));
    105. }
    106.  
    107. void Server::sendFortune()
    108. {
    109. clientConnection = tcpServer->nextPendingConnection();
    110.  
    111. connect(clientConnection, SIGNAL(disconnected()),
    112. clientConnection, SLOT(deleteLater()));
    113.  
    114. blockSize = 0;
    115.  
    116. connect(clientConnection, SIGNAL(readyRead()), this, SLOT(recvFortune()));
    117. }
    118.  
    119. void Server::recvFortune()
    120. {
    121. QDataStream in(clientConnection);
    122.  
    123. if (blockSize == 0) {
    124. if (clientConnection->bytesAvailable() < (int) sizeof(quint32))
    125. return;
    126.  
    127. in >> blockSize;
    128.  
    129. qDebug() << blockSize;
    130. }
    131.  
    132. if (clientConnection->bytesAvailable() < blockSize)
    133. return;
    134.  
    135. QByteArray nextFortune;
    136. in >> nextFortune;
    137.  
    138. qDebug() << nextFortune;
    139.  
    140. if (nextFortune == currentFortune) {
    141. QTimer::singleShot(0, this, SLOT(requestNewFortune()));
    142. return;
    143. }
    144.  
    145. QString fileName = "/home/saman/Desktop/png/dest.png";
    146. QFile file(fileName);
    147. if (!file.open(QIODevice::WriteOnly)) return;
    148. file.write(nextFortune);
    149. file.close();
    150.  
    151. currentFortune = nextFortune;
    152. statusLabel->setText(currentFortune);
    153.  
    154. QImage image;
    155. image.loadFromData(nextFortune);
    156.  
    157. imageLabel->setPixmap(QPixmap::fromImage(image));
    158.  
    159. blockSize = 0;
    160. }
    To copy to clipboard, switch view to plain text mode 

  4. #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 poblem

    I'm not sure if repeating what I (or others) have said in this forum in dozens of other threads regarding TCP connections makes any sense. I can think of a myriad of ways your code is going to fail. One of them is to connect to the "server" with two clients at the same time and try to make them send images to the server.

    So instead of repeating the same thing all over again (you can search the forums for similar problems), I am just going to say that your code is inherently wrong and will continue to be wrong unless you first devote yourself to understanding how a network connection between two peers works.

    I don't even want to think why you called your QDialog subclass "Server"...
    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. [TCP Server] Server only sending when terminating
    By papperlapapp in forum Qt Programming
    Replies: 0
    Last Post: 6th December 2010, 19:41
  2. Replies: 5
    Last Post: 2nd June 2009, 19:47
  3. QSslSocket server
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2009, 04:10
  4. server upload
    By ag.sitesh in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 13:57
  5. Poblem with readLineStdout()-QProcess
    By prasann87 in forum Qt Programming
    Replies: 0
    Last Post: 8th January 2008, 10:43

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.