Results 1 to 18 of 18

Thread: TCP Socket not connecting on Release

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default TCP Socket not connecting on Release

    Hi,

    I'm using a QTcpSocket that tryies to connect to a client. The code is working when the application is compiled on Debug mode but when I try using Release mode, I recive this error "QAbstractSocket::RemoteHostClosedError".

    Any idea what is happenging?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    Tryied to create another client application and, sending this(char[])
    Qt Code:
    1. char data[12];
    2. data[0] = 0;
    3. data[1] = 0;
    4. data[2] = 0;
    5. data[3] = 0;
    6. data[4] = 0;
    7. data[5] = 6;
    8. data[6] = 1;
    9. data[7] = 3;
    10. data[8] = 0;
    11. data[9] = 0;
    12. data[10] = 0;
    13. data[11] = 1;
    To copy to clipboard, switch view to plain text mode 

    I reccive:

    Qt Code:
    1. char data[12];
    2. data[0] = 0;
    3. data[1] = 0;
    4. data[2] = 0;
    5. data[3] = 0;
    6. data[4] = 6; // !!
    7. data[5] = 6;
    8. data[6] = 1;
    9. data[7] = 3;
    10. data[8] = 0;
    11. data[9] = 0;
    12. data[10] = 1; //!!
    13. data[11] = 1;
    To copy to clipboard, switch view to plain text mode 

    This only happens when the sender app is on Release mode, but when using on Debug mode the recived data is correctly.

    Thanks,
    Òscar Llarch i Galán

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

    Default Re: TCP Socket not connecting on Release

    What code did you use for sending and receiving the data?

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    I'm using the "QIODevice::write" method.

    The socket is running on a working thread. Used the "threadedfortuneserver" as example to write this code:

    Qt Code:
    1. void myThread::run()
    2. {
    3. m_pqSocket = new QTcpSocket();
    4. bool bC2 = connect(m_pqSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(ErrorConexio(QAbstractSocket::SocketError)));
    5. m_pqSocket->connectToHost(m_qIPSlave,502,QIODevice::ReadWrite);
    6. if (m_pqSocket->waitForConnected(5000))
    7. {
    8. exec();
    9.  
    10. //When exiting the eventLoop:
    11. m_pqSocket->disconnectFromHost();
    12. if (m_pqSocket->waitForDisconnected(5000))
    13. delete (m_pqSocket);
    14. }
    15. else
    16. {
    17. //If not connected
    18. delete (m_pqSocket);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    When using Debug version, on console I'm getting this messages:
    Qt Code:
    1. QSocketNotifier: socket notifiers cannot be enabled from another thread
    2. QSocketNotifier: socket notifiers cannot be disabled from another thread
    To copy to clipboard, switch view to plain text mode 

    Thanks,
    Òscar Llarch i Galán

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

    Default Re: TCP Socket not connecting on Release

    What about the rest of the code? Could you paste the exact code used for reading and writing?

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    Sorry, I'm developing Modbus protocol and I have some layers. The layer that I show you is the CModbusTCPMaster, and the socket is delivered to the CModbusTCPTransport:

    I had simplified it because it's a bit large code.

    Qt Code:
    1. void CModbusTCPTransport::setSocket(QTcpSocket* pqSocket)
    2. {
    3. bool bC1 = connect(m_pqTCPSocket,SIGNAL(readyRead()),this,SLOT(readResponse()));
    4. bool bC2 = connect(m_pqTCPSocket,SIGNAL(disconnected()),this,SLOT(disconect()));
    5. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void CModbusTCPTransport::sendRequest(CModbusRequest* request)
    2. {
    3. int iLength = 8;
    4. char* pcRequest = new char[260];
    5. pcRequest[0] = request->getTransaction() >> 8;
    6. pcRequest[1] = request->getTransaction();
    7. pcRequest[2] = request->getProtocol() >> 8;
    8. pcRequest[3] = request->getProtocol();
    9. pcRequest[4] = request->getLength() >> 8;
    10. pcRequest[5] = request->getLength();
    11. pcRequest[6] = request->getUnitID();
    12.  
    13. pcRequest[7] = request->getFunctionCode();
    14.  
    15. m_pqTCPSocket->write(pcRequest,iLength);
    16. //Force the Request to be sent
    17. m_pqTCPSocket->flush();
    18. delete[] pcRequest;
    19. }
    20.  
    21. void CModbusTCPTransport::readResponse()
    22. {
    23. //Read the Modbus Header (8 bytes)
    24. qint64 bytesRead = m_pqTCPSocket->read(m_cbuffer,8);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Thanks,
    Òscar Llarch i Galán

  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    Creating a QObject inside "run()" method of the Thread, shows this error:
    Qt Code:
    1. void myThread::run()
    2. {
    3. ...
    4. QObject ob(this);
    5. }
    To copy to clipboard, switch view to plain text mode 

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is CModbusTCPMaster(02BCAAB0), parent's thread is QThread(019480E0), current thread is CModbusTCPMaster(02BCAAB0)

    I tryied this because when creating the socket, no parent is passed to it. But trying to use "this" as the socket parents gets me the same error.

    Thanks,
    Òscar Llarch i Galán

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    I have an idea how to solve this:
    -Create a QTcpSocket dervied class that has a SLOT "sendMessage(char*)"
    -My Thread emits a SIGNAL "readySend(char*)" that is connected to the SLOT of the QTcpSocket derived class on a QueuedConnection.

    The problem is that when calling "pqTcpSocket->write", this is done by myThread.

    Thanks,
    Òscar Llarch i Galán

  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    The QSocketNotifier messages have disapeared using this behaviour.

    The other problem that the server application is not accepting the connection is still getting me errors, but using a simple Qt server aplication it connects well. So, I really don't understand why the other server application is reaching the connection with "QAbstractSocket::RemoteHostClosedError" error code(note that is a third party app that I don't have the source code).

    And finally, the problem that the server recives a different message ... is still getting me a different message.

    Thanks,
    Òscar Llarch i Galán

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

    Default Re: TCP Socket not connecting on Release

    Quote Originally Posted by ^NyAw^ View Post
    But trying to use "this" as the socket parents gets me the same error
    Because the child and parent objects are created in different threads and this is forbidden. Don't try to pass a parent.

    Quote Originally Posted by ^NyAw^ View Post
    And finally, the problem that the server recives a different message ... is still getting me a different message.
    Are the sender and receiver machines of the same architecture (32/64b big/little endianness)?

    Could you check using a network sniffer whether the content "turns incorrect" on the client or on the server? In other words - what is being transmitted over the network? The data that is being sent or the one that is being received?

  11. #11
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    I'm using the same machine to use client and server apps. A Windows XP OS on a 32 bit machine and Qt 4.3.0.

    Wireshark sniffer is not able to capture packages from/to the same machine, so I open a QFile and write the message(increasing by 48 to convert int to char ona ASCII) into it.

    The data that the client is sending is wrong when using Release but it's okay when running on Debug.

    Thanks,
    Òscar Llarch i Galán

  12. #12
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Wink Re: TCP Socket not connecting on Release

    Ã’scar, I'm sure everybody would like to help, but if you want an accurate answer, you will have to isolate your problem, and post a standalone compilable example that shows the bug.

    Usually differences in behavior between debug and release in networking code are caused by either

    1) Timing problems (i.e., race conditions)
    * the order of packet arrival affects you program's logics

    2) Network packet fragmentation (i.e., partial packet arrival)
    * the size of packets received affects your program's behavior

    3) Compiler bugs
    * _usually_ triggered by complex code, try simplifying

    Generally though I'd try to simplify and simplify your code until you have something that's still broken, but can be posted to this forum for review :-).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  13. #13
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    1) Timing problems (i.e., race conditions)
    * the order of packet arrival affects you program's logics
    Really do you thing that I do different things on Debug and on Release?

    2) Network packet fragmentation (i.e., partial packet arrival)
    * the size of packets received affects your program's behavior
    The packet size is as maximum 20 bytes long. Dou you really thing that it is packed on different little packages? But, when Debugging the packages that I recive are ok.

    3) Compiler bugs
    * _usually_ triggered by complex code, try simplifying
    Simply I can't.

    Will try to reproduce the problem.
    Òscar Llarch i Galán

  14. #14
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    Could this code:
    Qt Code:
    1. quint16 qAdd = request->getRegisterAddress();
    2. pcRequest[8] = (char)(qAdd >> 8);
    3. pcRequest[9] = (char)qAdd;
    To copy to clipboard, switch view to plain text mode 


    Get different values on Debug and on Release?
    What I want is
    to get the MSB 8 bits of "qAdd" to pcRequest[8] and the LSB 8 bits to pcRequest[9].
    This is well done on Debug and not on Release?
    Òscar Llarch i Galán

  15. #15
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    You can add qDebugs to find out :-).
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  16. #16
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    I don't really know if you are jocking. Have your read that the problem is in Release mode? So "qDebug" will get me ... ah, nothing.

    The problem was that I used this code:
    Qt Code:
    1. pcRequest[0] = request->getTransaction() >> 8;
    2. pcRequest[1] = request->getTransaction();
    To copy to clipboard, switch view to plain text mode 
    That on Debug gets me this result(p.ex. "request->getTransaction()" returns 1):
    pcRequest[0] == 0;
    pcRequest[0] == 1;

    And returns me this on Release:
    pcRequest[0] == 1;
    pcRequest[0] == 1;

    The new code that I used is this:
    Qt Code:
    1. quint16 qTrans = request->getTransaction();
    2. pcRequest[0] = (char)(qTrans >> 8);
    3. pcRequest[1] = (char)qTrans;
    To copy to clipboard, switch view to plain text mode 

    So, the problem that reciving different data is solved. Problem of the compiler and a mine mishap.


    The other problem that the server reaches the connection is not solved yet. Any help will be usefull.

    Thanks,
    Òscar Llarch i Galán

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

    Default Re: TCP Socket not connecting on Release

    Quote Originally Posted by ^NyAw^ View Post
    So "qDebug" will get me ... ah, nothing.
    No, it won't. And you can always use std::cerr if you want

    The other problem that the server reaches the connection is not solved yet. Any help will be usefull.
    Try isolating the problem using a minimal compilable example that we could check ourselves. At least post the code used to setup the server and accept connections.

  18. #18
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: TCP Socket not connecting on Release

    Hi,

    Try isolating the problem using a minimal compilable example that we could check ourselves. At least post the code used to setup the server and accept connections.
    As I said, the server is a Third party app that I don't have the source. I've created a minimal server app using Qt and it accept the connection that the 3rd party app don't accept(only on release), so there could be a problem on the 3rd party app. I use this code into "run()" method:
    Qt Code:
    1. m_pqSocket->connectToHost(m_qIPSlave,502,QIODevice::ReadWrite);
    2. if (m_pqSocket->waitForConnected(5000))
    3. {
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 


    And I can see that it don't wait the 5 seconds to connect. In the moment that the socket tryies "waitForConnected" it returns inmediatly(only on release). So, maybe is there any bug on the 3rd party app network libs, but don't worry about it, I use this app to ensure that my Modbus protocol works fine and this is the only problem that I'm having.

    Thanks for your time,
    Òscar Llarch i Galán

Similar Threads

  1. Socket not connecting
    By ComaWhite in forum Qt Programming
    Replies: 14
    Last Post: 11th March 2008, 02:55
  2. Greenphone mini USB socket problem
    By izico in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 25th September 2007, 11:59

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.