我在Windows 7上编写了TcpServer。Qt 5.7
当我使用Telnet连接到127.0.0.1
和端口4545
时,服务器正常工作
TCPSERVER:
_dataChannel = new QTcpServer(this);
connect(_dataChannel, QTcpServer::newConnection, this, &TCPTransfer::startDataSession);
if(!_dataChannel->listen(QHostAddress::Any, _dataPort))
{
throw TCPException(QString("Can't start listen on %1").arg(_dataPort));
}
qDebug() << "TcpTransfer:: Data channel was start at " << _dataChannel->serverAddress() << " : " << _dataChannel->serverPort();
//...
void TCPTransfer::startDataSession()
{
_dataSocket = _dataChannel->nextPendingConnection();
qDebug() << "TcpTransfer:: Data socket was created";
if(!_dataSocket->open(QIODevice::ReadWrite))
{
emit TCPException("Can't open data socket for Read/Write");
return;
}
qDebug() << "TcpTransfer:: Data socket was opened";
connect(_dataSocket, &QTcpSocket::disconnected, _dataSocket, &QTcpSocket::deleteLater);
connect(_dataSocket, &QTcpSocket::readyRead, this, &TCPTransfer::receiveData);
}
//...
void TCPTransfer::receiveData()
{
QByteArray data;
while(_dataSocket->bytesAvailable() > 0)
{
data.append(_dataSocket->readAll());
if(data.isEmpty())
{
emit ErrorOccurred("Can't read TCP buffer");
return;
}
}
qDebug() << "TcpTransfer:: Data received: " << data;
emit newTCPData(data);
}
但是,当我尝试从测试方法(#include <QTest>
连接所有正在运行的测试private slots
中的TestTcpServer
)到服务器连接时,永远不会从QAbstractSocket::ConnectingState
变为{{{} 1}}和QAbstractSocketConnectedState
信号永远不会发出。
我不知道,这是因为插槽,还是因为测试,或其他什么!
TcpClient代码:
connected()