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();
}
有誰知道我錯過了什麼?