Results 1 to 10 of 10

Thread: QTcpSoket and QTcpServer problem.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2007
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default QTcpSoket and QTcpServer problem.

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MyWidget(QWidget *parent = 0);
    6. public slots:
    7.  
    8. void Conectar()
    9. {
    10. socket->abort();
    11. socket->connectToHost("Localhost", 1050);
    12. }
    13.  
    14. private:
    15. QTcpSocket *socket;
    16. };
    17.  
    18. class MyQTextEdit : public QTextEdit
    19. {
    20. Q_OBJECT
    21. QLineEdit * textoEnvia;
    22.  
    23. public:
    24. MyQTextEdit(QWidget *parent, QLineEdit* tE) : QTextEdit(parent), textoEnvia(tE){
    25.  
    26. }
    27.  
    28. public slots:
    29.  
    30. void EnviaMsg()
    31. {
    32. char * Data = "Data";
    33. append(textoEnvia->text());
    34. textoEnvia->clear();
    35.  
    36. }
    37. };
    38.  
    39.  
    40. #include "main.moc"
    41.  
    42. MyWidget::MyWidget(QWidget *parent)
    43. : QWidget(parent)
    44. {
    45. this->setFixedSize(600, 300);
    46.  
    47. socket = new QTcpSocket(this);
    48.  
    49. QWidget * wLayout = new QWidget(this);
    50. QLineEdit * textoEnvia = new QLineEdit(wLayout);
    51. QPushButton *enviar = new QPushButton(tr("Enviar"), wLayout);
    52. QPushButton *conectar = new QPushButton(tr("Conectar"), wLayout);
    53. MyQTextEdit *output = new MyQTextEdit(this, textoEnvia);
    54. output->setMaximumHeight(200);
    55.  
    56. QVBoxLayout *layout = new QVBoxLayout;
    57. QHBoxLayout *lLayout = new QHBoxLayout;
    58.  
    59. lLayout->addWidget(textoEnvia);
    60. lLayout->addWidget(enviar);
    61. lLayout->addWidget(conectar);
    62. wLayout->setLayout(lLayout);
    63.  
    64. layout->addWidget(output);
    65. layout->addWidget(wLayout);
    66.  
    67. setLayout(layout);
    68.  
    69. textoEnvia->setText("Digite aqui o texto...");
    70.  
    71. connect(enviar, SIGNAL(clicked()), output, SLOT(EnviaMsg()));
    72. connect(conectar, SIGNAL(clicked()), this, SLOT(Conectar()));
    73. }
    74.  
    75. int main(int argc, char *argv[])
    76. {
    77. QApplication app(argc, argv);
    78. MyWidget widget;
    79. widget.show();
    80. return app.exec();
    81. }
    To copy to clipboard, switch view to plain text mode 

    This is the Client, it send a msg:"Caio", when the buttom "enviar" is clicked.

    And Connects with the server when the buttom "conectar" is cliked.

    Qt Code:
    1. #include "server.h"
    2.  
    3. Server::Server(QWidget *parent)
    4. : QDialog(parent)
    5. {
    6. statusLabel = new QLabel(this);
    7.  
    8. tcpServer = new QTcpServer(this);
    9. if (!tcpServer->listen()) {
    10. QMessageBox::critical(this, tr("Fortune Server"),
    11. tr("Unable to start the server: %1.")
    12. .arg(tcpServer->errorString()));
    13. close();
    14. return;
    15. }
    16.  
    17. statusLabel->setText(tr("The server is running on port %1.\n"
    18. "Run the Fortune Client example now.")
    19. .arg(tcpServer->serverPort()));
    20.  
    21. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
    22.  
    23. setWindowTitle(tr("Fortune Server"));
    24. }
    25.  
    26. void Server::newClient()
    27. {
    28. statusLabel->setText(tr("Connectou"));
    29. clientConnection = tcpServer->nextPendingConnection();
    30. connect(clientConnection, SIGNAL(disconnected()),
    31. clientConnection, SLOT(deleteLater()));
    32. connect(clientConnection, SIGNAL(readyRead()), this, SLOT(Leia()));
    33.  
    34. }
    35.  
    36. void Server::Leia()
    37. {
    38. char * Data;
    39. clientConnection->read(Data,256);
    40. statusLabel->setText(Data);
    41. statusLabel->setText("Caiow");
    42. }
    To copy to clipboard, switch view to plain text mode 

    And here's the server.

    But the problem is simple, this connect never executes: connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));

    Why?

    I execute the server, and after the client, the same Address and the same port.
    Last edited by v3n0w; 12th June 2007 at 21:39.

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.