PDA

View Full Version : How to free for re-using a socketDescriptor



erqsor
27th December 2011, 18:10
Hello,

OK, title was hard to find, but here is my problem:
I'm using QTcpServer and QTcpSocket for creating higher-level classes.
When a connection is etablished, I need to send the socketDescriptor of a connected QTcpSocket (gived by the nextPendingConnection function of QTcpServer) to another class for creating a new socket instance.
I have no problems under windows, but I encounter some problems under Linux and I can't receive any data. I think the problem is from this:
We can read this in the Qt Documentation: Note: It is not possible to initialize two abstract sockets with the same native socket descriptor.
But nothing about how to "free" a socketDescriptor for using it in another class (or thread).

Jonny174
29th December 2011, 06:41
That if to transfer not a socket descriptor but pointer to QTcpSocket?

erqsor
29th December 2011, 10:40
Yes, but that's not thread-safe.
You can read this in the Qt documentation:

Note: If you want to handle an incoming connection as a new QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the QTcpSocket object there and use its setSocketDescriptor() method.
My class is using a QTcpServer, and I need to override the QTcpServer::incomingConnexion function with a socketDesecriptor of a socket that is already connected. But what I do on the socket is disconnecting it (delete, deleteLater, close, abort, ...). When we created a QTcpSocket and set to it a socketDescriptor, the socketDescriptor is becoming useless ?