1

I have to send data reliable from a client using QTcpSocket to a server using QTcpServer. The connection is established once and the data is send line by line with terminator of "\r\n" (The server is splitting the incoming data on this terminator)

If need to know which line could be successful and which line not.

This works as expected. But if I unplug the server network cable from the network, the client is still writing data and the "bytesWritten" signal is still emitted.

But no write error occurred and the "error" signal is not emitted. It seems QTcpSocket is writing the data to an internal buffer even if the TPC connection is lost. Using flush() has no effect.

Example code based on the fortune client:

Client::Client(QWidget *parent)
  :   QDialog(parent), networkSession(0)
{

  m_messageCount=0;
  QString ipAddress= "192.168.1.16"; 


  m_socket = new QTcpSocket(this);
  //No effect...
  //m_socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);


  connect(m_socket, SIGNAL(connected()), this, SLOT(Connected()));
  connect(m_socket, SIGNAL(disconnected()), this, SLOT(DisConnected()));
  connect(m_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(OnBytesWritten(qint64)));
  connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)),    this, SLOT(displayError(QAbstractSocket::SocketError)));

}

void Client::connectToHost()
{
  m_socket->connectToHost("192.168.1.16", 1234);
}

void Client::Connected()
{
  qDebug() << "Connected()";
  QTimer::singleShot(1000, this, SLOT(SendNextRecord()));
}

void Client::DisConnected()
{
  qDebug() << "DisConnected()";
}

void Client::SendNextRecord()
{
  m_messageCount++;

  QByteArray singleRecord=QString("Nr: %1 Some Text").arg(m_messageCount).toUtf8();
  singleRecord.append("\r\n");

  Q_ASSERT(m_socket->isValid());
  qDebug() << "Sending: " <<singleRecord;

  //bytesSend always > 0
  qint64 bytesSend=m_socket->write(singleRecord);

  //No effect
  m_socket->flush();
  qDebug() <<"bytes Send:" << bytesSend;
}

//Signal is still emitted even if network cable is unplugged 
void Client::OnBytesWritten(qint64 bytes)
{
  qDebug() << "OnBytesWritten:" << bytes; 

  //No effect
  m_socket->flush();
  QTimer::singleShot(1000, this, SLOT(SendNextRecord()));
}

//Signal not emitted even if network cable is unplugged
void Client::displayError(QAbstractSocket::SocketError socketError)
{
   qDebug() << "Socket error";
}

Can I change this behaviour ?

pulp
  • 698
  • 1
  • 6
  • 13
  • Please check out similar issue here : http://stackoverflow.com/questions/10445122/qtcpsocket-state-always-connected-even-unplugging-ethernet-wire – baci Mar 07 '14 at 22:35
  • Also, since client has not got that ability, you may think of writing a protocol to check if connection is alive (like sending a specific data every 10 sec and write back a specific reply to that at server side) If you cant receive your reply, call DisConnected(). – baci Mar 07 '14 at 22:41
  • Why don't you just signal from the server to the client to stop the write into the buffer? Alternatively, have you tried `QIODevice::Unbuffered` to avoid the internal buffering? – László Papp Mar 08 '14 at 06:47

0 Answers0