我通过qtcpsocket与设备建立了连接。顺便说一下 Windows 10操作系统。当我连接到主机时,防火墙和Windows的安全性被禁用,连接通过。方法errorString向我推了一个未知错误,这意味着什么都没发生,但随着它发出信号socketError的时间,其含义为QAbstractSocket :: NetworkError(0x0007),这意味着“网络发生错误(例如,网络电缆意外发生)插入)”。 与设备ping通,但是当我尝试与其连接时,一段时间后我收到错误信号。
我的软件或操作系统中的主要问题在哪里?我该如何检查?
查看下面的所有代码:
// Get model of message for option
TcpConnectMessageModel *messageModel = dynamic_cast<TcpConnectMessageModel *>(_messageModel);
if (messageModel == nullptr)
{
systemMessagesController->SendConnectResultMessage(deviceName, ConnectResult::WrongConnectMessage, errors);
break;
}
// initializing data for connection
options = messageModel->GetOptions(message);
bool ok = false;
// create socket and connect it with hostAddress и ip:
senderSocket = QTcpSocketPtr(new QTcpSocket());
// when connection succesful
if(!connect(senderSocket.data(), &QTcpSocket::connected,this, [=](){
qDebug() << "Connect success" << "!" << senderSocket->errorString();
systemMessagesController->SendConnectResultMessage(deviceName, ConnectResult::Success, errors);
connected = true;
}))
{
errors << senderSocket->errorString();
systemMessagesController->SendConnectResultMessage(deviceName, ConnectResult::WrongConnectMessage, errors);
}
//connected = true;
if(!connect(senderSocket.data(), QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
[=](QAbstractSocket::SocketError socketError){
QString tmp = senderSocket->errorString();
//errors << tmp;
systemMessagesController->SendDisconnectResultMessage(deviceName, DisconnectResult::Error, errors);
}))
{
errors << senderSocket->errorString();
systemMessagesController->SendConnectResultMessage(deviceName, ConnectResult::WrongConnectMessage, errors);
break;
}
// stop socket when disconnect received
if(!connect(this, &TcpLinkAgent::finished,this, [=](){
if(!connected) return;
senderSocket->close();
qDebug() << "disconnected success" << "!" << senderSocket->errorString();
disconnect(senderSocket.data(), 0, 0, 0);
// socket turned off succesfully:
if (senderSocket->openMode() == QIODevice::NotOpen)
{
systemMessagesController->SendDisconnectResultMessage(deviceName, DisconnectResult::Success, errors);
senderSocket.reset();
connected = false;
}
else
{
QStringList errors;
systemMessagesController->SendDisconnectResultMessage(deviceName, DisconnectResult::Error, errors);
}
}))
{
errors << senderSocket->errorString();
systemMessagesController->SendConnectResultMessage(deviceName, ConnectResult::WrongConnectMessage, errors);
break;
}
if (!connect(senderSocket.data(), &QTcpSocket::readyRead, this, &TcpLinkAgent::DataReadyHandler, Qt::ConnectionType::QueuedConnection))
{
systemMessagesController->SendConnectResultMessage(deviceName, ConnectResult::WrongConnectMessage, errors);
break;
}
senderSocket->connectToHost(QHostAddress(options.ip), options.port.toUInt());
//This lines showed that all ok.
QString tmp = senderSocket->errorString();