Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: Reading from TCP Socket crashes program

  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
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    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
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    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 20: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
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    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
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    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 21:35.

  11. #11
    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: 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?

  13. #13
    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: Reading from TCP Socket crashes program

    Quote Originally Posted by OnionRingOfDoom
    ...How do I compile it in debug mode?
    Add "CONFIG += debug" to your .pro file (and remove "release" if it's there).

  14. #14

    Default Re: Reading from TCP Socket crashes program

    Ok so I changed the connection to do the readSpeed() function whenever readyRead() was emitted. However, now nothing happens when the client connects.

    I also added a new connection at the end of the acceptConnection() function, which is as follows: connect(slider, SIGNAL(valueChanged(int)), this, SLOT(sendSpeed()));
    so now, whenever I move the slider (and it outputs valueChanged signal), it crashes.
    Last edited by OnionRingOfDoom; 26th January 2006 at 22:08.

  15. #15

    Default Re: Reading from TCP Socket crashes program

    After further fiddling, it seems that it's not the server's writing to the IO device that's the problem, it's the client's reading from the IO device that's causing the crash. Moving the slider thus causes the server to write data, and the client tries to read it or something, but crashes.

  16. #16
    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: Reading from TCP Socket crashes program

    Does it crash if you change MainWindow::readSpeed() to:
    Qt Code:
    1. void MainWindow::readSpeed()
    2. {
    3. label->setNum(0);
    4. }
    To copy to clipboard, switch view to plain text mode 
    ?

    Did you try to generate that backtrace again?

  17. #17

    Default Re: Reading from TCP Socket crashes program

    yes, it still crashes, oddly enough...
    and I'm trying to compile in debug mode, but I have to fix all these errors switching to debug mode seems to have created.

  18. #18

    Default Re: Reading from TCP Socket crashes program

    Oh awesome! It's acting like it can't find the QtNetwork include file anymore...
    Any idea why adding that CONFIG thing to the .pro file would make it think it couldn't find QtNetwork, but could still find every other Qt file I included?
    Last edited by OnionRingOfDoom; 26th January 2006 at 23:07.

  19. #19
    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: Reading from TCP Socket crashes program

    Quote Originally Posted by OnionRingOfDoom
    yes, it still crashes, oddly enough...
    Is that label variable initialized?

    Quote Originally Posted by OnionRingOfDoom
    It's acting like it can't find the QtNetwork include file anymore...
    Can it find "QTcpSocket? Did you compile Qt in debug mode or maybe you have only release version?

  20. #20

    Default Re: Reading from TCP Socket crashes program

    First question: yes, it is initialised
    And I fixed the other problem, when I remade the .pro file, it didn't put in the
    QT += network thing.
    Last edited by OnionRingOfDoom; 27th January 2006 at 00:12.

Similar Threads

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