TCP Connection Retransmition Problem
Unsolved
General and Desktop
-
I am working on a project. in this project I am using tcp client.
When I send any message to the server, the server transmits messages to me in packets (total message length is always constant) . But most of the time there is a freeze. When I check via wireshark, I see that I get TCP Retransmission - TCP Dup ACK errors.
C++ code :
#include "client.h" Client::Client(QObject *parent) : QObject{parent} { _client = new QTcpSocket(this); connect(_client, &QTcpSocket::readyRead, this, &Client::readyRead); } Client::~Client() { _client->abort(); _client->close(); } void Client::connectToHost(QString address, int port) { if(_client->isOpen()){ _client->disconnect(); } REMOTE_ADDRESS = address; REMOTE_PORT = port; _client->connectToHost(REMOTE_ADDRESS, REMOTE_PORT); QTimer::singleShot(3000, this, &Client::sendHi); } void Client::disconnect() { _client->close(); _client->abort(); _client->flush(); } void Client::readyRead() { if(_data.size() < _max_len){ _data += _client->read(_max_len - _data.size()); } if(_data.size() == _max_len){ emit sendData(_data); _data.clear(); QByteArray data = QString("hi").toUtf8(); _client->write(data, data.size()); } else if(_data.size() > _max_len){ _data.clear(); QByteArray data = QString("hi").toUtf8(); _client->write(data); } } void Client::sendHi() { if(!_client->isOpen()){ qDebug() << "Could not connect"; return; } QByteArray data = QString("hi").toUtf8(); _client->write(data,data.size()); qDebug() << "First 'hi' has been sent : " << QDateTime::currentDateTime(); }
also when I run the same process on a simple python code, I get the same error less than my qt c++ code.
Python code :
import socket HOST = '192.168.1.10' PORT = 5001 def receive_data(sock, length): data = b'' while len(data) < length: more = sock.recv(length - len(data)) if not more: raise EOFError('ERR') data += more return data sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) sock.connect((HOST, PORT)) print(f'{HOST}:{PORT} connected') num_samples = 4096 for _ in range(10000): sock.sendall(b'hi') data = receive_data(sock, num_samples*4*2) time.sleep(.001) time.sleep(1) sock.close()
why am i getting this error and how can i handle this error? what is your suggestion?