使用QTcpSocket和QTcpServer的Qt双向客户端服务器
Qt bidirectional client server using QTcpSocket and QTcpServer
我正在尝试实现一个双向客户端-服务器程序,在该程序中,客户端和服务器可以在彼此之间传递序列化对象。我正在尝试使用Qt(QTcpSocket和QTcpServer)来实现这一点。我已经用java实现了这样的程序,但我不知道如何使用Qt。我已经查看了财富客户端和财富服务器的示例。。。但据我所见,客户端只是向服务器发送信号,服务器向其发送一些数据。我需要客户端和服务器来回发送对象。我不是在寻找一个完整的解决方案,我所寻找的只是一些正确方向的指导。
我写了一些代码,它接受连接,但不接受数据。
服务器
此类是服务器;它应该接受连接并输出正在发送的缓冲区的大小。然而,它正在输出0
#include "comms.h"
Comms::Comms(QString hostIP, quint16 hostPort)
{
server = new QTcpServer(this);
hostAddress.setAddress(hostIP);
this->hostPort = hostPort;
}
void Comms::attemptConnection(){
connect(server, SIGNAL(newConnection()), this, SLOT(connectionAccepted()));
//socket = server->nextPendingConnection();
server->listen(hostAddress,hostPort);
//receivedData = socket->readAll();
}
void Comms::connectionAccepted(){
qDebug()<<"Connected";
socket = new QTcpSocket(server->nextPendingConnection());
char* rec = new char[socket->readBufferSize()];
qDebug()<<socket->readBufferSize();
}
客户
此类是客户端。它应该发送字符串"hello"。它成功发送(据我所知)
#include "toplevelcomms.h"
#include "stdio.h"
TopLevelComms::TopLevelComms(QString hostIP, quint16 hostPort)
{
tcpSocket = new QTcpSocket();
hostAddress.setAddress(hostIP);
this->hostPort = hostPort;
}
void TopLevelComms::connect(){
tcpSocket->connectToHost(hostAddress,hostPort,QIODevice::ReadWrite);
//tcpSocket->waitForConnected(1);
QString string = "Hello";
QByteArray array;
array.append(string);
qDebug()<<tcpSocket->write(array);
}
请告诉我我做错了什么,或者告诉我在Qt中建立我想要的东西的一般逻辑。
QTcpSocket默认情况下是异步的,所以当您调用connectToHost并在同一上下文中写入时,它不会被发送,因为套接字没有连接。您应该更改您的"客户"代码:
void TopLevelComms::connect(){
tcpSocket->connectToHost(hostAddress,hostPort,QIODevice::ReadWrite);
if(tcpSocket->waitForConnected()) // putting 1 as parameter isn't reasonable, using default 3000ms value
{
QString string = "Hello";
QByteArray array;
array.append(string);
qDebug()<<tcpSocket->write(array);
}
else
{
qDebug() << "couldn't connect";
}
}
注意:你也没有检查你是否能够收听
void Comms::attemptConnection(){
connect(server, SIGNAL(newConnection()), this, SLOT(connectionAccepted()));
//socket = server->nextPendingConnection();
if(server->listen(hostAddress,hostPort))
{
qDebug() << "Server listening";
}
else
{
qDebug() << "Couldn't listen to port" << server->serverPort() << ":" << server->errorString();
}
//receivedData = socket->readAll();
}
还有最后一件事。请注意,QTcpServer::nextPendingConnection()返回QTcpSocket,因此您不使用该新连接,而是使用nextPendingConnect作为父创建新的QTcpSockets
void Comms::connectionAccepted(){
qDebug()<<"Connected";
// WRONG! it will use QTcpSocket::QTcpSocket(QObject * parent)
//socket = new QTcpSocket(server->nextPendingConnection());
// use simple asign
socket = server->nextPendingConnection();
// move reading to slot
connect(socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
}
现在我们将把读取转移到单独的插槽
void Comms::readSocket()
{
// note that dynamic size array is incompatible with some compilers
// we will use Qt data structure for that
//char* rec = new char[socket->readBufferSize()];
qDebug()<<socket->readBufferSize();
// note that QByteArray can be casted to char * and const char *
QByteArray data = socket->readAll();
}
我必须承认,对于这样小的代码示例,有很多错误。您需要了解一些有关TCP/IP连接的知识。这些都是流,不能保证整个数据块会立即到达您的
看起来您有时间问题。由于客户端和服务器是不同的进程,因此在服务器的connectionAccepted()
函数尝试从套接字读取之前,无法保证整个TopLevelComms::connect()
都已执行(以及网络数据传输)。
我怀疑,如果你利用QTcpSocket的waitForReadyRead()函数,你应该会有更好的运气:
void Comms::connectionAccepted(){
qDebug()<<"Connected";
socket = new QTcpSocket(server->nextPendingConnection());
if( socket->waitForReadyRead() ) {
char* rec = new char[socket->readBufferSize()];
qDebug()<<socket->readBufferSize();
}
}
- "unknown ca"自生成的 CA、证书和客户端/服务器
- Java 客户端C++服务器数据发送/接收问题
- ZMQ - 客户端服务器:客户端意外关闭,服务器如何检测到?
- 用于本地网络运行的客户端服务器体系结构
- TCP套接字(客户端-服务器)recv()返回-1值
- C++启动另一个程序(客户端-服务器)的程序
- C++客户端/服务器聊天应用程序 - 从 Ubuntu 命令窗口扫描输入
- UDP客户端/服务器程序在不同的IP和端口上通信超过2个插座
- 为什么在此 UDP 客户端/服务器示例中没有必要绑定客户端套接字?
- TCP在客户端服务器之间传输时,数据是否有任何限制
- 是否可以添加多个服务器地址?(Asio,客户端>服务器)
- 通过TCP在客户端/ C#服务器之间传输文件C++
- TCP 客户端服务器程序
- 客户端服务器回声程序进入UDP的僵局
- 如何制作具有同步数据读/写功能的多客户端服务器
- Qt,客户端-服务器关系
- Winsock 简单客户端服务器,接收失败
- 在C++中对客户端/服务器应用进行单元测试
- 在客户端/服务器计算器C 方面遇到麻烦
- 客户端服务器简单示例非块