TCP socket connection between dart and Qt/C++ close connection, remove old QtcpSocket object

385 Views Asked by At

My server is qt and for accept client I use QTcpSocket and setSocketDescriptor, if I want to close socket or any problem that causes the server to disconnect, dart client don't get any notify from server, I will only notice when I start sending data to the server,

Methods not working

socket->close();
socket->aboutToClose();
socket->disconnectFromHost();

dart side

Socket.connect(_serverIp, _serverPort).then((Socket sock) {
        _wSock = sock;
        _wSock.encoding = utf8;
        _wSock
            .transform(StreamTransformer.fromHandlers(
          handleData: _bufferHandle.handleSocketDataMessage,
        ))
            .listen(sendingSocketData, onError: (e) {
          print("onError: " + e.toString());
        }, onDone: () async {
          print("socket done");
        });
      }).then((value) async {
        _wSock.write(startHandShake)
      });

my next problem

In qt and thread I create socket object in heap and set SocketDescriptor

void HandShakeThread::run(){
    socket = new QTcpSocket();
    if(!socket->setSocketDescriptor(socketId)){
        emit error(socket->error());
        _isRunning = false;
    }
   exec();
   return;
}

if I want to move a socket to another class and thread I should be to send setSocketDescriptor to another thread and create new heap object and set setSocketDescriptor to listen, if I want to delete old heap QTcpsocket object, my current QTcpsocket disccounected and setSocketDescriptor don't work any more, how to delete old heap QTcpSocket object?

1

There are 1 best solutions below

0
Ali Forouzan On

you can use from errorOccurred slot on your client side.

slots:
void errorOccurred(QAbstractSocket::SocketError error);

see the qt refrece for this slot.