Results 1 to 6 of 6

Thread: problem with Tcp socket reception in a thread...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with Tcp socket reception in a thread...

    Hi! I have quite similar problem with Qthread.

    Everything is ok until I unlink the Ethernet cable. And Try reconnect when connection error signel recived.


    from console:
    socket: QObject(0x0)
    socket: QTcpSocket(0xafb02498)
    Connecting to: "192.168.1.5"
    resize event
    "Ppm2_window" OGOLNY
    show event
    MODEL:x: false
    "Ppm2_window" OGOLNY
    console request
    permision to send data: true
    Socket state: QAbstractSocket::HostLookupState
    Socket state: QAbstractSocket::ConnectingState
    Not connected: true
    sock open: false
    Socket state: QAbstractSocket::UnconnectedState
    sock open: false
    Socket error: "Unknown error"
    Socket error: QAbstractSocket::SocketTimeoutError

    Connecting to: "192.168.1.5"
    Socket state: QAbstractSocket::HostLookupState
    Socket state: QAbstractSocket::ConnectingState
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0xafb02498), parent's thread is TcpClient(0x8473da0), current thread is QThread(0x82f66a8)
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QTcpSocket(0xafb02498), parent's thread is TcpClient(0x8473da0), current thread is QThread(0x82f66a8)
    Socket state: QAbstractSocket::UnconnectedState
    sock open: true
    Socket error: "Host unreachable"
    Socket error: QAbstractSocket::NetworkError
    Network error: "Host unreachable"
    Not connected: true
    sock open: false
    Socket state: QAbstractSocket::UnconnectedState
    sock open: false
    My Qestion is. Why it works like that? I have no idea how to solve this problem

    Qt Code:
    1. #ifndef TCPCLIENT_H
    2. #define TCPCLIENT_H
    3.  
    4. #include <QThread>
    5. #include <QTcpSocket>
    6. #include <QAbstractSocket>
    7. #include <QObject>
    8. #include <QDebug>
    9. #include <QTime>
    10. #include "globalne.h"
    11.  
    12. class TcpClient : public QThread
    13. {
    14. Q_OBJECT
    15.  
    16.  
    17.  
    18. public:
    19.  
    20. explicit TcpClient(QObject *parent = 0);
    21. ~TcpClient();
    22. void run();
    23. bool StartConnection();
    24. void zakoncz_polaczenie_z_e1000();
    25.  
    26. signals:
    27. void dataReady(QByteArray, QTime);
    28. void connectionError();
    29.  
    30. public slots:
    31. void readyRead();
    32. void disconnected();
    33. void connected();
    34. void stateChanged(QAbstractSocket::SocketState);
    35. void displayError(QAbstractSocket::SocketError);
    36. // void bytesWritten (qint64 bytes);
    37.  
    38.  
    39. private:
    40.  
    41. // CanMon canmnon_parent;
    42. // QTcpSocket socket;
    43. QTcpSocket *socket;
    44. QTime time;
    45. // QStringList lista;
    46. bool ponawiaj;
    47.  
    48.  
    49.  
    50. };
    51.  
    52. #endif // TCPCLIENT_H
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. //#include "tcpclient.h"
    2.  
    3. //TcpClient::TcpClient(QObject *parent) :
    4. // QObject(parent)
    5. //{
    6. //}
    7.  
    8. #include <QStringList>
    9. #include "tcpclient.h"
    10.  
    11. #include <QFile>
    12. #include <QTextStream>
    13.  
    14.  
    15. TcpClient::TcpClient(QObject *parent) :
    16. QThread(parent)
    17. {
    18.  
    19. socket = NULL;
    20. qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
    21. qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
    22. ponawiaj = true;
    23.  
    24. }
    25.  
    26. TcpClient::~TcpClient()
    27. {
    28. // qDebug() << "p";
    29.  
    30.  
    31. disconnect(socket,SIGNAL(connected()), this, SLOT(connected()));
    32. disconnect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    33. disconnect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
    34. // disconnect(socket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),this,SLOT(stateChanged(qSockState)));
    35. socket->close();
    36. qDebug() << "socket open: " << socket->isOpen();
    37. delete socket;
    38. }
    39.  
    40. void TcpClient::stateChanged(QAbstractSocket::SocketState st)
    41. {
    42. qDebug() << "Socket state: " << st;
    43. switch(st)
    44. {
    45. case QAbstractSocket::UnconnectedState:
    46. qDebug() << "sock open: " << socket->isOpen();
    47. break;
    48. }
    49. }
    50.  
    51. void TcpClient::displayError(QAbstractSocket::SocketError se)
    52. {
    53. qDebug() << "Socket error: " << socket->errorString();
    54. qDebug() << "Socket error: " << se;
    55.  
    56. switch(se)
    57. {
    58. case QAbstractSocket::SocketTimeoutError:
    59. StartConnection();
    60. break;
    61. case QAbstractSocket::NetworkError:
    62. qDebug() << "Network error: " << socket->errorString();
    63. break;
    64.  
    65. }
    66.  
    67. // this->StartConnection();
    68. }
    69.  
    70. bool TcpClient::StartConnection()
    71. {
    72. #define z_pliku 0
    73.  
    74. #if z_pliku
    75. QFile mFile("iphosta.cfg");
    76. if (!mFile.open(QFile::ReadOnly | QFile::Text))
    77. {
    78. qDebug() << mFile.errorString();
    79. qDebug() << "nie otworzono pliku... próba połączenia z localhost";
    80. socket->connectToHost("localhost",4010);
    81. }
    82. else
    83. {
    84. QTextStream in(&mFile);
    85. QString host = in.readAll();
    86. mFile.close();
    87. qDebug() << "Connecting to: " << host;
    88. socket->connectToHost(host,4010);
    89. }
    90. #else
    91. QString host = "192.168.1.5";
    92. // QString host = "192.168.1.48";
    93. qDebug() << "Connecting to: " << host;
    94.  
    95. socket->connectToHost(host,4010);
    96. #endif
    97.  
    98. if(socket->waitForConnected(5000))
    99. {
    100. qDebug() << "Connected: " << socket->isOpen();
    101.  
    102. }
    103. else
    104. {
    105. qDebug() << "Not connected: " << socket->isOpen();
    106. socket->close();
    107. }
    108.  
    109. qDebug() << "sock open: " << socket->isOpen();
    110.  
    111. return socket->isOpen();
    112. }
    113.  
    114. void TcpClient::zakoncz_polaczenie_z_e1000()
    115. {
    116. int result;
    117. if (socket->state())
    118. {
    119. result = socket->write(QByteArray::fromHex(QString("e1fc00000000000000000000000000000000000000000000000000f7e000e2").toAscii()));
    120. qDebug() << "nadano koniec polaczenia: " << result;
    121. this->msleep(500);
    122. qDebug() << "minely 2 sekundy";
    123. }
    124. ponawiaj = false;
    125. }
    126.  
    127. void TcpClient::run()
    128. {
    129. qDebug() << "socket: " << socket;
    130. if(!socket)
    131. socket = new QTcpSocket();
    132. qDebug() << "socket: " << socket;
    133.  
    134. connect(socket,SIGNAL(connected()), this, SLOT(connected()));
    135. connect(socket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    136. connect(socket,SIGNAL(disconnected()), this, SLOT(disconnected()));
    137. connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));
    138. connect(socket,SIGNAL( stateChanged(QAbstractSocket::SocketState)),this, SLOT(stateChanged(QAbstractSocket::SocketState)));
    139. // error(QAbstractSocket::SocketError)
    140. // SIGNAL(stateChanged(qSockState)),this,SLOT(tcStateChanged(qSockState)));
    141.  
    142. this->StartConnection();
    143. this->exec();
    144.  
    145. }
    146.  
    147. void TcpClient::connected()
    148. {
    149. qDebug() << "connection established: " << socket->isOpen();
    150. }
    151.  
    152. void TcpClient::disconnected()
    153. {
    154. qDebug() << " Disconnected";
    155. }
    156.  
    157. void TcpClient::readyRead()
    158. {
    159.  
    160. QByteArray data;
    161.  
    162. // qDebug() << "Redy read ";
    163.  
    164. if (socket->bytesAvailable() > 0)
    165. {
    166. // qDebug() << "dostepne bajty: " << socket->bytesAvailable();
    167.  
    168. data = socket->readAll();
    169. // qDebug() << "odebrano: " << data.toHex();
    170. time.start();
    171. // qDebug() << "TIME: " << time.toString() << "." << time.msec();
    172. emit dataReady(data, time);
    173. }
    174. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: problem with Tcp socket reception in a thread...

    That looks weird quite wrong. All your slots are being executed in the main thread (cross thread signal/slot connections, receiver object "thsi" belongs to main thread).

    Cheers,
    _

Similar Threads

  1. Socket + thread problem
    By Daxos in forum Qt Programming
    Replies: 11
    Last Post: 13th July 2010, 12:28
  2. socket & thread
    By prashant in forum Qt Programming
    Replies: 2
    Last Post: 2nd December 2009, 11:09
  3. thread with socket?
    By triperzonak in forum Qt Programming
    Replies: 6
    Last Post: 25th September 2008, 17:21
  4. Socket on a Thread
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2008, 16:56
  5. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 17:48

Tags for this Thread

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.