在Linux中的应用程序之间共享TCP插座

发布于 2025-01-28 06:55:40 字数 1797 浏览 2 评论 0原文

我想分享从主应用程序到设备上第二个应用程序的现有TCP套接字,并继续与客户端进行通信。应用程序在Linux系统中起作用。我在qabstractsocket 文档模式 qabstractsocket :: ShareadDress ,但我无法实现一个工作示例。我试图使用套接字描述符共享套接字,或使用IP地址和端口创建 QTCPSOCKECT 对象。

演示实现服务器端

[第一个应用程序]

服务器上的incomingConnection 在服务器上重新实现:

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();
}

通信是正确的,并且可以与客户端进行通信。

[第二个应用程序]

尝试使用设备上第二个应用程序中的套接字描述符打开套接字:

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;
}

尝试使用设备上第二个应用程序中的地址和端口打开插座:

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;
}

可以在服务器端实现它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文