【发布时间】:2018-01-13 14:57:05
【问题描述】:
我知道在发出QTcpServer newConnection 时如何向新连接的客户端发送消息。我的做法是这样的:
connect(serverConnection.tcpServer, &QTcpServer::newConnection, this, &ServerInterface::sendMessages);
void ServerInterface::sendMessages()
{
QByteArray messagesToClients;
QDataStream out(&messagesToClients, QIODevice::WriteOnly);
QTcpSocket *clientConnection = serverConnection.tcpServer->nextPendingConnection();
out << inputBox->toPlainText(); //get text from QTextEdit
clientConnection->write(messagesToClients);
}
但我想要做的是,每当单击服务器中的发送消息按钮时,它都会向当前连接的客户端发送消息。我提供的代码只能向新连接的客户端发送一条新消息。我不知道如何实现我想做的事情,那么有人可以为我提供一种方法吗?我对 Qt 网络有点陌生。
谢谢。
【问题讨论】:
-
您可以通过另一个
socket广播来自QTcpServer的返回信息,并在客户端捕获此信息。 -
您需要跟踪您的客户端..我只知道“socketDescriptor”..如果您想与您的客户端通信,请尝试通过其套接字描述符将消息发送到每个客户端套接字..一个可靠的可靠的 QTcpServer 示例在这里找到youtube.com/watch?v=iKtCXUHsV70&t=43s
标签: qt qtcpserver