Results 1 to 3 of 3

Thread: How I can get the message from client

  1. #1

    Default How I can get the message from client

    How can i get reading the message from client,Sorry My English
    I was using this example http://wiki.qtcentre.org/index.php?title=Simple_Chat

    Server.cpp
    Qt Code:
    1. connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    2. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(sendFortune()));
    3.  
    4. QHBoxLayout *buttonLayout = new QHBoxLayout;
    5. buttonLayout->addStretch(1);
    6. buttonLayout->addWidget(quitButton);
    7. buttonLayout->addStretch(1);
    8.  
    9.  
    10. QVBoxLayout *mainLayout = new QVBoxLayout;
    11. mainLayout->addWidget(statusLabel);
    12. mainLayout->addLayout(buttonLayout);
    13. setLayout(mainLayout);
    14.  
    15. setWindowTitle(tr("Fortune Server"));
    16. }
    17.  
    18. void Server::sendFortune()
    19. {
    20. QTcpSocket *tcpSocket = tcpServer->nextPendingConnection();
    21.  
    22. connections.append(tcpSocket);
    23. QBuffer* buffer = new QBuffer(this);
    24. buffer->open(QIODevice::ReadWrite);
    25. buffers.insert(tcpSocket, buffer);
    26.  
    27. out.setDevice(tcpSocket);
    28. QString tiedosto=haeTiedosto(kuvaIndex);
    29. QFile file(tiedosto);
    30. file.open(QIODevice::ReadOnly);
    31. fstr.setDevice(&file);
    32.  
    33.  
    34. file.seek(0);
    35. char* data = new char[65536];
    36. while(!file.atEnd())
    37. {
    38. int read = fstr.readRawData(data, 65536);
    39. int sent = out.writeRawData(data, read);
    40. }
    41.  
    42. delete[] data;
    43.  
    44. tcpSocket->waitForBytesWritten();
    45.  
    46.  
    47.  
    48. connect(tcpSocket, SIGNAL(readyRead()), SLOT(receiveMessage()));
    49. connect(tcpSocket, SIGNAL(disconnected()), SLOT(removeConnection()));
    50.  
    51.  
    52. }
    53. void Server::removeConnection()
    54. {
    55.  
    56. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    57. QBuffer* buffer = buffers.take(socket);
    58. buffer->close();
    59. buffer->deleteLater();
    60. connections.removeAll(socket);
    61. socket->deleteLater();
    62.  
    63.  
    64.  
    65.  
    66. }
    67.  
    68. void Server::receiveMessage()
    69. {
    70.  
    71. QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
    72. QBuffer* buffer = buffers.value(socket);
    73.  
    74. // missing some checks for returns values for the sake of simplicity
    75. qint64 bytes = buffer->write(socket->readAll());
    76. // go back as many bytes as we just wrote so that it can be read
    77. buffer->seek(buffer->pos() - bytes);
    78. // read only full lines, line by line
    79. while (buffer->canReadLine())
    80. {
    81. QByteArray line = buffer->readLine();
    82. foreach (QTcpSocket* tcpSocket, connections)
    83. {
    84. int d=line.toInt();
    85. QString data=data.setNum(d);
    86. statusLabel->setText(data);
    87. }
    88.  
    89. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How I can get the message from client

    You should implement it as the receiving part is implemented in the client. No big differences. But you'll have to modify the client too, to be able to send data.

    Regards

  3. #3

    Default Re: How I can get the message from client

    Qt Code:
    1. #include <QtGui>
    2. #include <QImage>
    3. #include "kuvaclient.h"
    4.  
    5.  
    6. // including <QtGui> saves us to include every class user, <QString>, <QFileDialog>,...
    7.  
    8. KuvaClient::KuvaClient(QDialog *parent)
    9. {
    10. setupUi(this); // this sets up GUI
    11.  
    12. tcpSocket = new QTcpSocket(this);
    13. sockStream = new QDataStream(tcpSocket);
    14. file = new QFile("testi.bmp");
    15. if(file->exists())
    16. file->remove();
    17. file->open(QIODevice::ReadWrite);
    18.  
    19. numberFrame=0;
    20. fileStream = new QDataStream(file);
    21. tcpSocket->abort();
    22. tcpSocket->connectToHost("masa",1200);
    23. connect(vasen, SIGNAL( clicked() ), this, SLOT( Vasenpaina()));
    24. connect(oikea, SIGNAL( clicked() ), this, SLOT( Oikeapaina()));
    25. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readImage()));
    26.  
    27. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    28. this, SLOT(displayError(QAbstractSocket::SocketError)));
    29. }
    30.  
    31. void KuvaClient::Vasenpaina()
    32. {
    33.  
    34. tcpSocket->abort();
    35. tcpSocket->connectToHost("localhost",1200);
    36. numberFrame=numberFrame+1;
    37. QString number=number.setNum(numberFrame);
    38. //koe->setText(number);
    39.  
    40. tcpSocket->write(number);
    41.  
    42.  
    43.  
    44.  
    45. }
    46. void KuvaClient::readImage()
    47. {
    48.  
    49. //tcpSocket->seek(0);
    50. char *buffer = new char[65536];
    51. //int count = tcpSocket->bytesAvailable();
    52. //sockStream->resetStatus();
    53. while(!sockStream->atEnd())
    54. {
    55. int src = sockStream->readRawData(buffer, 65536);
    56. int rcv = fileStream->writeRawData(buffer, src);
    57. int xxx = 0;
    58. xxx++;
    59. }
    60.  
    61. delete[] buffer;
    62. file->flush();
    63. QImage someimage("testi.bmp");
    64. QPixmap preview;
    65. preview = preview.fromImage(someimage);
    66.  
    67. label->setPixmap(preview);
    68. tcpSocket->waitForBytesWritten();
    69.  
    70.  
    71. }
    72. void KuvaClient::Oikeapaina()
    73. {
    74.  
    75.  
    76. tcpSocket->abort();
    77. tcpSocket->connectToHost("locahost",1200);
    78. numberFrame++;
    79. /*QString number=number.setNum(numberFrame);
    80.   tcpSocket->write(number.toLatin1() );
    81. tcpSocket->deleteLater();
    82. */
    83.  
    84. }
    85. void KuvaClient::displayError(QAbstractSocket::SocketError socketError)
    86. {
    87. switch (socketError) {
    88. case QAbstractSocket::RemoteHostClosedError:
    89. break;
    90. case QAbstractSocket::HostNotFoundError:
    91. QMessageBox::information(this, tr("Fortune Client"),
    92. tr("The host was not found. Please check the "
    93. "host name and port settings."));
    94. break;
    95. case QAbstractSocket::ConnectionRefusedError:
    96. QMessageBox::information(this, tr("Fortune Client"),
    97. tr("The connection was refused by the peer. "
    98. "Make sure the fortune server is running, "
    99. "and check that the host name and port "
    100. "settings are correct."));
    101. break;
    102. default:
    103. QMessageBox::information(this, tr("Fortune Client"),
    104. tr("The following error occurred: %1.")
    105. .arg(tcpSocket->errorString()));
    106. }
    107.  
    108.  
    109. }
    To copy to clipboard, switch view to plain text mode 
    This is my client code.It' writes message to server,but i can't read message from socket in the server side.

Similar Threads

  1. statusBar() message color change
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 23:20
  2. How to I get read of the message
    By jcr in forum General Discussion
    Replies: 1
    Last Post: 16th April 2007, 15:22
  3. Fix: QT app won't run under Oracle Standard Client install
    By GreyGeek in forum General Discussion
    Replies: 0
    Last Post: 12th May 2006, 18:20
  4. How do I keep the client connection open ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 25th March 2006, 19:06
  5. synching client readings to server output
    By OnionRingOfDoom in forum Qt Programming
    Replies: 14
    Last Post: 28th January 2006, 18:15

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.