问题标签 [qtcpsocket]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
1387 浏览

c++ - QTcpSocket 写入数据结构

我曾经使用 C++ 套接字发送一个结构,现在我尝试使用 QTcpSockets。这是结构:

那么,为了写作,我曾经打电话给write(sockfd, &messageToSend, uiMessagePcToMastSize);

现在我正在尝试:socket->write(&messageToSend, uiMessagePcToMastSize);并且不起作用:没有匹配函数调用 'QTcpSocket::write(mastControl::st_messagePcToMast*, const unsigned int&)

我搜索了更多信息,看起来我应该将结构转换为 QDataStrem,或者只是 QByteArray。我真的不知道如何处理这个问题。

有什么帮助吗?

0 投票
2 回答
6296 浏览

c++ - 无法为不同线程中的父级创建子级

我在 MyThread.cpp 和 .h 中定义了一个 QThread。那里我有一个私人 QTcpSocket。

另一方面,我在 Functions.cpp 和 .h 中定义了一组函数,它们没有定义在 Functions 类中,而是定义在命名空间中。

然后,如果我错了,请纠正我,当我在 Functions 中调用一个函数以将 MyThread 的 QTcpSocket 连接到主机时,我假设我在同一个线程上,因为我正在从线程调用函数我已经创造,而不是一个不同的。

尽管如此,在 socket->connectToHost 上,我得到QObject: Cannot create children 为位于不同线程中的父级。两次。

这是为什么?怎么了?

0 投票
1 回答
1820 浏览

c++ - Reading a QTcpSocket

I had a socket connection with UNIX C++ sockets where, after connecting, I had a loop for reading byte by byte until I had the full msg. I know the first two bytes of the message I'm going to receive, and its length (15 bytes). So the function looked like:

Now I'm implementing the same with QTcpSockets. Connection is established, and then I call:

So I'm waiting until there is some info ready yo be read, and then call findPacket, which is now almost the same, reading byte by byte:

Looks pretty the same, but it doesn't work. Once I have waited on waitForReadyRead, I enter findPacket's loop and I am able to read the first 4 bytes received. After that, no more data is received. It remains inside findPacket's loop checking again, and again, and again, but read function always return 0 bytes read. No new info is received. Which is impossible, because server is sending the same packet once every few ms, so even if I loss some data, eventually I should be reading something.

So, what I am doing wrong? Should I wait in a different way? Should I wait again the first time read function returns 0 bytes read? What's the difference between this read function and the C++ libraries one?

0 投票
4 回答
27262 浏览

c++ - QTcpSocket 状态始终连接,即使拔掉以太网线

我有一个 QTcpSocket,我正在读一个循环。每次读取完整的数据包或出现错误时,我都会手动检查循环内套接字的状态,其中:

当我执行de程序时,一旦连接并且循环开始,它总是打印qDebug()<<"Socket status: connected. Looking for packets...";然后一直停留waitForReadyRead直到准备好读取一些数据。

问题是未检测到断开连接。如果我从操作系统选项断开网络连接,或者即使我拔下以太网线,它的行为也是一样的:套接字状态等于QAbstractSocket::ConnectedState,所以它继续,但当然没有收到任何东西。

我还尝试检测断开连接disconnected()信号(在第一次连接后)到重新连接功能:

但是信号永远不会发出,因为这段代码永远不会被执行。这是合乎逻辑的,因为它看起来像套接字状态总是ConnectedState.

如果我再次插入,连接将恢复并再次开始接收数据,但我确实想检测断开连接以在 GUI 上显示“断开连接”。

为什么 QTcpSocket 会这样,我该如何解决这个问题?

编辑:我在类构造函数中创建套接字,然后初始化调用 prepareSocket 函数:

0 投票
1 回答
1955 浏览

c++ - 连接(在 QT 项目中)不起作用

我开始使用 QT 库创建我的第一个多线程应用程序。

按照关于 QTcpServer 和 QTcpSocket 的 qt 指南,我编写了一个服务器应用程序,它使用这个构造函数创建连接:

这是incomingConnection() 函数,它在每次新客户端尝试连接时创建一个新线程:

现在,这是 MyThreadClass:

代码编译正常,但是当我启动客户端(启动服务器后)时,服务器收到以下错误:

然后服务器无法接收客户端的数据。

有谁有想法吗?

提前感谢丹尼尔

0 投票
1 回答
374 浏览

c++ - QTcpSocket 的状态

我对 QTcpSocket 库有疑问。

我创建了两个程序,它们使用相同的套接字连接与同一个客户端进行通信。不幸的是,这样做,如果程序同时写入,客户端只能通过套接字读取一个字符串(通常是第一个)。

所以我需要知道是否存在特定函数(或方法)来知道套接字是否忙,以便延迟发送第二个字符串。

提前致谢。

0 投票
2 回答
6295 浏览

c++ - 我怎么知道哪个 QTcpSocket 发出了 readyRead() 信号?

我创建了一个 hello-world 程序来帮助我理解 QTcpServer 和 QTcpSocket 是如何工作的。在其中,QTcpServer 接受新连接并将它们的readyRead()信号连接到MainWindow::onNewData()它们的新数据附加到纯文本控件的插槽。

问题是可以同时打开多个套接字。所以当我得到readyRead()信号并且我想用 读取数据时readAll(),我怎么知道是哪个套接字发出的呢?

这是代码的相关部分。我根本不在这里存储 QTcpSocket 指针,但即使我这样做了,我仍然不知道它们中的哪一个发出了信号。

0 投票
1 回答
1379 浏览

newline - Qt QTcpSocket QByteArray/QString issues with special characters and proper syntax

I'm having trouble some special characters in Qt, the 2 being the quote and newline. I'm connecting to the server using telnet, and the server automatically puts quotes at the beginning and end of the input for some reason. I'm having trouble removing both the quotes and newline.

The problem with replacing the newline is described below:

My input from telnet: Hello

After the program replaces the newline: " ello (Normal output with quotes would be "Hello")

And replacing the quotes has no effect at all. I have tried also using dual backslashes since it is a RegEx, but both still have the same problems as if there were one. Help is appreciated, thanks.

Edit: This turned out to be qDebug's formatting, not a result of using Telnet or anything.

0 投票
1 回答
6631 浏览

qt - 属性绑定未更新

我在应用程序运行时不断获取数据,但是一旦我读入数据并将其存储在地图中,我在显示数据时遇到了一些麻烦。

当我尝试在 QML 中显示数据时,它只是显示为零,尽管我可以在应用程序输出中看到它正在更新。

我使用属性绑定访问 QML 中的值(我的印象是这些会导致在更改headingSensor时更新carData.headingSensor?):


在我的数据类中,我有:


在我最初的 c++ 实现中:


它返回映射中的值,该值正在使用传入信息进行更新。

我意识到这可能不起作用,因为该属性依赖于 headingSensor 变量,尽管返回了正确的值,但该变量本身并未更新。所以,我想如果我改变它来更新headingSensor值并返回它可能会工作。

因此,在我的数据采集逻辑中,我也编写了一个更新变量的方法。

虽然这导致headingSensor除了地图中的值之外还更新了变量,但 QML 显示中仍然没有显示正确的值。它只是显示0(它最初显示时的默认值,因为它还没有从传入的数据中获取值)。

所以,我想知道,如何让 QML 中显示的 sensorHeading 的值随着它的值和/或地图中的值在 C++ 中的变化而更新?我是否需要做类似的事情:

编辑:尝试这样的事情, onSensorHeadingChanged 永远不会触发。我不知道为什么,因为 sensorHeading 的值在我在应用程序输出中观察时明显变化


0 投票
3 回答
13839 浏览

c++ - QTcpSocket 客户端自动重连

我正在尝试编写一段代码,它会定期尝试使用 QTcpSocket 连接到服务器,直到服务器启动并准备好。客户端还应该在服务器关闭时自动并定期尝试重新连接,直到它再次启动或用户手动关闭程序。

我所做的是订阅 QTcpSocket 的已连接和错误信号。当我捕捉到错误信号时,我基本上会再次调用 connectToHost 方法。

我的代码会定期尝试连接到主机,直到服务器准备好(这部分工作正常)。但是,问题是当服务器关闭时,它永远无法重新连接。当连接断开时,我按预期收到 RemoteHostClosedError 。但是,在同一方法中再次调用 connectToHost 方法(我在其中捕获 RemoteHostClosedError)后,我什么也没得到。甚至 QTcpSocket 对象也不会发出错误信号。

我在下面给出了我的代码。

}

我在调用 connectToHost 方法之前和之后检查 QTcpSocket 的状态(我在这里给出的最后一行)。在调用 connectToHost 之前,状态为 UnconnectedState,在调用 connectToHost 之后,其状态变为 Connecting。没有什么意外。但是,它既不能连接到服务器,也不能发出错误信号。

任何的想法?

注意:QTcpSocket 的 connectToHost 方法是第一次在内部调用。