I want to share the existing TCP socket from the main application to the second one on the device and continue communication with the client. Applications work in the Linux system. I found in QAbstractSocket documentation that I should bind the socket with mode QAbstractSocket::ShareAddress, but I can't achieve a working example. I tried to share the socket using a socket descriptor or create a QTcpSocket object with the IP address and port.
The demo implementation server-side
[First application]
Reimplemented incomingConnection on the server:
void InternalTcpServer::incomingConnection(qintptr socketDescriptor)
{
QTcpSocket *newSocket = new QTcpSocket();
newSocket->bind(QHostAddress("0.0.0.0"), 0 , QAbstractSocket::ShareAddress);
newSocket->setSocketDescriptor(socketDescriptor);
this->addPendingConnection(newSocket);
emit internalNewConnection();
}
The communication is correct and communication with client is possible.
[Second application]
Try opening the socket using socket descriptor in the second application on the device:
bool TransmitterServer::initSocket(qintptr socketDescriptor)
{
m_tcpSocket = new QTcpSocket();
m_tcpSocket->setSocketDescriptor(socketDescriptor);
if(m_tcpSocket->state() == QAbstractSocket::ConnectedState){
qDebug() << "Socket connected";
return true;
}
qDebug() << "Socket doesn't connected.";
return false;
}
Try opening the socket using the address and port in the second application on the device:
bool TransmitterServer::initSocket(QString peerAddress, QString peerPort)
{
m_tcpSocket = new QTcpSocket();
if(m_tcpSocket->bind(QHostAddress(peerAddress), peerPort.toInt(), QAbstractSocket::ShareAddress)){
qDebug() << "Socket binded";
return true;
}else{
qDebug() << "Can't bind socket " << m_tcpSocket->error();
}
return false;
}
Is it possible to achieve it on the server-side?