【发布时间】:2016-01-17 02:06:48
【问题描述】:
我通过QTcpSocket 连接到QTcpServer。我可以在服务器端指定监听端口,但客户端选择一个随机端口进行连接。我曾尝试使用QAbstractSocket::bind 的方法,但没有任何区别。
这是我的代码:
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();
}
有人知道我错过了什么吗?
【问题讨论】:
标签: port qtcpsocket