Records of some problems in the Qtcpsocket use process

Source: Internet
Author: User
Tags thread class


At present, in the original C socket communication to use QT class Library Qtcpsocket communication, in the process of modification encountered a lot of problems, here will be recorded together, in case of later use.


The communication methods used: Qtimer timer, Qthread multithreading and Qtcpsocket communication. Qtimer Set the timer communication interval (10s), Qthread run the timer response function, qtcpsocket to send data.

1. Question one: Qtcpsocket object creation and use to be on the same thread, otherwise an error.

Description of the error: Qobject:cannot create children for a parent, in a different thread.

Problem Description: The beginning is to use the Qtcpsocket object as a member variable of the main interface, so initialize (new) within the constructor, and then connect the server side within the Qthread thread and send the data.

Problem Resolution: The initialization and connection are placed in the qthread, and the establishment of a long connection, and the use of a flag to indicate the status of the connection, before sending a signal to determine the location of the connection, in order to connect or send.


2. Question two: Qthread the Qtcpsocket object signal (connected, error, statechangeed) with slot function connection report data type not registered

Error description: Qobject::connect:cannot queue arguments of type ' qabstractsocket::socketerror ' (Make sure ' Qabstractsocket:: SocketError ' is registered using Qregistermetatype (). )

Problem Description: Connect automatic connection The signal in the thread class is the default queuing mode queuedconnection, so you need to register the parameter type.

Problem Resolution: Method One, the default connect to the connection to QT::D irectconnection, do not need type information; method Two, the Qregistermetatype parameter type is registered within the thread and is not currently tested.


3. Question three: Call Connecttohost to send data after establishing a connection failed

Error description: Qnativesocketengine::write () is not called in Qabstractsocket::connectedstate

Problem Description: Error is not calling the write () function in the Qtcpsocket connection state, that is, the connection is not established when calling write ()

Problem Solving: Method One, after calling Connecttohost, then call waitforconnected (timeout), assign a value to the delay parameter, and then call the Write () function;

Method Two, call the Write () function in the slot function of the signal connected to send the data;

Method Three, in the signal statechangeed slot function to judge the current Qtcpsocket object connection state (), if it is qabstractsocket::connectedstate, call the Write () function to send data;

The actual use of a connection flag bit to determine whether to call the write () function to send data, in the signal connected slot function to modify the flag bit is true, each timer expiration first judge the flag bit, for false then try to establish the connection again.


4. Question four: The service end is not open and the network cable is not connected error

Error description: Server does not open error: Qtcpsocket Connection refused, network cable no connection error: Network operation timed out

Problem Description: The server does not open the return connection is rejected, network cable is not connected will time out, this may be related to the internet situation ...

Problem solving: Not clear the specific reasons, the above phenomenon as a judgment to distinguish between the two methods.


5. Question five: Connection closure

Error description: Direct call disconnect, error message forgotten ...

Problem Description:

Problem Resolution: Instead of calling the Disconnectfromhost () function, this function waits for the associated operation of the connection (data sent) to complete before closing the connection.


6. Question six: keepalive

Error description: Do not send keepalive package

Problem Description: KeepAlive configuration for Qtcpsocket.socketdescriptor, invalid configuration prior to connection success

Problem Resolution: Replace in the connected signal slot function, and then modify the connection mark in the disconnected signal slot function. This can monitor the end of the abnormal disconnect, but the network cable is unplugged or not detected, this is a legacy problem.


7. Question Seven: heartbeat mechanism

To determine whether the connection is normal, consider using two methods:

Method One: Use the keepalive mechanism in problem six, send empty packets at idle time, receive the acknowledgement information to confirm the connection is normal;

Method Two: Use the heartbeat mechanism, the end of the time to send a heartbeat packet, and then receive the response to the end of the heartbeat packet, so as to confirm the normal connection, or send back after the end of the message to receive acknowledgement of the reply.

Both methods are used to confirm the connection through the packet, by contrast, the method is simple to configure, does not need to modify the End-to-end code, but the two sides to interact; method two needs to modify the server-side code (increase the return acknowledgement code at the data reception), but the method is less than one time to send packets to the end-to-end. (This end refers to the client, to the end of the service side)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.