QTcpSocket 真的是全双工的吗?

QTcpSocket is really full duplex?

BSD 流套接字是全双工的,这意味着两个连接方可以同时send/receive。

一个QTcpSocket(qt socket实现)具有异步支持,非阻塞模式,但只能属于一个线程,见qt docs.

Event driven objects may only be used in a single thread. Specifically, this applies to the timer mechanism and the network module.

假设我想要一个 transmit/tx 线程和一个单独的 receive/rx 线程同时使用同一个套接字和 send/receive 数据。

据我所知,这可以通过 qt 'done' signals/slots,但套接字线程永远不会真正同时执行 send() 和 receive()。它只是运行事件循环,它将以串行方式执行此操作,并在 send/receive 完成时发出信号。

是的,我的 rx 和 tx 线程可以同时工作并通过 qt 槽处理通知,但套接字本身从未真正用于全双工模式。

这样说是否正确:仅考虑一个端点,在套接字线程中,它的 send() 和 receive() 调用总是串行的,而不是同时的? (因为事件循环线程只有一个线程)

In my understanding this can be 'done' via qt signals/slots, but the socket thread will never really perform the send() and the receive() simultaneously. It just runs the event loop which will do this in a serial fashion and emit the signals when send/receive is done.

正确,但请记住内核缓冲传入和传出数据,并且 QTCPSocket 将套接字设置为非阻塞,因此 send() 和 recv() 总是 return 立即调用并且永远不会阻止事件循环。这意味着发送和接收数据的实际过程将同时发生(在内核内部),即使(或多或少是瞬时的) send() 和 recv() 调用在技术上不会发生。 (*)

Yes, my rx and tx threads can work concurrently and handle the notifications via qt slots, but the socket itself is never really used in full duplex mode. Is this correct?

这是不正确的——套接字的数据流可以(并且确实)同时在网络中双向流动,所以套接字确实是全双工的。无论您使用单线程还是多线程,都存在全双工功能。

(*) 您可以使用使用 QTCPSocket 发送或接收数据的单线程 Qt 程序对此进行测试,只需在大数据传输期间断开计算机的以太网电缆即可。如果 QTCPSocket 的 send() 或 recv() 调用一直阻塞直到完成,这将阻塞 GUI 线程并导致您的 GUI 变得无响应,直到您重新连接电缆(或直到 TCP 连接在几分钟后超时)。