Results 1 to 6 of 6

Thread: Qt Network Programming

  1. #1
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Qt Network Programming

    Hy!

    I got a problem with small Qt Network application. I got a Client and a Server which communicates with TCP. I want, that they are able to send messages to each other. I am in an early development stage of this application and I am testing fundamental function at the moment. Following flowchat describes the problem.

    ..............CLIENT...............................................SERVER


    ........................TCP connect [SYN], [SYN,ACK], [ACK]
    ....................<---------------- ---------------------------------->

    ....................<----------------------------------------------------- ......sendResponse
    .....................................TCP Message [PSH].................................Button

    ....................----------------------------------------------------->
    ..................................TCP Acknowledge [ACK]
    display the
    message


    .................................................. ..........................<---- *


    When I try to send at the end of the flowchat (at *) I got the Message on the console of the Server: SEGMENTATION Fault

    The SourceCode for sending messages on the Server is following:

    Qt Code:
    1. void Server::sendResponse()
    2. {
    3. QString responseMessage;
    4. responseMessage = "ACK Hello";
    5. QByteArray block;
    6. QDataStream out(&block, QIODevice::WriteOnly);
    7. out.setVersion(QDataStream::Qt_4_0);
    8. out << (quint16)0;
    9. out << responseMessage;
    10. out.device()->seek(0);
    11. out << (quint16)(block.size() - sizeof(quint16));
    12.  
    13. QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    14.  
    15. clientConnection->write(block);
    16. }
    To copy to clipboard, switch view to plain text mode 


    What is the problem, what can do to solve it?



    Best Regards!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Network Programming

    Could you run your application through a debugger and print the backtrace?

  3. #3
    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: Qt Network Programming

    If there are no pending connections then nextPendingConnection returns NULL.
    You should test for that before calling clientConnection->write().

    Regards

  4. #4
    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: Qt Network Programming

    I can't understand your code when compared to your flow diagram.
    What is this part for?:
    Qt Code:
    1. out.device()->seek(0);
    2. out << (quint16)(block.size() - sizeof(quint16));
    To copy to clipboard, switch view to plain text mode 

    Also, I am not sure this slot is a good place for:
    Qt Code:
    1. QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    To copy to clipboard, switch view to plain text mode 
    Since you assume that every time you get data from what ever socket, there is another pending connection - and there is no assurance for that.
    So this code:
    Qt Code:
    1. QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    2. clientConnection->write(block);
    To copy to clipboard, switch view to plain text mode 
    Is probably what is crashing your application, when clientConnection is NULL.

    At the very least you should test it first:
    Qt Code:
    1. QTcpSocket *clientConnection = NULL;
    2. if(tcpServer->hasPendingConnections()
    3. {
    4. clientConnection = tcpServer->nextPendingConnection();
    5. clientConnection->write(block);
    6. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: beat to it by marcel
    ==========================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.

  5. #5
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Network Programming

    Quote Originally Posted by wysota View Post
    Could you run your application through a debugger and print the backtrace?

    OMG!

    What do you mean with the backtrace?
    You know, .... I am one of the newbs! ;-)

  6. #6
    Join Date
    Mar 2007
    Location
    Vienna / Austria
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Network Programming

    Thx High_flyer,
    Thx marcel

    I am going to test this and I will reconsider my code, and if I have any further problems, you will tell you! ;-)

    Many thx to you!

Similar Threads

  1. Network programming and the main thread
    By invictus in forum Qt Programming
    Replies: 5
    Last Post: 16th March 2007, 00:25
  2. QT COM Programming
    By sarav in forum Newbie
    Replies: 5
    Last Post: 24th February 2007, 13:41
  3. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  4. QSocket - signal for network wire disconnection
    By manivannan_1984 in forum Qt Programming
    Replies: 7
    Last Post: 5th September 2006, 13:52
  5. MODEL/VIEW programming
    By mira in forum Newbie
    Replies: 3
    Last Post: 21st April 2006, 11:19

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.