QTcpSocket有时不发送数据
我有两个QT应用程序。一个应用程序可以被认为保存着一个大数据,它每秒向应用程序发送大约10KB的数据块 早些时候,我尝试使用QUdpSocket传输数据,但由于MTU限制在2-5K左右,并且需要自己分割和重新组合数据,因此我切换到QTcpSocket 有时使用QTcpSocket正确发送数据(特别是如果我非常频繁地~100毫秒写入数据),但有时根本不发送数据。5秒后也不行。有时几个数据块在内部缓冲很长一段时间(几秒钟),然后一起发送QTcpSocket有时不发送数据,qt,Qt,我有两个QT应用程序。一个应用程序可以被认为保存着一个大数据,它每秒向应用程序发送大约10KB的数据块 早些时候,我尝试使用QUdpSocket传输数据,但由于MTU限制在2-5K左右,并且需要自己分割和重新组合数据,因此我切换到QTcpSocket 有时使用QTcpSocket正确发送数据(特别是如果我非常频繁地~100毫秒写入数据),但有时根本不发送数据。5秒后也不行。有时几个数据块在内部缓冲很长一段时间(几秒钟),然后一起发送 m_socket = new QTcpSocket(this)
m_socket = new QTcpSocket(this);
m_socket->connectToHost(QHostAddress::LocalHost, 45454);
sendDataEverySec()
{
QByteArray datagram(10000, 'a');
qint64 len = m_socket->write(datagram);
if(len != datagram.size())
qDebug() << "Error"; //this NEVER occurs in MY case.
m_socket->flush();
}
m_socket=新的QTcpSocket(此);
m_socket->connectToHost(QHostAddress::LocalHost,45454);
sendDataEverySec()
{
QByteArray数据报(10000,‘a’);
qint64 len=m_套接字->写入(数据报);
if(len!=datagram.size())
qDebug()flush();
}
在接收器端,我使用readyRead
信号来知道数据何时到达
如何确保立即发送数据?对于我正在尝试做的事情,有没有更好的选择
编辑::当我在长时间间隔1秒后写入时,每次发送方发送数据时,我都会在接收方接收到“QAbstractSocket::SocketTimeoutError”
。如果发送方频繁写入数据,则不会收到此错误。使用QTcpSocket以我现在的方式传输数据可以吗 编辑2:在接收器端,当发出
readyRead
信号时,我再次检查,同时(m_socket->waitForReadyRead(500))
,因此我得到了“QAbstractSocket::SocketTimeoutError”
。此外,此检查阻止了单个块的传递。在浏览了更多文档之后,当有新数据可用时,
readyRead
似乎会不断发出,因此不需要waitForReadyRead
我正在接收发送的所有数据,但仍然无法立即收到数据。有时两个或三个块被合并。这可能是因为接收器端读取数据时出现延迟等原因。我的客户机服务器应用程序的典型解决方案 在服务器端:
class Server: public QTcpServer {
public:
Server(QObject *parent = 0);
~Server();
private slots:
void readyRead();
void disconnected();
protected:
void incomingConnection(int);
};
关于cpp:
void Server::incomingConnection(int socketfd) {
QTcpSocket *client = new QTcpSocket(this);
client->setSocketDescriptor(socketfd);
connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
void Server::disconnected() {
QTcpSocket *client = (QTcpSocket*) sender();
qDebug() << " INFO : " << QDateTime::currentDateTime()
<< " : CLIENT DISCONNECTED " << client->peerAddress().toString();
}
void Server::readyRead() {
QTcpSocket *client = (QTcpSocket*) sender();
while (client->canReadLine()) {
//here i needed a string..
QString line = QString::fromUtf8(client->readLine()).trimmed();
}
}
void Client::readyRead() {
// if you need to read the answer of server..
while (this->canReadLine()) {
}
}
void Client::doConnect() {
this->connectToHost(ip_, port_);
qDebug() << " INFO : " << QDateTime::currentDateTime()
<< " : CONNESSIONE...";
}
void Client::connected() {
qDebug() << " INFO : " << QDateTime::currentDateTime() << " : CONNESSO a "
<< ip_ << " e PORTA " << port_;
//do stuff if you need
}
void Client::sendMessage(const QString& message) {
this->write(message.toUtf8());
this->write("\n"); //every message ends with a new line
}
关于cpp:
void Server::incomingConnection(int socketfd) {
QTcpSocket *client = new QTcpSocket(this);
client->setSocketDescriptor(socketfd);
connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
}
void Server::disconnected() {
QTcpSocket *client = (QTcpSocket*) sender();
qDebug() << " INFO : " << QDateTime::currentDateTime()
<< " : CLIENT DISCONNECTED " << client->peerAddress().toString();
}
void Server::readyRead() {
QTcpSocket *client = (QTcpSocket*) sender();
while (client->canReadLine()) {
//here i needed a string..
QString line = QString::fromUtf8(client->readLine()).trimmed();
}
}
void Client::readyRead() {
// if you need to read the answer of server..
while (this->canReadLine()) {
}
}
void Client::doConnect() {
this->connectToHost(ip_, port_);
qDebug() << " INFO : " << QDateTime::currentDateTime()
<< " : CONNESSIONE...";
}
void Client::connected() {
qDebug() << " INFO : " << QDateTime::currentDateTime() << " : CONNESSO a "
<< ip_ << " e PORTA " << port_;
//do stuff if you need
}
void Client::sendMessage(const QString& message) {
this->write(message.toUtf8());
this->write("\n"); //every message ends with a new line
}
void客户端::readyRead(){
//如果您需要阅读服务器的答案。。
而(此->canReadLine()){
}
}
无效客户端::doConnect(){
此->连接到主机(ip、端口);
qDebug()在接收器端,当readyRead信号发出时,我再次检查,同时(m_socket->waitForReadyRead(500))
,因此我得到了“QAbstractSocket::SocketTimeoutError”
。此外,该检查阻止了单块的传递
在浏览了更多文档之后,当新数据可用时,readyRead似乎会不断发出,因此不需要waitForReadyRead
。
它解决了我的问题。你能尝试将QAbstractSocket::LowDelayOption标志设置为setSocketOption API吗?@Kunal,我尝试了LowDelayOption和KeepAliveOption,但都没有成功。有趣的是,一旦发送方发送数据,接收方就会显示“QAbstractSocket::SocketTimeoutError”。这发生在以1秒的间隔写入数据时。我确信问题在于接收端。请向我们展示如何处理传入连接。