Results 1 to 7 of 7

Thread: QTcpServer dosent revie msg

  1. #1
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpServer dosent revie msg

    Hi

    I dont know why my server dosent recive any msg... I even copied code from example and it dosent work... Can u tell me what is wrong? I m sure thath client sendes msg but server dosent recive them... I tried to write in QDataStream in(tcpServer) but it didnt want to compile...

    client.h
    Qt Code:
    1. class klient : public QMainWindow,private Ui::klientClass
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. klient(QWidget *parent = 0);
    7. ~klient();
    8. public slots:
    9. // TCp
    10. void polacz();
    11. void Trejestracja();
    12. private:
    13. QTcpSocket *tcpSocket;
    14.  
    15.  
    16. };
    To copy to clipboard, switch view to plain text mode 

    client.cpp

    Qt Code:
    1. klient::klient(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. setupUi(this);
    5. polacz();
    6. Trejestracja();
    7.  
    8. }
    9. void klient::polacz()
    10. {
    11. tcpSocket = new QTcpSocket(this);
    12. tcpSocket->connectToHost("localhost",6112);
    13.  
    14. }
    15.  
    16. void klient::Trejestracja(QString l,QString h)
    17. {
    18. //COPIED FROM EXAMPLE
    19. QByteArray block;
    20. QDataStream out(&block, QIODevice::WriteOnly);
    21. out.setVersion(QDataStream::Qt_4_0);
    22. out << (quint16)0;
    23. out << "heh";
    24. out.device()->seek(0);
    25. out << (quint16)(block.size() - sizeof(quint16));
    26. tcpSocket->write(block);
    27. tcpSocket->disconnectFromHost();
    28. /*
    29.   QByteArray reje;
    30.   QDataStream out(&reje, QIODevice::WriteOnly);
    31.   out.setVersion(QDataStream::Qt_4_0);
    32.   out<<"TEST";
    33.   tcpSocket->write(reje);*/
    34. Lopis->setText("OK");
    35. }
    To copy to clipboard, switch view to plain text mode 

    server.h
    Qt Code:
    1. class server : public QWidget, private Ui::serverClass
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. server(QWidget *parent = 0);
    7. ~server();
    8. public slots:
    9. // TCP
    10. void newconnection();
    11. void odbierzw();
    12.  
    13.  
    14. private:
    15.  
    16. // TCP
    17. QTcpServer *tcpServer;
    18. QTcpSocket *clientConnection;
    19. quint16 blockSize;
    20.  
    21. };
    To copy to clipboard, switch view to plain text mode 

    server.cpp
    Qt Code:
    1. server::server(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. setupUi(this);
    5. // TCp
    6. tcpServer = new QTcpServer(this);
    7. tcpServer->listen(QHostAddress::Any,6112);
    8. connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newconnection()));
    9. connect(tcpServer, SIGNAL(readyRead()), this, SLOT(odbierzw()));
    10.  
    11.  
    12.  
    13.  
    14. }
    15.  
    16.  
    17. void server::newconnection()
    18. {
    19. clientConnection = tcpServer->nextPendingConnection();
    20. QString ip;
    21. ip = clientConnection->localAddress().toString();
    22. item->setText(0,ip);
    23. item->setText(1,QTime::currentTime().toString());
    24. TWlist->addTopLevelItem(item);
    25.  
    26. }
    27. void server::odbierzw()
    28. {
    29. // COPIED FROM EXAMPLE
    30. QDataStream in(clientConnection);
    31. in.setVersion(QDataStream::Qt_4_0);
    32.  
    33. if (blockSize == 0) {
    34. if (clientConnection->bytesAvailable() < (int)sizeof(quint16))
    35. return;
    36.  
    37. in >> blockSize;
    38. }
    39.  
    40. if (clientConnection->bytesAvailable() < blockSize)
    41. return;
    42. QString nextFortune;
    43. in >> nextFortune;
    44.  
    45.  
    46. /*
    47.   QDataStream in(clientConnection);
    48.   in.setVersion(QDataStream::Qt_4_0);
    49.   QString sprawdz;
    50.   in >> sprawdz;
    51.   label->setText(sprawdz);*/
    52. label_2->setText("DOSZLO");
    53.  
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 

    Big thx for help
    Best Regards

    ps
    sry for my english ;p
    Last edited by Zergi; 29th December 2007 at 17:09. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer dosent revie msg

    While I go through the code, try running the application from the Qt console, and see if you get any warnings in the console.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer dosent revie msg

    You should connect clientConnection's readyRead() to odbierzw().

  4. #4
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpServer dosent revie msg

    Yes i did thath before i posted ... When i do thath my program dosent want to open ;p I have msg with errors from windows... I tried to copy connect to newconnection() function, program opened but he still dosent want to recive msg....

    thx for help

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer dosent revie msg

    I have msg with errors from windows...
    Post the errors you got.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Dec 2007
    Posts
    28
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpServer dosent revie msg

    It's normal windows error ;p
    something like thath http://www.oatmedia.com/support/windows_error.jpg

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer dosent revie msg

    Compile your application in debug mode and start it within a debugger. You should get a real error message then. But first make sure that clientConnection is initialized at the time you establish the connection.

    And a small question: What will happen if two clients connect to the server at the same time?

  8. The following user says thank you to jacek for this useful post:

    Zergi (30th December 2007)

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.