小编Quo*_*uoi的帖子

QT QTcpServer :: incomingConnection(qintptr句柄)没有触发?

我正在尝试第一次使用Qt创建一个多线程服务器.通常会使用QTcpServer::nextPendingConnection()已经烘焙的套接字句柄返回的套接字指针- 但由于我在单独的线程上与连接客户端连接,我需要使用qintptr handlefrom QTcpServer :: incomingConnection(qintptr handle)单独创建套接字).经过一个非常沉闷,错误打包的调试会话后,我设法将问题追踪到了QTcpServer::incomingConnection()永远不会被解雇?

有没有人有类似的问题 - 最新版本Qt有什么变化?

这些是我尝试过的:

  • QTcpServer::incomingConnection(qintptr handle)
  • QTcpServer::incomingConnection(qintptr socketDescriptor)
  • QTcpServer::incomingConnection(int handle)

编辑:

创建服务器实例:

TestServer *myServer = new TestServer();
myServer->initializeServer(1234);
Run Code Online (Sandbox Code Playgroud)

哪个电话:

void TestServer::initializeServer(quint16 port)
{

mainServer = new QTcpServer(this);

mainServer->listen(QHostAddress::Any, port);

qDebug() << "Listening for connections on port: " << port;



}
Run Code Online (Sandbox Code Playgroud)

服务器正在侦听.当客户端连接incomingConnection(qintptr句柄)应该被调用:

void TestServer::incomingConnection(qintptr socketDescriptor){

TestClient *client = new TestClient(this);
client->setSocket(socketDescriptor);

}
Run Code Online (Sandbox Code Playgroud)

哪个电话:

void TestClient::setSocket(quint16 socketDescr)
{
socket = new QTcpSocket(this);

socket->setSocketDescriptor(socketDescr);

connect(socket, SIGNAL(connected()),this,SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(socket, SIGNAL(readyRead()),this,SLOT(readyRead())); …
Run Code Online (Sandbox Code Playgroud)

sockets qt tcp

3
推荐指数
1
解决办法
4781
查看次数

标签 统计

qt ×1

sockets ×1

tcp ×1