Results 1 to 7 of 7

Thread: waitForReadyRead and disconnect

  1. #1
    Join Date
    Dec 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default waitForReadyRead and disconnect

    I have a Socket where I wait the data with the function waitForReadyRead (I can't use the singal ReadyRead).
    The problem is that I disconnect the network the application crash.
    Suggestions?

    Thanks
    Teo

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

    Default Re: waitForReadyRead and disconnect

    Show your code.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: waitForReadyRead and disconnect

    There is one thread to receive data:
    void ThreadInputFrameWorkEth::run(void)
    {
    char vectFrame[1024];
    QByteArray byteArrayFrame;
    CircularBuffer* lpInput = lpParent->GetInput();
    int nCount = 0;
    bExit = false;
    while (true)
    {
    //attende che ci siano dei dati. Nel caso di disconnesione o errore
    //la bExit viene messa a true anche quando si effettua sconnessione manuale (viene richiamata StopAndWait)
    lpParent->waitForReadyRead();
    if (bExit)
    {
    break;
    }
    ...
    ...
    From main thread I create the QTcpSocket and connect to the host

    lpSocketChild->connectToHost(data[1].toString(),0xBBBB);
    connect(lpSocketChild,SIGNAL(connected()),this,SLO T(Connected()));

    When is Connected to keep alive the connection (for windows)
    void LocalServer::Connected(void)
    {
    tcp_keepalive structSettings;
    int bOptLen = sizeof (BOOL);
    BOOL bOptVal = TRUE;
    int nSocketDesctriptor = lpSocketChild->socketDescriptor();
    DWORD nBytes = 0;
    structSettings.onoff = 1;
    structSettings.keepaliveinterval = 500;
    structSettings.keepalivetime = 1000;
    nRet = ::WSAIoctl(nSocketDesctriptor, SIO_KEEPALIVE_VALS,&structSettings,sizeof(structSe ttings),NULL,0,&nBytes,NULL,NULL ); // succeeds, r==0
    if (nRet == SOCKET_ERROR) //errori da gestire teo da fare forse
    {
    int nErr = WSAGetLastError();
    nRet = -1;
    }
    nRet = setsockopt(nSocketDesctriptor,SOL_SOCKET,SO_KEEPAL IVE,(char *) &bOptVal,bOptLen);

    if (nRet == SOCKET_ERROR)
    {
    int nErr = WSAGetLastError();
    nRet = -2;
    }
    int nVal = 0;
    setsockopt(nSocketDesctriptor, IPPROTO_IP, IP_DONTFRAGMENT,(char *) &nVal, sizeof(nVal));

    connect(lpSocketChild,SIGNAL(error(QAbstractSocket ::SocketError)),this,SLOT(Error(QAbstractSocket::S ocketError)));
    connect(lpSocketChild,SIGNAL(disconnected()),this, SLOT(DisconnectedSocket()));
    }

    void LocalServer::Error(QAbstractSocket::SocketError e)
    {
    ((SocketChild*)sender())->SetClosed(true);
    }
    //----------------------------------------------------------------------------

    void LocalServer:isconnected(void)
    {
    }
    //----------------------------------------------------------------------------

    void LocalServer:isconnectedSocket(void)
    {
    ((SocketChild*)sender())->SetClosed(true);
    }
    //----------------------------------------------------------------------------

    two problems:
    1) If I unplug the ethernet cable the application crash.
    2) I can't to stop the thread because even if I set from the volatile bool variable bExit to true, the thread is waiting data ("lpParent->waitForReadyRead() and doesn't control the bExit value if there are not data on the network.


    Suggestions?

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

    Default Re: waitForReadyRead and disconnect

    Quote Originally Posted by matteo.ceruti View Post
    1) If I unplug the ethernet cable the application crash.
    Where does it crash? Show the backtrace.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2011
    Posts
    1,245
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: waitForReadyRead and disconnect

    and don't forget code tags
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. #6
    Join Date
    Dec 2011
    Posts
    19
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: waitForReadyRead and disconnect

    Qt Code:
    1. //************* PROVASOCKET.H ***********************
    2. #ifndef PROVASOCKET_H
    3. #define PROVASOCKET_H
    4.  
    5. #include <QtGui/QMainWindow>
    6. #include <QAbstractSocket>
    7. #include "ui_provasocket.h"
    8.  
    9. class provasocket : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. provasocket(QWidget *parent = 0, Qt::WFlags flags = 0);
    15. ~provasocket();
    16.  
    17. private:
    18. Ui::provasocketClass ui;
    19. private slots:
    20. void Connected(void);
    21. void Error(QAbstractSocket::SocketError e);
    22. void Disconnected(void);
    23. };
    24.  
    25. #endif // PROVASOCKET_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //************* PROVASOCKET.CPP ***********************
    2.  
    3.  
    4. #include "provasocket.h"
    5. #include <QTcpSocket>
    6. #include <QDebug>
    7. #include <winsock2.h>
    8. #include <mstcpip.h>
    9. #include <ws2tcpip.h>
    10. #include <ws2ipdef.h>
    11. #include <QApplication>
    12. #include "threadread.h"
    13. #pragma comment(lib, "Ws2_32.lib")
    14.  
    15. QTcpSocket* lpSocket;
    16. ThreadRead* lpThreadRead;
    17. provasocket::provasocket(QWidget *parent, Qt::WFlags flags)
    18. : QMainWindow(parent, flags)
    19. {
    20. ui.setupUi(this);
    21. lpSocket = new QTcpSocket();
    22. lpThreadRead = new ThreadRead();
    23. connect(lpSocket,SIGNAL(connected()),this,SLOT(Connected()));
    24. lpSocket->connectToHost("192.168.15.111",0xbbbb);
    25. }
    26.  
    27. provasocket::~provasocket()
    28. {
    29. delete lpSocket;
    30. }
    31.  
    32. void provasocket::Connected(void)
    33. {
    34. tcp_keepalive structSettings;
    35. int bOptLen = sizeof (BOOL);
    36. BOOL bOptVal = TRUE;
    37. int nSocketDesctriptor = lpSocket->socketDescriptor();
    38. DWORD nBytes = 0;
    39. structSettings.onoff = 1;
    40. structSettings.keepaliveinterval = 500;
    41. structSettings.keepalivetime = 1000;
    42. int nRet = ::WSAIoctl(nSocketDesctriptor, SIO_KEEPALIVE_VALS,&structSettings,sizeof(structSettings),NULL,0,&nBytes,NULL,NULL ); // succeeds, r==0
    43. if (nRet == SOCKET_ERROR) //errori da gestire teo da fare forse
    44. {
    45. int nErr = WSAGetLastError();
    46. nRet = -1;
    47. }
    48. nRet = setsockopt(nSocketDesctriptor,SOL_SOCKET,SO_KEEPALIVE,(char *) &bOptVal,bOptLen);
    49.  
    50. if (nRet == SOCKET_ERROR)
    51. {
    52. int nErr = WSAGetLastError();
    53. nRet = -2;
    54. }
    55. int nVal = 0;
    56. setsockopt(nSocketDesctriptor, IPPROTO_IP, IP_DONTFRAGMENT,(char *) &nVal, sizeof(nVal));
    57. connect(lpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(Error(QAbstractSocket::SocketError)));
    58. connect(lpSocket,SIGNAL(disconnected()),this,SLOT(Disconnected()));
    59. lpThreadRead->start();
    60. }
    61. //----------------------------------------------------------------------------
    62.  
    63. void provasocket::Error(QAbstractSocket::SocketError e)
    64. {
    65. qDebug() << "Error";
    66. lpSocket->disconnectFromHost();
    67. }
    68. //----------------------------------------------------------------------------
    69.  
    70. void provasocket::Disconnected(void)
    71. {
    72. qDebug() << "Disconnected";
    73. }
    74. //----------------------------------------------------------------------------
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. //************* PROVASOCKET.H ***********************
    2. #ifndef THREADREAD_H
    3. #define THREADREAD_H
    4.  
    5. #include <QThread>
    6.  
    7. class ThreadRead : public QThread
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. ThreadRead();
    13. ~ThreadRead();
    14. volatile bool bExit;
    15.  
    16. protected:
    17. void run(void);
    18. };
    19.  
    20. #endif // THREADREAD_H
    21.  
    22.  
    23. //************* PROVASOCKET.CPP ***********************
    24.  
    25. #include "threadread.h"
    26. #include <QTcpSocket>
    27. #include <QApplication>
    28. extern QTcpSocket* lpSocket;
    29.  
    30. ThreadRead::ThreadRead()
    31. : QThread()
    32. {
    33.  
    34. }
    35.  
    36. ThreadRead::~ThreadRead()
    37. {
    38.  
    39. }
    40.  
    41. void ThreadRead::run(void)
    42. {
    43. bExit = false;
    44. while (!bExit)
    45. {
    46. while (lpSocket->waitForReadyRead(5000))
    47. {
    48. if (bExit)
    49. {
    50. return;
    51. }
    52. QApplication::processEvents();
    53. }
    54. }
    55. }
    To copy to clipboard, switch view to plain text mode 


    If I unplug the ethrnet cable the next

    Qt Code:
    1. while (lpSocket->waitForReadyRead(5000))
    To copy to clipboard, switch view to plain text mode 

    crash.

    Thanks
    Teo
    Last edited by wysota; 27th November 2012 at 19:28. Reason: missing [code] tags

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

    Default Re: waitForReadyRead and disconnect

    Show the backtrace please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to get out from waitForReadyRead(Timeout) blocking?
    By qmonkey in forum Qt Programming
    Replies: 6
    Last Post: 16th February 2011, 19:26
  2. QextSerialPort and waitForReadyRead problem
    By Ivan Wagner in forum Qt Programming
    Replies: 12
    Last Post: 20th August 2010, 07:52
  3. qextserialport waitForReadyRead and timeOut
    By adamatic in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2009, 09:52
  4. QUdp and waitForReadyRead
    By babu198649 in forum Newbie
    Replies: 5
    Last Post: 22nd December 2008, 18:49
  5. QextSerialPort WaitForReadyRead()
    By kingslee in forum Qt Programming
    Replies: 9
    Last Post: 9th October 2008, 19:44

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.