Results 1 to 5 of 5

Thread: receive data from client via QTcpServer

  1. #1
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default receive data from client via QTcpServer

    Hi, I made a simple chat server, but can't get it working to receive data from the client (incoming connection)
    heres my code:
    server.h:
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QTcpServer;
    7. class QLabel;
    8. class QTextEdit;
    9.  
    10. class Server : public QDialog
    11. {
    12. Q_OBJECT
    13. public:
    14. Server(QWidget* parent = 0);
    15.  
    16. private slots:
    17. void sendWelcomeMessage();
    18.  
    19. private:
    20. QLabel* status;
    21. QPushButton* quit;
    22. QTcpServer* server;
    23. QTextEdit* read;
    24. };
    25. #endif
    To copy to clipboard, switch view to plain text mode 
    server.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3. #include "server.h"
    4.  
    5. Server::Server(QWidget* parent)
    6. : QDialog(parent)
    7. {
    8. status = new QLabel;
    9. quit = new QPushButton(tr("&Close"));
    10. connect(quit, SIGNAL(clicked()), this, SLOT(close()));
    11. server = new QTcpServer(this);
    12. read = new QTextEdit(tr("Status:\n"));
    13. read->setReadOnly(true);
    14. QHostAddress addr("127.0.0.1");
    15.  
    16. if(!server->listen(addr, 12345)) {
    17. QMessageBox::information(this, tr("Error!!"), tr("The following errors has occured: %1").arg(server->errorString()));
    18. return;
    19. }
    20.  
    21. status->setText(tr("The server is running on %1 port.").arg(server->serverPort()));
    22. connect(server, SIGNAL(newConnection()), this, SLOT(sendWelcomeMessage()));
    23.  
    24. QHBoxLayout* layout = new QHBoxLayout;
    25. layout->addWidget(status);
    26. layout->addWidget(quit);
    27. layout->addWidget(read);
    28. setLayout(layout);
    29. }
    30.  
    31. void Server::sendWelcomeMessage()
    32. {
    33. typedef QVector<QTcpSocket*> Connections;
    34. Connections connections;
    35. QTcpSocket *client = server->nextPendingConnection();
    36. connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
    37. client->write("Welcome!");
    38. char buffer[100];
    39. sprintf(buffer, "Incoming connection from IP: %s", client->peerAddress().toString().toStdString().c_str());
    40. read->append(buffer);
    41. connections.push_back(client);
    42. for(Connections::Iterator it = connections.begin(); it != connections.end(); ++it)
    43. (*it)->write("Hello!!!");
    44. }
    To copy to clipboard, switch view to plain text mode 
    guess no need for main.cpp.
    Well, i tried with client->readLine & client->read both didnt work.
    @Edit:
    also tried with QTextStream and QDataStream didnt work also.
    Last edited by Fallen_; 8th September 2010 at 01:27.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: receive data from client via QTcpServer

    You can only read from the socket if there's something to read.
    You try to read a nanosecond after you've send the welcom message. In that time, the client didn't have time to respond.

    Suggestions:
    Qt Code:
    1. void Server::sendWelcomeMessage()
    2. {
    3. typedef QVector<QTcpSocket*> Connections; // <-- Define this at the class level
    4. Connections connections; // This too
    5.  
    6. while (server->hasPendingConnections ()) {
    7. QTcpSocket *client = server->nextPendingConnection();
    8. connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
    9. connect(client, SIGNAL(readyRead()), signalmapper, SLOT(map())); // Signal mapper is defined in the class definition and created in the constructor, see QSignalMapper.
    10. signalmapper->setMapping(client, client);
    11. client->write("Welcome!");
    12. }
    13. connect(signalmapper, SIGNAL(mapped(QObject *)), this, SLOT(clientReadyRead(QObject *)));
    14. }
    15.  
    16. void Server::clientReadyRead(QObject *socket)
    17. {
    18. QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(socket);
    19.  
    20. clientSocket->read...()
    21. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: receive data from client via QTcpServer

    Exactly as mentioned, try to read the data on the readyRead() signal for the socket, which ensures that some data is present on the socket and then try to read.

  4. #4
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: receive data from client via QTcpServer

    Quote Originally Posted by tbscope View Post
    You can only read from the socket if there's something to read.
    You try to read a nanosecond after you've send the welcom message. In that time, the client didn't have time to respond.

    Suggestions:
    Qt Code:
    1. void Server::sendWelcomeMessage()
    2. {
    3. typedef QVector<QTcpSocket*> Connections; // <-- Define this at the class level
    4. Connections connections; // This too
    5.  
    6. while (server->hasPendingConnections ()) {
    7. QTcpSocket *client = server->nextPendingConnection();
    8. connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
    9. connect(client, SIGNAL(readyRead()), signalmapper, SLOT(map())); // Signal mapper is defined in the class definition and created in the constructor, see QSignalMapper.
    10. signalmapper->setMapping(client, client);
    11. client->write("Welcome!");
    12. }
    13. connect(signalmapper, SIGNAL(mapped(QObject *)), this, SLOT(clientReadyRead(QObject *)));
    14. }
    15.  
    16. void Server::clientReadyRead(QObject *socket)
    17. {
    18. QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(socket);
    19.  
    20. clientSocket->read...()
    21. }
    To copy to clipboard, switch view to plain text mode 
    i get this error:
    signalmapper was not declared in this scope

  5. #5
    Join Date
    Aug 2010
    Posts
    58
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: receive data from client via QTcpServer

    nvm, i fixed it myself thx

Similar Threads

  1. Replies: 4
    Last Post: 18th August 2010, 08:13
  2. Replies: 1
    Last Post: 24th March 2010, 08:15
  3. QTCPsocket data corrupt when receive
    By gabizzz in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2010, 17:29
  4. Replies: 14
    Last Post: 27th November 2006, 05:09
  5. Replies: 1
    Last Post: 5th July 2006, 14:12

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.