最简单的QT TCP服务器
我需要什么才能从客户端接收数据?
QTcpServer Server;
if(!Server.listen("127.0.0.1", 9000)) {
return;
}
connect(Server, SIGNAL(newConnection()), this, SLOT(ReceiveData()));
到目前为止这是正确的吗?我在 ReceiveData 中需要什么?我真的需要另一个函数来接收数据吗?我想将其保存在 QByteArray 中,
谢谢
What would I need to receive data from a client?
QTcpServer Server;
if(!Server.listen("127.0.0.1", 9000)) {
return;
}
connect(Server, SIGNAL(newConnection()), this, SLOT(ReceiveData()));
Is this correct so far? What do I need in ReceiveData? Do I really need another function to receive the data? I would like to save it in a QByteArray
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于这个问题尚未得到解答,因此这是一个非常基本的示例。
在 ReceiveData 槽中,您需要接受来自服务器的连接。
在 Qt 的 QTcpServer 中,这是通过调用 nextPendingConnection() 来完成的。
因此,QTcpServer 的 newConnection 插槽将调用您的 ReceiveData 插槽。
在您的 receivedata 槽中,您可以执行以下操作:
注意:这是一个阻塞示例,waitForReadyRead 会将线程挂起长达 5000 毫秒。
要执行非阻塞示例,您需要将另一个插槽连接到新套接字的readyread信号。
As this hasn't been answered, here's a really basic example.
In your ReceiveData slot, you would need to accept the connection from the server.
In Qt's QTcpServer this is done by calling nextPendingConnection().
So the QTcpServer's newConnection slot will call your ReceiveData slot.
In your receivedata slot, you can do something like:
Note: This is a blocking example, the waitForReadyRead will hang the thread for up to 5000 milliseconds.
To do a non blocking example, you need to connect another slot to the new socket's readyread signal.
您是否看过这个示例:
http://doc.qt.io /qt-5/qtnetwork-fortuneserver-server-cpp.html
PS:
是的,您至少需要一个回调函数来:
1) 接受新连接
2) 在连接上接收和发送数据
Have you seen this example:
http://doc.qt.io/qt-5/qtnetwork-fortuneserver-server-cpp.html
PS:
Yes, you need at least one callback function to:
1) accept new connections
2) Receive and Send data on the connect(s)