Results 1 to 20 of 27

Thread: Reading from TCP Socket crashes program

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Reading from TCP Socket crashes program

    Ok, so I have a program which acts as a server, and can write data to a socket. Now when I use the client program to connect to the server, the client connects just fine, but it crashes whenever it goes to read the data. I'm not entirely sure how the whole QIODevice thing plays into it, but what I wrote makes sense, at least to me :P. Anyway, here's the code:

    Qt Code:
    1. // Server/Client-related connections
    2. connect(&tcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
    3. connect(&tcpServer, SIGNAL(connectionAccepted()), this, SLOT(sendSpeed()));
    4. connect(&tcpClient, SIGNAL(connected()), this, SLOT(readSpeed()));
    5.  
    6. connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
    7. connect(&tcpClient, SIGNAL(state(QAbstractSocket::SocketState)), this, SLOT(displayStatus(QAbstractSocket::SocketState)));
    8.  
    9. // Set up layout
    10. layout->addWidget(connectButton, 0, 0, 1, 2);
    11. layout->addWidget(slider, 1, 0, 1, 2);
    12. layout->addWidget(label, 3, 0);
    13. //layout->addWidget(label2, 3, 0);
    14. layout->addWidget(ipLabel, 5, 0);
    15. win->setLayout(layout);
    16.  
    17. } // MainWindow()
    18.  
    19. ////////////////////////////////////////////////////////////////////////
    20. // SERVER FUNCTIONS //
    21.  
    22. // startServer(QString ip, quint16 port) - Starts the server
    23. ////////////////////////////////////////////////////////////////////////
    24. void MainWindow::startServer(QString ip, quint16 port)
    25. {
    26. statusBar->showMessage("Server created", 0);
    27. if (!tcpServer.listen(QHostAddress(ip), port))
    28. {
    29. statusBar->showMessage(tr("Unable to start the server: %1.", 0).arg(tcpServer.errorString()));
    30. return;
    31. }
    32. else
    33. {
    34. statusBar->showMessage(tr("Listening on port %1...", 0).arg(tcpServer.serverPort()));
    35. }
    36. }
    37.  
    38. // acceptConnection() - Completes the connection to the client
    39. ////////////////////////////////////////////////////////////////////////
    40. void MainWindow::acceptConnection()
    41. {
    42. slider->setAmpValue(10);
    43. slider->setFreqValue(10);
    44. clientConnection = tcpServer.nextPendingConnection();
    45. statusBar->showMessage("Connection established.", 0);
    46. emit connectionAccepted();
    47. }
    48.  
    49. // sendSpeed() - Sends the slider speed to the client
    50. ////////////////////////////////////////////////////////////////////////
    51. void MainWindow::sendSpeed()
    52. {
    53. QByteArray block;
    54. QDataStream out(&block, QIODevice::WriteOnly);
    55. value = slider->ampValue();
    56.  
    57. // Write the slider value to the output buffer
    58. out.setVersion(QDataStream::Qt_4_0);
    59. out << value;
    60.  
    61. // Send the linear slider value to the client
    62. clientConnection->write(block);
    63. }
    64.  
    65. // SERVER FUNCTIONS
    66.  
    67. ////////////////////////////////////////////////////////////////////////
    68. // CLIENT FUNCTIONS //
    69.  
    70. // getServer() - Establishes a connection to the server
    71. ////////////////////////////////////////////////////////////////////////
    72. void MainWindow::getServer(QString ip, quint16 port)
    73. {
    74. blockSize = 0;
    75. tcpClient.connectToHost(QHostAddress(ip), port);
    76. }
    77.  
    78. // readSpeed() - Reads the slider speed value into a label
    79. ////////////////////////////////////////////////////////////////////////
    80. void MainWindow::readSpeed()
    81. {
    82. QDataStream in(&tcpClient);
    83. in.setVersion(QDataStream::Qt_4_0);
    84.  
    85. quint8 sliderValue;
    86. in >> sliderValue;
    87. label->setNum(sliderValue);
    88. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Reading from TCP Socket crashes program

    Could you post the backtrace?

  3. #3

    Default Re: Reading from TCP Socket crashes program

    Sure! Lemme just figure out how to make MinGW do that

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

    Default Re: Reading from TCP Socket crashes program

    Quote Originally Posted by OnionRingOfDoom
    Sure! Lemme just figure out how to make MinGW do that
    You need gdb for this.

    C:\...\gdb app.exe
    (gdb) run
    <program crashes>
    (gdb) bt
    <backtrace>
    (gdb) quit

  5. #5

    Default Re: Reading from TCP Socket crashes program

    Ok, lemme install this doohicky
    Last edited by OnionRingOfDoom; 26th January 2006 at 21:38.

  6. #6

    Default Re: Reading from TCP Socket crashes program

    #0 0x10047934 in _size_of_stack_reserve__ ()
    Cannot access memory at address 0x200000

    that's what it gave me.

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

    Default Re: Reading from TCP Socket crashes program

    Quote Originally Posted by OnionRingOfDoom
    #0 0x10047934 in _size_of_stack_reserve__ ()
    Cannot access memory at address 0x200000

    that's what it gave me.
    Only this? Maybe there was something more?

    If there is no main() in the backtrace, it might mean that there is some problem with libraries. Make sure you have "QT += network" in your .pro file.

  8. #8

    Default Re: Reading from TCP Socket crashes program

    Quote Originally Posted by jacek
    If there is no main() in the backtrace, it might mean that there is some problem with libraries. Make sure you have "QT += network" in your .pro file.
    Yep, QT += network" is in the .pro file.

    If you want, I can upload the entire source somewhere, and you can see what it does on your end?

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

    Default Re: Reading from TCP Socket crashes program

    Quote Originally Posted by OnionRingOfDoom
    If you want, I can upload the entire source somewhere, and you can see what it does on your end?
    According to Murphy's law it will work on my system

    Does your program crash before it starts?

  10. #10

    Default Re: Reading from TCP Socket crashes program

    Quote Originally Posted by jacek
    According to Murphy's law it will work on my system

    Does your program crash before it starts?
    Nope, I have to start up the program in server mode, then start another instance of the program in client mode. As soon as I click OK on the connect dialog box after typing in the propper port and IP, the server indicates that the client has sucessfully connected, and then the client crashes.
    Last edited by OnionRingOfDoom; 26th January 2006 at 22:35.

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

    Default Re: Reading from TCP Socket crashes program

    Quote Originally Posted by OnionRingOfDoom
    and then the client crashes.
    And there was only one entry in the backtrace? Did you compile your application in debug mode?

    Anyway, you try to read data from the socket before they arrive --- you must wait for readyRead() signal.

  12. #12

    Default Re: Reading from TCP Socket crashes program

    ...How do I compile it in debug mode?

Similar Threads

  1. wrong connection? program crashes altough it compiles
    By cbarmpar in forum Qt Programming
    Replies: 7
    Last Post: 30th September 2008, 13:48
  2. Socket Reading Advice
    By tntcoda in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2008, 12:26
  3. Program crashes with assert error in xcb_lock.c
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2007, 20:56
  4. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 21:53
  5. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 05: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
  •  
Qt is a trademark of The Qt Company.