在Linux中的应用程序之间共享TCP插座
我想分享从主应用程序到设备上第二个应用程序的现有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论