Results 1 to 18 of 18

Thread: QTcpSocket connection problem

  1. #1
    valdemar593 Guest

    Default QTcpSocket connection problem

    Hello everyone. I'm trying to write a chat using QTcpSocket and QTcpServer, but strange things are happening. In the first example QTcpServer emits newConnection(), but QTcpSocket remains in connecting state without emiting error(...). In the second example I receive SIGSEGV when trying connectToHost() . What is more, FortuneClient/Server (single threaded) example runs just fine. In both examples network sessiong is not required. Being discouraged I examined the Fortune code and discovored that it is just the same concerning establishing connection. Here are both examples.

    FIRST

    Qt Code:
    1. //CLIENT
    2. ChatClient::ChatClient(QObject *parent)
    3. : QObject(parent) {
    4. tcpSocket = new QTcpSocket(this);
    5. QNetworkConfigurationManager manager;
    6. if (QNetworkConfigurationManager::NetworkSessionRequired
    7. & manager.capabilities()) {
    8. qDebug() << "Network session required";
    9. }
    10. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    11. this, SLOT(error(QAbstractSocket::SocketError)));
    12. connect(tcpSocket, SIGNAL(connected()),
    13. this, SLOT(requestForID()));
    14. connect(tcpSocket, SIGNAL(readyRead()),
    15. this, SLOT(receiveMessage()));
    16. tcpSocket->connectToHost("192.168.0.100", PORT);
    17. }
    18. ...
    19. void ChatClient::error(QAbstractSocket::SocketError error) {
    20. qDebug() << "Socket error" << error;
    21. }
    22.  
    23. void ChatClient::requestForID() {
    24. qDebug() << "Connected, requesting for ID";
    25. QByteArray segment;
    26. QDataStream out(&segment, QIODevice::WriteOnly);
    27. out.setVersion(QDataStream::Qt_4_7);
    28. out << (quint16)0 << ID;
    29. out.device()->seek(0);
    30. out << (quint16)(segment.size() - sizeof(quint16));
    31. tcpSocket->write(segment);
    32. }
    33.  
    34. //SERVER
    35. ChatServer::ChatServer(QObject *parent)
    36. : QObject(parent) {
    37. tcpServer = new QTcpServer(this);
    38. if (!tcpServer->listen(/*QHostAddress::Any, PORT*/)) {
    39. qDebug() << "Unable to start the server"
    40. << tcpServer->errorString();
    41. }
    42. qDebug() << "Server port" << tcpServer->serverPort();
    43. connect(tcpServer, SIGNAL(newConnection()),
    44. this, SLOT(processConnection()));
    45. }
    46.  
    47. void ChatServer::processConnection() {
    48. qDebug() << "Incoming connection";
    49. QTcpSocket *clientSocket = tcpServer->nextPendingConnection();
    50. /*connect(clientSocket, SIGNAL(readyRead()),
    51.   this, SLOT(readData()));
    52.   readData(clientSocket);
    53.   connect(clientSocket, SIGNAL(disconnected()),
    54.   clientSocket, SLOT(deleteLater()));*/
    55. QByteArray segment;
    56. QDataStream out(&segment, QIODevice::WriteOnly);
    57. out.setVersion(QDataStream::Qt_4_7);
    58. out << (quint16)0 << (quint16)Message
    59. << "Successfully connected";
    60. out.device()->seek(0);
    61. out << (quint16)(segment.size() - sizeof(quint16));
    62. clientSocket->write(segment);
    63. clientSocket->disconnectFromHost();
    64. }
    To copy to clipboard, switch view to plain text mode 

    SECOND

    Qt Code:
    1. //CLIENT
    2. Client::Client() {
    3. QTcpSocket *clientSocket = new QTcpSocket(this);
    4. connect(clientSocket, SIGNAL(connected()),
    5. this, SLOT(connectionEstablished()));
    6. connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    7. this, SLOT(error()));
    8. }
    9.  
    10. void Client::establishConnection() {
    11. // throws SIGSEGV
    12. clientSocket->connectToHost(QHostAddress("192.168.0.100"), 6178);
    13. }
    14.  
    15. void Client::connectionEstablished() {
    16. qDebug() << "Connection established";
    17. }
    18.  
    19. void Client::error() {
    20. qDebug() << clientSocket->errorString();
    21. }
    22.  
    23. //SERVER
    24. Server::Server() {
    25. QNetworkConfigurationManager manager;
    26. if (manager.capabilities() &
    27. QNetworkConfigurationManager::NetworkSessionRequired) {
    28. qDebug() << "Network session required";
    29. } else {
    30. sessionOpened();
    31. }
    32. connect(server, SIGNAL(newConnection()),
    33. this, SLOT(processConnection()));
    34. }
    35.  
    36. void Server::processConnection() {
    37. qDebug() << "Incoming connection";
    38. }
    39.  
    40. void Server::sessionOpened() {
    41. server = new QTcpServer(this);
    42. if (!server->listen(QHostAddress::Any, 6178)) {
    43. qDebug() << QString("Unable to start the server: %1")
    44. .arg(server->errorString());
    45. return;
    46. }
    47. qDebug() << "The server is running...";
    48. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas?
    Last edited by valdemar593; 27th May 2011 at 15:51.

  2. #2
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket connection problem

    Use QThread and Mutex. I solved with them. I want to send it but my code is C based.

  3. #3
    valdemar593 Guest

    Default Re: QTcpSocket connection problem

    Please explain why I should use QMutex and QThread in those examples? Should I implement tcp connection in a seperate thread?

  4. #4
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket connection problem

    Oh.. its just my opinion
    No not. Just inherit your class from QThread like this

    Qt Code:
    1. class logthread : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit logthread(QObject *parent = 0);
    6. ~logthread();
    7. ..........
    To copy to clipboard, switch view to plain text mode 

    and use mutex only when your application sends data

    Qt Code:
    1. QMutex mutex;
    2. ...........
    3. .........
    4. .........
    5. mutex.lock();
    6. send_function();
    7. mutex.unlock();
    To copy to clipboard, switch view to plain text mode 

    Its a very basic example but my code is running clearly. For more information just read the docs.

  5. #5
    valdemar593 Guest

    Default Re: QTcpSocket connection problem

    Thanks I'll try right now. Should I lock tcpSocket->connectToHost()? But why FortuneClient/Server doesn't use mutex and doesn't inherit QThread and works fine?

  6. #6
    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: QTcpSocket connection problem

    There is no reason to use threads and mutexes here.
    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.


  7. #7
    valdemar593 Guest

    Default Re: QTcpSocket connection problem

    Yes that hasn't solved the problem. But still I'm confused because Fortune example works and my doesn't. At the same time I do not find differencies except that Fortune iterates over network interfaces to choose one and I choose manually. Even then both IP addresses are the same. May be I should post the full code? It is not long.

  8. #8
    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: QTcpSocket connection problem

    Post the debugger backtrace after a crash.
    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.


  9. #9
    valdemar593 Guest

    Default Re: QTcpSocket connection problem

    //
    int idx = obj->metaObject()->indexOfMethod(sig.constData());
    //
    return invokeMethod(obj, member, type, QGenericReturnArgument(), val0, val1, val2, val3, val4, val5, val6, val7, val8, val9);
    //
    QMetaObject::invokeMethod(this, "connectToHostImplementation",
    Qt:: DirectConnection,
    Q_ARG(QString, hostName),
    Q_ARG(quint16, port),
    Q_ARG(OpenMode, openMode));
    //
    connectToHost(address.toString(), port, openMode);
    //
    clientSocket->connectToHost(QHostAddress("192.168.0.100"), 6178);

    please note that I talk about two different projets, where in the first one QTcpSocket remains in connecting state while in the second QTcpSocket::connectToHost() produces SIGSEGV.
    Last edited by valdemar593; 27th May 2011 at 16:37.

  10. #10
    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: QTcpSocket connection problem

    Please post the backtrace as requested.
    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.


  11. #11
    valdemar593 Guest

    Default Re: QTcpSocket connection problem

    Sorry I misunderstand what is backtrace?

  12. #12
    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: QTcpSocket connection problem

    http://en.wikipedia.org/wiki/Call_stack

    If you are using Qt Creator, the backtrace is in the "stack" view when you're in the Debug mode.
    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.


  13. #13
    valdemar593 Guest

    Post Re: QTcpSocket connection problem

    gdb

    text Code:
    1. #0 0x00402b73 in QMetaObject::invokeMethod (obj=0x7e9ff4,
    2. member=0x22a33f "connectToHostImplementation", type=Qt::DirectConnection,
    3. ret=..., val0=..., val1=..., val2=..., val3=..., val4=..., val5=..., val6=...,
    4. val7=..., val8=..., val9=...)
    5. at /var/tmp/qt-src/src/corelib/kernel/qmetaobject.cpp:1135
    6. #1 0x001efe73 in invokeMethod (this=0x7e9ff4, hostName=..., port=6178, openMode=...)
    7. at ../../include/QtCore/../../src/corelib/kernel/qobjectdefs.h:408
    8. #2 QAbstractSocket::connectToHost (this=0x7e9ff4, hostName=..., port=6178,
    9. openMode=...) at /var/tmp/qt-src/src/network/socket/qabstractsocket.cpp:1304
    10. #3 0x001f1835 in QAbstractSocket::connectToHost (this=0x7e9ff4, address=...,
    11. port=6178, openMode=...)
    12. at /var/tmp/qt-src/src/network/socket/qabstractsocket.cpp:1417
    13. #4 0x080494e4 in Client::establishConnection (this=0xbffff1fc)
    14. at ../NetTestClient/client.cpp:13
    15. #5 0x08049201 in main (argc=1, argv=0xbffff2d4) at ../NetTestClient/main.cpp:8
    To copy to clipboard, switch view to plain text mode 

    qt creator

    0 QHashData::detach_helper2 qhash.cpp 206 0x001a18fa
    1 QHash<QObject*, QHash<QEvent::Type, int> >::detach_helper qhash.h 582 0x002e69eb
    2 detach qhash.h 299 0x002dbc23
    3 operator[] qhash.h 737 0x002dbc23
    4 QStateMachinePrivate::registerEventTransition qstatemachine.cpp 1598 0x002dbc23
    5 invokeMethod qobjectdefs.h 408 0x00a25e73
    6 QAbstractSocket::connectToHost qabstractsocket.cpp 1304 0x00a25e73
    7 QAbstractSocket::connectToHost qabstractsocket.cpp 1417 0x00a27835
    8 Client::establishConnection client.cpp 13 0x080494e4
    9 main main.cpp 8 0x08049201

    Last edited by wysota; 27th May 2011 at 17:12.

  14. #14
    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: QTcpSocket connection problem

    What does the main() function look like?
    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.


  15. #15
    valdemar593 Guest

    Default Re: QTcpSocket connection problem

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4. Client client;
    5. client.establishConnection();
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

  16. #16
    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: QTcpSocket connection problem

    Ah.... I missed it. You are creating a local variable called "clientSocket" in your constructor that shadows the class member called "clientSocket" thus it remains uninitialized and a dangling pointer causes a crash.
    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.


  17. #17
    valdemar593 Guest

    Default Re: QTcpSocket connection problem

    Oh my god =))
    Thanks a lot man!
    But what about the first example? Everything seems to be correct there, but the socket stays in connecting state all the time while the server emits newConnection()?

  18. #18
    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: QTcpSocket connection problem

    I don't know, I'd need to see code that's more complete than this.
    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. Problem with connection
    By Adonitos in forum Qt Programming
    Replies: 4
    Last Post: 8th April 2011, 11:56
  2. Replies: 1
    Last Post: 15th November 2010, 11:36
  3. DB Connection Problem
    By ktmdwn in forum Qt Programming
    Replies: 13
    Last Post: 4th August 2010, 15:12
  4. SQL Connection Problem
    By Utku in forum Installation and Deployment
    Replies: 3
    Last Post: 25th October 2009, 13:21
  5. QTcpsocket - connection problem
    By vishesh in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2007, 17:03

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.