0
我通过QTcpSocket
连接到QTcpServer
。我可以在服务器端指定监听端口,但客户端为其连接选择一个随机端口。我试图使用方法QAbstractSocket::bind
但这没有什么区别。将QTcpSocket绑定到特定端口
这里是我的代码:
void ConnectionHandler::connectToServer() {
this->socket->bind(QHostAddress::LocalHost, 2001);
this->socket->connectToHost(this->ip, this->port);
if (!this->socket->waitForConnected()) {
this->socket->close();
this->errorMsg = this->socket->errorString();
}
qDebug() << this->socket->localPort();
}
有谁知道我错过了什么?